Hardware Reference
In-Depth Information
A more elegant way of writing this is through the switch / case statement.
Just like the if statement, switch / case controls the l ow of the program by
allowing different sections to be executed depending on a condition. A switch
statement checks the value of a variable, and executes different case statements
depending on the value.
switch(button)
{
case 1:
turn_on_lights();
break;
case 2:
if (blinds_up == false)
{
raise_blinds();
blinds_up = true;
}
break;
case 3:
Notice the break instruction; it is typically used at the end of each case and
tells the compiler to stop running instructions. Without the break statement, the
Arduino would continue to execute the case instructions, even when another
case should be used. This can actually be used to your advantage. Imagine that
in this application, pushing buttons 4 , 6 , and 8 actually do the same thing. You
can write the following:
switch(button)
{
case 4:
case 6:
case 8:
//code to be run
break;
}
while Loop
The while loop is the most basic loop in C; it will loop over the same code while
a condition is satisi ed. As long as the condition is true , while continues to
execute the same code, checking the condition at the end of the loop.
while (button == false)
{
button = check_status(pin4);
}
 
Search WWH ::




Custom Search