Robo Lec
Robo Lec
I. Introduction
II. Overview of Commercial Robots
III. Designing the Arduino Robot
IV. Programming Fundamentals: Loops, Conditionals, Variables, Functions, and Parameters
V. Assembling the Hardware Components of an Arduino Robot
VI. Programming the Arduino Robot
VII. Testing the Arduino Robot
VIII. Troubleshooting
Arduino Programming
I. Introduction
Microcontroller vs Microprocessor
Arduino vs Rasberry Pi
An embedded system is a combination of computer hardware and software designed for a specific
function. Embedded systems may also function within a larger system. The systems can be
programmable or have a fixed functionality. Industrial machines, consumer electronics, agricultural
and processing industry devices, automobiles, medical equipment, cameras, digital watches, household
appliances, airplanes, vending machines and toys, as well as mobile devices, are possible locations for
an embedded system.
While embedded systems are computing systems, they can range from having no user interface (UI) --
for example, on devices designed to perform a single task -- to complex graphical user interfaces
(GUIs), such as in mobile devices. User interfaces can include buttons, LEDs (light-emitting diodes) and
touchscreen sensing. Some systems use remote user interfaces as well.
Markets and Markets, a business-to-business (B2B) research firm, predicted that the embedded market
will be worth $116.2 billion by 2025. Chip manufacturers for embedded systems include many well-
known technology companies, such as Apple, IBM, Intel and Texas Instruments. The expected growth
is partially due to the continued investment in artificial intelligence (AI), mobile computing and the
need for chips designed for high-level processing.
Embedded systems are used in a wide range of technologies across an array of industries. Some
examples include:
Automobiles. Modern cars commonly consist of many computers (sometimes as many as 100), or
embedded systems, designed to perform different tasks within the vehicle. Some of these systems
perform basic utility functions and others provide entertainment or user-facing functions. Some
embedded systems in consumer vehicles include cruise control, backup sensors, suspension control,
navigation systems and airbag systems.
Mobile phones. These consist of many embedded systems, including GUI software and hardware,
operating systems (OSes), cameras, microphones, and USB (Universal Serial Bus) I/O (input/output)
modules.
Industrial machines. They can contain embedded systems, like sensors, and can be embedded systems
themselves. Industrial machines often have embedded automation systems that perform specific
monitoring and control functions.
Medical equipment. These may contain embedded systems like sensors and control mechanisms.
Medical equipment, such as industrial machines, also must be very user-friendly so that human health
isn't jeopardized by preventable machine mistakes. This means they'll often include a more complex OS
and GUI designed for an appropriate UI.
Embedded systems always function as part of a complete device -- that's what's meant by the term
embedded. They are low-cost, low-power-consuming, small computers that are embedded in other
mechanical or electrical systems. Generally, they comprise a processor, power supply, and memory and
communication ports. Embedded systems use the communication ports to transmit data between the
processor and peripheral devices -- often, other embedded systems -- using a communication protocol.
The processor interprets this data with the help of minimal software stored on the memory. The
software is usually highly specific to the function that the embedded system serves.
Often, embedded systems are used in real-time operating environments and use a real-time operating
system (RTOS) to communicate with the hardware. Near-real-time approaches are suitable at higher
levels of chip capability, defined by designers who have increasingly decided the systems are generally
fast enough and the tasks tolerant of slight variations in reaction. In these instances, stripped-down
versions of the Linux operating system are commonly deployed, although other OSes have been pared
down to run on embedded systems, including Embedded Java and Windows IoT (formerly Windows
Embedded).
can be embedded in a larger system to perform a specific function, as they are built for
specialized tasks within the system, not various tasks;
can be either microprocessor-based or microcontroller-based -- both are integrated circuits that
give the system compute power;
are often used for sensing and real-time computing in internet of things (IoT) devices, which are
devices that are internet-connected and do not require a user to operate;
can vary in complexity and in function, which affects the type of software, firmware and
hardware they use; and
are often required to perform their function under a time constraint to keep the larger system
functioning properly.
Embedded systems vary in complexity but, generally, consist of three main elements:
Hardware. The hardware of embedded systems is based around microprocessors and microcontrollers.
Microprocessors are very similar to microcontrollers and, typically, refer to a CPU (central processing
unit) that is integrated with other basic computing components such as memory chips and digital
signal processors (DSPs). Microcontrollers have those components built into one chip.
Software and firmware. Software for embedded systems can vary in complexity. However, industrial-
grade microcontrollers and embedded IoT systems usually run very simple software that requires little
memory.
Real-time operating system. These are not always included in embedded systems, especially smaller-
scale systems. RTOSes define how the system works by supervising the software and setting rules
during program execution.
In terms of hardware, a basic embedded system would consist of the following elements:
The sensor reads external inputs, the converters make that input readable to the processor, and the
processor turns that information into useful output for the embedded system.
There are a few basic embedded system types, which differ in their functional requirements. They are:
Mobile embedded systems are small-sized systems that are designed to be portable. Digital
cameras are an example of this.
Networked embedded systems are connected to a network to provide output to other systems.
Examples include home security systems and point of sale (POS) systems.
Standalone embedded systems are not reliant on a host system. Like any embedded system,
they perform a specialized task. However, they do not necessarily belong to a host system,
unlike other embedded systems. A calculator or MP3 player is an example of this.
Real-time embedded systems give the required output in a defined time interval. They are often
used in medical, industrial and military sectors because they are responsible for time-critical
tasks. A traffic control system is an example of this.
Medium-scale embedded systems use a larger microcontroller (16-32 bit) and often link
microcontrollers together.
Sophisticated-scale embedded systems often use several algorithms that result in software and
hardware complexities and may require more complex software, a configurable processor
and/or a programmable logic array.
There are several common embedded system software architectures, which become necessary as
embedded systems grow and become more complex in scale. These include:
Simple control loops call subroutines, which manage a specific part of the hardware or
embedded programming.
Interrupt controlled systems have two loops: a main one and a secondary one. Interruptions in
the loops trigger tasks.
Very large-scale integration, or VLSI, is a term that describes the complexity of an integrated circuit
(IC). VLSI is the process of embedding hundreds of thousands of transistors into a chip, whereas LSI
(large-scale integration) microchips contain thousands of transistors, MSI (medium-scale integration)
contains hundreds of transistors, and SSI (small-scale integration) contains tens of transistors. ULSI, or
ultra-large-scale integration, refers to placing millions of transistors on a chip.
VLSI circuits are common features of embedded systems. Many ICs in embedded systems are VLSIs,
and the use of the VLSI acronym has largely fallen out of favor.
One area where embedded systems part ways with the operating systems and development
environments of other larger-scale computers is in the area of debugging. Usually, developers working
with desktop computer environments have systems that can run both the code being developed and
separate debugger applications that can monitor the embedded system programmers generally cannot,
however.
Some programming languages run on microcontrollers with enough efficiency that rudimentary
interactive debugging is available directly on the chip. Additionally, processors often have CPU
debuggers that can be controlled -- and, thus, control program execution -- via a JTAG or similar
debugging port.
In many instances, however, programmers need tools that attach a separate debugging system to the
target system via a serial or other port. In this scenario, the programmer can see the source code on the
screen of a general-purpose computer, just as would be the case in the debugging of software on a
desktop computer. A separate, frequently used approach is to run software on a PC that emulates the
physical chip in software. This is essentially making it possible to debug the performance of the
software as if it were running on an actual physical chip.
Broadly speaking, embedded systems have received more attention to testing and debugging because a
great number of devices using embedded controls are designed for use, especially in situations where
safety and reliability are top priorities.
Embedded systems date back to the 1960s. Charles Stark Draper developed an integrated circuit in 1961
to reduce the size and weight of the Apollo Guidance Computer, the digital system installed on the
Apollo Command Module and Lunar Module. The first computer to use ICs, it helped astronauts
collect real-time flight data.
In 1965, Autonetics, now a part of Boeing, developed the D-17B, the computer used in the Minuteman I
missile guidance system. It is widely recognized as the first mass-produced embedded system. When
the Minuteman II went into production in 1966, the D-17B was replaced with the NS-17 missile
guidance system, known for its high-volume use of integrated circuits. In 1968, the first embedded
system for a vehicle was released; the Volkswagen 1600 used a microprocessor to control its electronic
fuel injection system.
By the late 1960s and early 1970s, the price of integrated circuits dropped and usage surged. The first
microcontroller was developed by Texas Instruments in 1971. The TMS1000 series, which became
commercially available in 1974, contained a 4-bit processor, read-only memory (ROM) and random-
access memory (RAM), and it cost around $2 apiece in bulk orders.
Also, in 1971, Intel released what is widely recognized as the first commercially available processor, the
4004. The 4-bit microprocessor was designed for use in calculators and small electronics, though it
required eternal memory and support chips. The 8-bit Intel 8008, released in 1972, had 16 KB of
memory; the Intel 8080 followed in 1974 with 64 KB of memory. The 8080's successor, the x86 series,
was released in 1978 and is still largely in use today.
In 1987, the first embedded operating system, the real-time VxWorks, was released by Wind River,
followed by Microsoft's Windows Embedded CE in 1996. By the late 1990s, the first embedded Linux
products began to appear. Today, Linux is used in almost all embedded devices.
While some embedded systems can be relatively simple, they are becoming more complex, and more
and more of them are now able to either supplant human decision-making or offer capabilities beyond
what a human could provide. For instance, some aviation systems, including those used in drones, are
able to integrate sensor data and act upon that information faster than a human could, permitting new
kinds of operating features.
The embedded system is expected to continue growing rapidly, driven in large part by the internet of
things. Expanding IoT applications, such as wearables, drones, smart homes, smart buildings, video
surveillance, 3D printers and smart transportation, are expected to fuel embedded system growth.
The purpose of this paper is to introduce the sketch writing fundamentals to all new comers to the
Arduino world. It is meant to be a beginners guide that includes detailed explanation about the basic
statements and functions. Most of the sketches I use are taken from the Arduino IDE for their
simplicity. I wrote this guide when I first started working with Arduino as a way to help me code
easier.
For the purpose of this paper, I modified it this document a little bit to make it a better fit for everyone.
I hope you like it, if you have comments or questions do not hesitate to ask.
To load the most basic Arduino sketch, open the Arduino IDE click on file then examples then 01.Basics
and select the Bare Minimum.
void setup() {
void loop() {
Essentially this is a complete code, it complies and can be uploaded to the Arduino but there is nothing
in this code. It is important to understand this code structure because every Arduino sketch will
contain a void setup() and a void loop() functions even if they are empty. The sketch will not compile
without either one.
The void setup() is the first function to be executed in the sketch and it is executed only once. It usually
contains statements that set the pin modes on the Arduino to OUTPUT and INPUT, example:
pinMode(11, INPUT);
There are other statements that can be included in the void setup(),the above functions were only
examples, but the important thing is to remember that the setup() is a statement that runs only once at
the beginning of the sketch.
The void loop() is a function that executes indefinitely until you power off the Arduino. The functions
in the void loop() usually manipulates the Arduino’s I/Os , example: Write a HIGH or LOW to a
certain pin, and the data collected from them , example: Change the temperature sensor value from
Celsius to Fahrenheit . You can also call upon local and global variables as well as other functions. In
short this were the magic happens.
You will need and Arduino, a solder less bread board, an LED, and two jumper wires.
Connect the short –ve prong of the LED to the GND on the Arduino and the long +ve prong to PIN 13
on the Arduino.
The Sketch:
void setup() {
void loop() {
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
void setup() {
We already established that the void setup() is a function that runs only once at the beginning of the
sketch. Within this function there is a statement called pinMode(). The job of the pinMode statement is
to set the Arduino pins to OUTPUT or INPUT. OUTPUT means that this pin will produce a result like
turning on or off an LED. INPUT is used to prepare the pin to receive information from a connected
device like a sensor.
pinMode(Pin, Mode): The Pin can be any physical pin on the Arduino, you can use Pin number like 1,
2, 3 etc… or you can use the variable name assigned to this pin, example LED1, pinLed, etc… The
Mode is OUTPUT or INPUT in capital letters. Example:
pinMode(11, OUTPUT);
pinMode(echo, INPUT);
Curly braces{}: Are used to define the beginning and the end of functions and certain statements. The
braces are not only used with the void setup(), they are used throughout the sketch including the void
loop, if statements, user defined functions, etc… every opening { should be followed by a closing }
otherwise the code will produce an error when compiling.
Semicolon ;: Are used to define the end of a statement. Missing a semicolon will also produce an error
while compiling the code. They are also be found separating elements in a for loop.
Line comment //: Anything written after the // is not used by the program and they are optional.
However it is usually good practice to add comments so that other people can understand the code also
later on when the code gets bigger and more complicated, it will help the programmer not to lose track
of the code.
Back to the code:
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
Within the curly braces of the void loop there are two new statements: digitalWrite() and delay().
digitalWrite() changes the status of a pin by either writing a 5V or 0V using the following syntax:
delay() is straight forward, it is used to delay the execution of the next statement by the amount of
milliseconds within the (). Example:
delay(1000) means delay the execution of the next statement by 1000 milliseconds or 1 second.
Step 3: Variables
In this example I will introduce the concept of variables and the statements Serial.begin(),
Serialprintln() and digitalRead(). So fire up the Arduino IDE and load the DigitalReadSerial sketch
from File à Examples à 01.Basics.
You will need an Arduino, a push button, a 10Kohm resistor, a solder less breadboard and some
jumper cables. Connect the push button on the breadboard as in the picture. One pin of the button
connects on the 5v on the Arduino, the other pin connects to the resistor then to the GND on the
Arduino. The third pin connects to Pin 2 on the Arduino.
The Sketch:
int pushButton = 2; // digital pin 2 has a pushbutton attached to it. Give it a name
void setup() {
void loop() {
int pushButton = 2;
int pushButton = 2; is a variable declaration statement that precedes the void setup() function. A
variable is the bread and butter of all programming. It is used to store information to be used later in
the code. In this case we are declaring that a variable called pushButton of the type int(integer) is
assigned to digital pin number 2 on the Arduino. Variables that are declared before the void setup() are
called global variables because they can be used by any function within the code. Since we declared
pushButton before the void setup(), this variable can be used in the void setup() and the void loop().
On the other hand variables that are declared within a function, can only be used by that function and
they are called local variables (we will see an example later on).
void setup() {
Serial.begin(9600); // initialize serial communication at 9600 bits per second
Serial.begin(): starts the serial monitor port and sets the speed rate in bauds per second. According to
arduino.cc you can use one of the following speeds to communicate with the computer 300, 600, 1200,
2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. This is useful to display information on
your computer screen. In this case we will display the state of the button by displaying 1 if the button is
pressed and 0 if the button is not pressed. Once the program is uploaded to the Arduino you can start
the serial monitor by clicking on Tools then Serial Monitor from the Arduino IDE.
pinMode(pushbutton, INPUT): Sets the mode of the pin defined by the variable pushbutton to INPUT.
void loop() {
int buttonState = digitalRead(pushButton); // read the input pin:
A new variable is declared within the void loop() called buttonState (local variable). This variable is of
the type int and is initialized with the value of the pin 2. Here is a good example on how to initialize a
variable without using a number but by using statement called digitalRead().
digitalRead(): is a statement that can be used with pins that are set to INPUT. Since pin 2 was set to
INPUT mode, using digitalRead() we can read the value in this pin. In this case the value will be either
1 if the button is pressed or 0 if the button is not pressed. The syntax is digitalRead(pin# or variable);.
Serial.println(): This statement prints the value within the brackets on the serial monitor with a line
break. You can use the statement Serial.print() to print without line break. In this sketch the value
stored in the variable buttonState will be printed.
delay(1): This is a delay for only 1 millisecond used to give stability in between reads
In this example I will demonstrate the use of the two statements analogRead() and analogWrite(). The
sketch I am using is called ReadAnalogVoltage that it can be loaded from File/ Examples/01.Basics,
and I added few lines to demonstrate analogwrite().
You will need an Arduino, a potentiometer, an LED and few jumper wires. Connect the middle pin of
the potentiometer to pin A0 (analog 0) on the Arduino, then the +ve lead to the 5v on the Arduino and
the -ve lead to the GND on the Arduino. And Connect the +ve Lead of the LED to D5 on the Arduino
and the -ve lead to GND.
In this Sketch the value of the pin A0 will be printed on the serial monitor and at the same time the
brightness of the LED will change when we turn the potentiometer.
The Sketch:
void setup() {
void loop() {
float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog reading (which goes from 0 -
1023)
to a voltage (0 - 5V)
int LEDpin = 5;
This is a variable of the type int called LEDpin decalring that there is an LED on pin 5 on the Arduino.
void setup() {
Serial.begin(9600);
pinMode(LEDpin, OUTPUT);
Serial.begin(9600): Starts the serial monitor with the speed 9600 bauds per seconds
pinMode(LEDpin, OUTPUT): Sets the pin Mode of pin 5 on the Arduino to OUTPUT.
void loop() {
analogWrite(LEDpin, voltage);
Serial.println(voltage);
analogRead(): The first line is declaring a new local variable called sensorValue of the type int and is
assigned the value that we read from the analog pin A0 on the Arduino. The digital pins can read and
write 1 and 0, however the analog pins on the Arduino can read and write values between 0 and 1023.
The second line introduces a new variable of the type float (with decimals) called voltage, the value of
this new local variable is sensorValue*(5.0/1023.0). Now we can see that the value of a variable can be
manipulated using a mathematical equation. Since the analog pins yield results between 0 and 1023,
and we want to convert this value to volts between 0 and 5 volts. So we take the value we got in
sensorValue and multiply it by 5.0/1023.0 this gives us a value between 0 and 5 volts.
Example: If we turn the potentiometer half way, the analog read will be 512. Then we assign this value
512 to sensorValue.
According to the code float voltage = sensorvalue*(5.0/1023.0) then voltage = 512* (5.0/1023.0) = 2.50
volts
analogWrite(): using this statement we can write a new value into a pin, using the example above,
assigning the value voltage to LEDpin will power up the LED if there is enough voltage.
Serial.println(): this will print the value of voltage on the serial monitor.
The LED that I used will turn on faintly when the voltage reaches 1.3 volts
Step 5: If Statement
I will use the same circuit as in the previous example and turn on the LED using an if/else statement.
The if statement is a very efficient way to introduce condition to the code. I will use the Sketch called
IfStatementConditional found in the Arduino IDE under File/Examples/05.Control.
You will need an Arduino, a potentiometer, an LED and few jumper wires. Connect the middle pin of
the potentiometer to pin A0 (analog 0) on the Arduino, then the +ve lead to the 5v on the Arduino and
the -ve lead to the GND on the Arduino. And Connect the +ve Lead of the LED to D5 on the Arduino
and the -ve lead to GND.
In this Sketch the value of the pin A0 will be printed on the serial monitor and at the same time the
brightness of the LED will change when we turn the potentiometer.
The Sketch
const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input
void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
void loop() {
if (analogValue > threshold) { // if the analog value is high enough, turn on the LED:
digitalWrite(ledPin, HIGH);
else {
digitalWrite(ledPin, LOW);
As usual we start with the variables, this time I opted to use “const int” as a type, this means that the
value of the variables will never change.
Serial.begin(9600);
There is nothing new in the void setup, we set the pin 5 to OUTPUT since there is an LED attached to it
and we start the serial monitor at 9600bps
void loop() {
int analogValue = analogRead(analogPin);
digitalWrite(ledPin, HIGH);
else {
digitalWrite(ledPin, LOW);
Serial.println(analogValue);
delay(1);
The first line in the void loop is the declaration of a local variable of the type int called analogValue and
we assign to it the value we read from the analogPin variable.
Syntax:
or
Every time we are using an if statement, we are looking to meet a certain condition, example if "a" is
bigger than "b". And if the result is true then we can execute whatever is in between the curly brackets.
If the result is not true then do nothing.
Now if you are using an if/else statement, and the result is not true, then whatever is under "else" will
execute.
The best way to set a condition is to use comparison and logical operators.
Comparison operators:
They are used to compare variables and constants against each other to verify if a certain condition is
met.
Note: the difference between = and ==. = is used to assign a variable a number or a value, while == is
used to compare two variable.
Logical operators:
Logical operators are used to compare two expressions and returns a TRUE or FALSE. The logical
operators are AND(&&), OR(||), and NOT (!).
Logical AND if (a>b && c<d) Only true if both expressions are true
Logical OR if(a>b || c<d) True if one or both of the expressions is true
digitalWrite(ledPin, HIGH);
else {
digitalWrite(ledPin, LOW);
Here we are testing to see if the analogValue is bigger than the threshold : if (analogValue > threshold)
if so then turn on the LED: digitalWrite(ledPin, HIGH);
If not : else {
Then turn off the LED: digitalWrite(ledPin, LOW);
Serial.println(analogValue): print the value of analogValue on the serial monitor
This example is all about the for statement. I will introduce the syntax and how to use it. I will also use
a sketch directly from the Arduino IDE. Load the sketch called ForLoopIteration from
File/Examples/05.Control.
You will need an Arduino, 6 red LEDs, 6 220ohms resistors, a breadboard and few jumper wires.
Connect the +ve lead of each LED to a resistor and connect them on PINS 2,3,4,5,6, and 7 on the
Arduino. Then connect the –ve leads to the GND on the Arduino.
The sketch:
int timer = 100; // The higher the number, the slower the timing.
void setup() {
for (int thisPin = 2; thisPin < 8; thisPin++) { // use a for loop to initialize each pin as an output
pinMode(thisPin, OUTPUT);
void loop() {
for (int thisPin = 2; thisPin < 8; thisPin++) { // loop from the lowest pin to the highest:
delay(timer);
for (int thisPin = 7; thisPin >= 2; thisPin--) { // loop from the highest pin to the lowest:
digitalWrite(thisPin, HIGH); // turn the pin on:
delay(timer);
Since this exercise is about the for statement or loop, I will start by explain how to use it.
Syntax:
statement(s);
The for loop is used to repeat the statement(s) enclosed within the curly brackets a number of times
defined by the increment and the condition.
For example:
“a”=0, is “a” less than 10? Yes; print “a” then add 1 to “a”
“a”=1, is “a” less than 10? Yes; print “a” then add 1 to “a”
“a”=2, is “a” less than 10? Yes; print “a” then add 1 to “a”
“a”=3, is “a” less than 10? Yes; print “a” then add 1 to “a”
“a”=4, is “a” less than 10? Yes; print “a” then add 1 to “a”
“a”=5, is “a” less than 10? Yes; print “a” then add 1 to “a”
“a”=6, is “a” less than 10? Yes; print “a” then add 1 to “a”
“a”=7, is “a” less than 10? Yes; print “a” then add 1 to “a”
“a”=8, is “a” less than 10? Yes; print “a” then add 1 to “a”
“a”=9, is “a” less than 10? Yes; print “a” then add 1 to “a”
“a”=10, is “a” less than 10? No; stop the loop
This loop ran only 10 times because the condition was not met when it ran the 11th time.
These are shorthand notation of an arithmetic equation. They can be easily substituted with the
arithmetic equation but they look nicer in the code:
Declare a variable of the type int called timer with the value 100. This will be used as a timer later on to
create the Cylon effect.
void setup() {
pinMode(thisPin, OUTPUT);
This is a very interesting use of the for loop. We use the for loop to set the pin modes on the Arduino to
OUTPUT. This only works if the pins we are using are in a sequence we use the pins 2 to 7. This for
loop replaced the following lines:
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
void loop() {
digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
The object of this exercise is to turn on the LEDs one at a time from left to right then from right to left
crating a Cylon effect or the Knight rider effect.
We do this effect by creating two for loops once to move the light from right to left and another one to
move the lights in the opposite direction.
digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
In this for loop we start at pin number 2 turning on the LED one at a time until it reaches the 7th pin,
the LEDs stay on for a delay of “timer” which is 100 milliseconds, this gives us the illusion that the light
traveled from right to left.
digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
In the second for loop the light starts at pin 7 and stops at pin2, again turning off after a delay of
“timer”. This completes the effects of the red light moving in both directions.
In this example I will demonstrate how the While loop works, this time I will not use a sketch from the
Arduino IDE because I didn’t find a sketch simple enough in the IDE examples.
The sketch:
void setup() {
int i = 0; //declare an integer I and set it to the number 0
while (i < 10) { //Beginning of the while loop check for the condition if i is less than 10
The While loop will keep on working until the statement within the brackets is no longer true.
Syntax:
while(condition){
statement1
statement2
etc..
void setup() {
int i = 0;
Serial.begin(9600);
Serial.print("i = ");
Serial.println(i);
i++;
This is the first time we see a loop within the void setup. This means when this while loop finish
running the program will stop since there is nothing in the void loop.
int i = 0: We start by declaring a variable of the type int and set its value to 0.
while (i < 10) {Serial.print("i = "); Serial.println(i); i++ }: this is the while loop, it tests to see if “i”
is less than 0, if the condition is true it prints i=it’s current value then it increments “i”. the loop and the
program stops when “i” reaches 10 because the void loop is empty.
CONTINUATION