pROGRAMMING: whaT IF...
Programs get a lot more interesting when you let them make decisions. The simplest way to make a decision is to use the if statement. For example, you might read a sensor that measures the temperature and then do different things depending on the result:
if(temperature < 40) digitalWrite(blueLED,HIGH);
if(temperature > 90) digitalWrite(redLED,HIGH);
If you only want to do one thing, you can put the if statement and the command on the same line. If you want to run more than one line, you need to tell the Arduino to run a block of statements. The beginning of the block is marked with a { and the end with a }.
if(temperature < 40){
digitalWrite(blueLED,HIGH);
Serial.println(“It’s cold!!”);
}
Notice that the part of the program between { and } is indented to the right. It’s customary to hit the Tab key after each { and to remove the indent before each }. The Arduino language doesn’t actually require it, but it makes it much easier to recognize the block of code that goes with the if statement. The Arduino IDE also has a built-in function that formats your program with the conventional indentations between brackets (Tool > Auto Format).
The part of the if statement in parentheses is called a conditional statement. Arduino tests to see whether the conditional statement is true and, if it is, runs the lines in the block. Otherwise it skips the block entirely. Here are the types of tests that Arduino understands:
· a == b does a equal b?
· a < b is a less than b?
· a <= b is a less than or equal to b?
· a > b is a greater than b?
· a >=b is a greater than or equal to b?
· a !=b is a not equal to b?
Condition statements can be combined using the AND operator (&&) and the OR operator (||):
· a == b && b < c is true only if a equals b AND b is less than c
· a==b || b < c is true if a equals b OR b is less than c
if(temperature < 40) digitalWrite(blueLED,HIGH);
if(temperature > 90) digitalWrite(redLED,HIGH);
If you only want to do one thing, you can put the if statement and the command on the same line. If you want to run more than one line, you need to tell the Arduino to run a block of statements. The beginning of the block is marked with a { and the end with a }.
if(temperature < 40){
digitalWrite(blueLED,HIGH);
Serial.println(“It’s cold!!”);
}
Notice that the part of the program between { and } is indented to the right. It’s customary to hit the Tab key after each { and to remove the indent before each }. The Arduino language doesn’t actually require it, but it makes it much easier to recognize the block of code that goes with the if statement. The Arduino IDE also has a built-in function that formats your program with the conventional indentations between brackets (Tool > Auto Format).
The part of the if statement in parentheses is called a conditional statement. Arduino tests to see whether the conditional statement is true and, if it is, runs the lines in the block. Otherwise it skips the block entirely. Here are the types of tests that Arduino understands:
· a == b does a equal b?
· a < b is a less than b?
· a <= b is a less than or equal to b?
· a > b is a greater than b?
· a >=b is a greater than or equal to b?
· a !=b is a not equal to b?
Condition statements can be combined using the AND operator (&&) and the OR operator (||):
· a == b && b < c is true only if a equals b AND b is less than c
· a==b || b < c is true if a equals b OR b is less than c