Description
By using the if…else instruction, the Arduino will check whether a certain condition is met (the result is ‘true’), and then execute the program under the if statement. While the result is not ‘true’, the program under the else will be run.
Syntax
if (condition1) {
// if condition1 is true, run this code.
}
else {
// if condition1 is false, run this code instead.
}
Example Code
if (button == HIGH) //if the button is pressed
{
digitalWrite(13, LOW); //switch off LED
}else //if the button is not pressed
{
digitalWrite(13, HIGH); //switch on LED
}