by

Arduino Program Flow Control Using Conditional Statements and Loops

There are a vast number of programming languages each with their own syntax but most share common features that allow a programmer to control the flow of processing. This laboratory introduces some of the most important programming structures for flow control using the Arduino programming language, which is based on C/C++;

The IF Statement

An if statement allows a programmer to introduce conditional branches into his or her code. A conditional branch is lines of code that are executed only if some condition is true or not true. For example, perhaps you want to turn on an LED indicator light but only if the temperature in a room exceeds 24 degrees C. An if statement will allow you to accomplish this. The following snippet of code demonstrates the syntax for an if statements in the Arduino programming language.

if (temp > 24) {
// turn on the LED
}

The IF/ELSE Statement

Conditional branches can be made more complex by adding an else statement to an if statement. For example, suppose you want to turn a green LED on if the temperature is less than or equal to 24 degrees C but turn a red LED on if the temperature exceeds 24 degrees C. An if else statement will allow you to accomplish this.

if (temp <= 24) {
// turn on the green LED
}
else {
// turn on the red LED
}

The IF/ELSE IF Statement

Conditionals can be made even more functional by using else if branches. For example, suppose you want to turn on a blue LED if the temperature is less than or equal to 0 degrees C, a green LED if the temperature is between 0 and 24 degrees C, and a red LED otherwise. You could implement code like the following.

if (temp <= 0) {
// turn on the blue LED
}
else if (temp > 0 && temp < 24) {
// turn on the green LED
}
else {
// turn on the red LED
}

FOR Loops

Another commonly encountered problem in programming is a task that must be repeated a certain number of time before the rest of the program can proceed. To accomplish such tasks, a programmer will use a for loop. For example, suppose you have more than one LED to turn on in response to the temperature changing. You could do something like the following where LEDCount is the number of LEDs to turn on.

for (int ii=1; ii<=LEDCount; ii++) {
// turn on LED ii
}

WHILE Loops

Another commonly encountered problem in programming is a task that must be repeated until some condition is met instead of a fixed number of times. A while loop can be used to accomplish such a task. For example, suppose you want an LED to blink repeatedly so long as the temperature is less than 24 degrees C. You could use code like the following.

while (temp<24) {
// turn LED on
// wait some time
// turn LED off
// wait some time
// check temperature and update temp
}

Final Thoughts

The structures presented here (and others not mentioned like the switch case structure) are incredibly powerful methods to get a computer to do what you want it to do. Remember that programming is less about learning specific syntaxes that change from language to language and more about learning to think like a computer. The structures summarized here exist in most programming languages because they allow a programmer to translate the flow of tasks as performed by a human to code that a computer can execute.

Assignments

1) EASY: Write a program that uses a for loop to add together all of the positive integers from one to some maximum value specified by the user and prints the result to the serial terminal

2) HARD: Write a program that uses a while loop and if statements to calculate the stopping time for the series specified by the Collatz conjecture starting from some arbitrary user supplied positive integer.

Leave a Reply

Your email address will not be published. Required fields are marked *