Many robotic tasks involve repeating an action over and over again. Next, we’ll look at two options for repeating code: the for loop and while loop. The for loop is commonly used for repeating a block of code a certain number of times. The while loop is used to keep repeating a block of code as long as a condition is true.
A for loop is typically used to make the statements in a code block repeat a certain number of times. For example, your BOE Shield-Bot will use five different values to make a sensor detect distance, so it needs to repeat a certain code block five times. For this task, we use a for loop. Here is an example that uses a for loop to count from 1 to 10 and display the values in the Serial Monitor.
// Robotics with the BOE Shield - CountToTen void setup() { Serial.begin(9600); for(int i = 1; i <= 10; i++) { Serial.println(i); delay(500); } Serial.println("All done!"); } void loop() { // Empty, no repeating code. }
The figure below shows the for loop from the last example sketch, CountTenTimes. It labels the three elements in the for loop’s parentheses that control how it counts.
The first time though the loop, the value of i starts at 1. So, Serial.println(i) displays the value 1 in the Serial Monitor. The next time through the loop, i++ has made the value of i increase by 1. After a delay (so you can watch the individual values appear in the Serial Monitor), the for statement checks to make sure the condition i <= 10 is still true. Since i now stores 2, it is true since 2 is less than 10, so it allows the code block to repeat again. This keeps repeating, but when i gets to 11, it does not execute the code block because it’s not true according to the i <= 10 condition.
As mentioned earlier, i++ uses the ++ increment operator to add 1 to the i variable each time through the for loop. There are also compound operators for decrement --, and compound arithmetic operators like +=, -=, *=, and /=. For example, the += operator can be used to write i = i + 1000 like this: i+=1000.
for(int i = 5000; i <= 15000; i+=1000)
In later chapters, you’ll use a while loop to keep repeating things while a sensor returns a certain value. We don’t have any sensors connected right now, so let’s just try counting to ten with a while loop:
int i = 0; while(i < 10) { i = i + 1; Serial.println(i); delay(500); }
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?