Datasheet
Your Shield-Bot’s Brain • Chapter 1
Robotics with the BOE Shield-Bot • 35
Want to condense your code a little? You can use the increment operator (++) to increase
the value of
i inside the Serial.println statement. Notice that ++ is on the left side of the
i variable in the example below. When ++ is on the left of a variable, it adds 1 to the value of
i
before the println function executes. If you put ++ to the right, it would add 1
after
println executes, so the display would start at zero.
int i = 0;
while(i < 10)
{
Serial.println(++i);
delay(500);
}
The loop function, which must be in every Arduino sketch, repeats indefinitely. Another
way to make a block of statements repeat indefinitely in a loop is like this:
int i = 0;
while(true)
{
Serial.println(++i);
delay(500);
}
So why does this work? A while loop keeps repeating as long as what is in its parentheses
evaluates as true. The word 'true' is actually a pre-defined constant, so
while(true) is
always true, and will keep the
while loop looping. Can you guess what while(false)
would do?
Try these three different
while loops in place of the for loop in the CountToTen
sketch.
Also try one instance using
Serial.println(++i), and watch what happens in
the Serial Monitor.
Try
while(true) and while(false) also.