Simple Timer Light | MindPlus Coding Kit for Arduino MindPlus Tutorial E04
Simple Timer Light
What does a timer light do? Imagine this: it’s getting late and you are going to bed. However, the lamp is some distance away from your bed. After turning it off, you have to feel your way in utter darkness to your bed. Students who sleep on the top beds in the dormitory have the same trouble.
In the previous project, we learned how to use buttons to control LED lights. Next, we will further apply the conditional statement structure to learn how to make simple timer lights.
Task Navigation
1. What is a conditional statement structure?
2. Make a timer LED light.
Hands-On
Hardware connection
Refer to “Magic Button Switches” for circuit connection.
Programming
1. Write the program
2. Reference program
3. Program effect
After the button press, the LED turns on for a few seconds before going off.
4. Program analysis
Just like using button to control LED in the previous chapter, it also deals with the relationship of "button-UNO master control-LED". The difference here is that it adds a delay before turning off and simplifies the switch function.
The logical relationship analysis is as follows:
Here is a simple application of the conditional structure, so how does it go?
If the current status meets the requirements of the conditional, the program in the conditional module (inside the green box) will be executed. If the condition is not met, the conditional module is skipped and the program inside the blue box is executed.
Further reading
The other two forms of conditional structure can be referred to the code pane on the right side of Mind+.
1. The second form is: if-else
if (expression)
Statement 1;
else
Statement 2;
The semantics are: if the value of the expression is true, then execute statement 1, otherwise execute statement 2. The execution process can be expressed as the following figure.
2. The third form is the if-else if.
The first two forms of if statements are generally used when there are two branches. When there are multiple branches, an if-else-if statement can be used, and its general form is:
if (expression 1)
statement 1;
else if (expression 2)
statement 2;
else if (expression 3)
statement 3;
…
else if (expression m)
statement m;
else
statement n;
Its semantics are: check the values of the expressions in order; if a value is True, execute the corresponding statement. Then it exits the if statement scope and continue to execute. If all expressions are False, then the statement n is executed. After that, continue with the subsequent program. The execution process of the if-else-if statement is shown in the following figure.
Further Exercise
Now that you have mastered timer light, try making a small timer microphone.