LESSON 07 – IF
STATEMENTS IF STATEMENTS An
IF Statement is a programming structure that allows our programs to make
decisions based on a condition.
Essentially, it allows a program do run some statements when a
condition is true. It can also allow for
other statements to be ran when the condition is not true. IF STRUCTURE In
general, the simplest IF Statement looks like this: if
(condition) { //statement block } A
statement block is simply one or more statements. These statements are only executed when the
condition is true. If the condition is
false, the Arduino skips to the statement following the closing bracket of
the IF structure. These
statements should be indented inside the brackets. CONDITIONS A
condition is a piece of code that can be evaluated to be either TRUE or
FALSE. CONDITIONAL OPERATORS The
conditional operators are
The
above operators allow for a Boolean expression to be evaluated to TRUE or
FALSE. EXAMPLE Consider
the code below. if (voltage > 4) {
//statement block }
If
the variable called voltage is in fact greater than 4, then the condition is
true and the statement block will be executed. If the condition is false, the statement
block is skipped. ELSE Sometimes,
we want to run some statements when a condition is true but other statements
when it is false. We can do that by
adding an else section to the code. if (condition) {
//statement block used if condition is true } else {
//statement block used if condition is false } Note:
No matter the condition’s value, Arduino will only execute one of the
statement blocks.
ELSE IF Sometimes,
it is useful to create a structure that will have many possible situations
that all have their own statement block.
We can do this using as many ELSE IF sections as we want. Here
is a general example with two else if sections. if (condition1) {
//statement block used if condition1 is true } else if (condition2) { //statement block used if condition2
is true } else if (condition3) { //statement block used if condition3
is true } else {
//statement block used if no condition is true }
COMPOUND BOOLEAN
OPERATORS We
can combine several conditions using either AND or OR. The symbol for AND is && and the
symbol for OR is ||. In
general, using AND looks like this: if(condition1
&& condition2) { //statements are executed if both
conditions are true } In
general, using OR looks like this: if(condition1
|| condition2) { //statements are executed if one of the
conditions //is true }
|
TRY THIS… PRACTICE 07 Click here
to go to the program analysis page. PRACTICE 08 You will create a program
that will continuously output the numbers from 1 to 100 over and over again. HINTS: The number will be a
global variable. The number is displayed
and incremented inside the loop() function.
An if statement is used to check if the number has reached 100. If it has, it is reset to 1. PRACTICE 09 Similar to Practice
program #6. You will connect an LED
and resitor to any digital pin that can do Pulse Width Modulation. You will create a global
variable called voltageNumber that keeps track of the voltage number which
ranges from 0 to 255. Inside the loop()
function, you will increase the voltageNumber by one and analogWrite() the
new voltageNumber to the pin with the LED.
So the LED should gradually increase in brightness. In order to slowdown the
increases, you will put a short delay of 10 milliseconds. Also, you will place your
commands above inside of an if statement to make sure that voltageNumber
never exceeds 255. ALTERATIONS Using the idea from above,
try to make the LED fade in and then fade out continuously. Hint: This is done in the Example calle Fade. |