Let’s move on to another important concept in programming: conditional statements – also known as if…else… With these statements, you can create branches in your sketch for specific events.
We’ll use the LED that’s already on your breadboard. You’ll also use the counter again that increases a number by 1 every second and displays it in the Serial Monitor.
This time, the LED should only light up when the current number is divisible by 3. For all other numbers, it remains off.
For this, conditional statements are ideal. But let’s take it one step at a time. First, declare some variables at the beginning of your sketch:
int ledPin = 9;
int number = 1;
int remainder;
___STEADY_PAYWALL___
You already know the first two from previous lessons. The third variable remainder
is needed to test whether the current number is divisible by 3. You set the value of remainder
to 1 initially.
In the code above, you see another shorthand form. Instead of writing:
int remainder = 1;
Just the name of the variable and a final semicolon ; is sufficient.
This also works with Strings:
String text; // creates an empty String just like:
String text = "";
The Setup Function
The void setup()
function also contains code you already know. Here you start the Serial Monitor and define the pinMode of ledPin:
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
The Loop Function
In the loop, you first output the current value of the variable number
in the Serial Monitor – during the first pass, that’s 1.
Serial.println(number);
Then follows the test whether this number is divisible by 3. You do this with the so-called modulo operator.
remainder = number % 3;
You write the modulo with a percent sign %. It calculates the remainder when one integer is divided by another. If you divide 3 by 3 – the remainder is 0. If you divide 4 by 3, the remainder is 1. You store this remainder in the variable of the same name.
If the remainder is 0…then…
Now comes the conditional statement. Whenever the variable remainder
contains the value 0 (meaning the current number is divisible by 3), the LED should light up. If not, then it should remain off.
It works like this:
if (remainder == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
Expressed in pseudocode, this statement would look like:
if remainder is 0, then
turn the LED on;
if not, then
turn it off;
In the first statement if
, you check if something is true – in our case, whether the value 0 is stored in the variable remainder
(and the current number is thus divisible by 3). If that’s actually true, the code between the curly braces { } is executed. This is the first possible branch in your code.
Afterwards, you use else
to provide another branch for all other cases, which is contained in the curly braces that follow. In these cases, the LED is turned off.
Comparison Operators
Whenever you check whether something is true or false, you need the so-called comparison operators.
In the code above, you made this check with a double equals sign ==:
remainder == 0
If the value 0 was stored in the variable remainder
, this check returned true and the code in the curly braces was executed.
In all other cases, the check resulted in false and the code in the curly braces after else was executed.
There are several other comparison operators that you probably already know from math class:
== is something equal?
!= is something not equal?
< is something less than?
<= is something less than or equal to?
> is something greater than?
>= is something greater than or equal to?
With these, you can check, for example, whether a number is less than or greater than another. Something special is !=, because with this, something becomes true when one side does not correspond to the other:
3 != 4 // is true
Back to the sketch. There are still two lines missing for the counter, which you already know.
number++;
delay(1000);
In the first line, you increase the value in the variable number
by 1, and in the second line, you wait one second before you start the next iteration of the loop function. Now upload the sketch from the downloads to your Arduino and try it out.
In the next lesson, another component is added: a potentiometer, with which you can dim the LED.