Arduino Conditionals
Conditionals
In programming, sometimes we want our lines to execute sometimes, but not all
the time. Specifically, we might want things to only happen based on a condition,
like a variable being a certain value, a button being pushed, a motion being
recognized by a sensor, etc.
Booleans
A boolean is a data type that can either be true or false. We can create variables
that store booleans like so:
bool var1 = true;
bool var2 = false;
If statements
If statements are statements that will (or will not) run
based on a boolean condition. You will use these if you
need your program to make a decision to run or not run
something.
if (true){ ← This will always run
//some lines of code
}
if (false){ ← This will never run
//some lines of code
}
In the example, lines 5-7 will happen and make pin 3
output for a second, and lines 10-12 will not happen
Comparison Operators
It is rather moot to create “if statements” that either always run or never run, so we
should use comparison operators instead.
Comparison operators are like math operators (+, -, *, etc) but instead of giving a number
as an answer, they give back a boolean
The comparison operators are:
== (equals), != (does not equal), <, > (bigger/less than), >=, <= (bigger/less or equal to)
For example:
5 > 7 → false “banana” == “banana” → true “banana” != “banana” → false
Practice
int x = 50;
char y = “hello”;
5>6 y != “hello” 0 == x
x <= 50 x == y x == 50
x < 50 45 < x x == “50”
y == “hello” 55 >= x
Comparison Operators (2)
Along with variables, we can make
things like this:
This program waits for 6 seconds, then
starts blinking
Note that inside the brackets on line 10,
we no longer have if(true) or if(false),
but a statement using comparison
operators (in this case, >=) that can
become true or false
else if
After putting down an if statement, you
have the option to use an else if
statement. Your program will only
“check” an else if statement if the above
if statement failed to run.
This program waits for 4 seconds, blinks
pin 4 twice, then blinks pin 3
You can also follow up an else if with
more else ifs.
else
After an “if” or “else if” statement, we
can follow with an else statement.
An else statement has no conditions
and will run if the above if and else if
statements have all failed.
This program blinks both pin 3 and 4
three times, then pin 4 two times, then
pin 3 repeatedly afterward.
digitalRead
digitalRead(pin) looks at the input from pin and returns HIGH(1) or LOW(0)
accordingly.
Note: The pin should be set to input mode.
Example: Make an LED that turned on when a button is
pressed, and off when that button is pressed again.
With this example, we cannot follow the same structure as before, since the LED
would turn off shortly after the button is released the first time.
Consider using a variable to keep track of what “state” the LED should be in.
Example: Make an LED that turned on when a button is
pressed, and off when that button is pressed again.
The last example is imperfect since longer holds of the button continuously flip the
state of the LED, leading to unexpected outcomes.
We need to make sure the LEDState only “flips” when the button is initially
pressed (unpressed to pressed), and not when it is held
Consider using a variable to keep track of the buttonState, and ANOTHER
variable to keep track of the previous buttonState
Finished Button