Outline
Let's make an alarm clock of which the countdown time can be set, and it can ring at the end of the countdown.
Suitable kits: mCookie302
Module Bill
Module
Other Equipment
- USB cable*1
- Sensor cable*4
- Sensor shell*1
- Lego or other fixation structure
Module Setup
Setup
- Assemble the modules and sensors:
- Stick the red core module, battery base and green extension module together;
- Connect the buzzer to pin 8/9 of Hub;
- Connect the 3 touch buttons to pin 6/7, 4/5, and 2/3 of Hub respectively;
- Insert one end of USB cable into the battery base and connect the other to computer;
Programming
Required Program Blocks
Programming Thought
Choose 3 touch buttons, one to control the set, start and end of the countdown, another to set the time addition of the countdown, and another to set the subtraction of the countdown.
Start to Program
Declare as. After the touch button 6 is released, repeat executing the time set program.
Declare a variable named time, to save the second number of the countdown.
When the touch button 6 is released, repeat executing the internal program(detect the button time + and -).
If touch button 4 is pressed(time reduces by 1s, wait for the button to be released, and send the changed time to computer).
If touch button 2 is pressed(time increases by 1s, wait for the button to be released, and send the changed time to computer).
Start the countdown. After then end of time, the buzzer rings.
Send "start" to computer, representing the countdown starts. Make the second number saved in variable time multiply 1000(ms), and delay, then send "alarm!" to computer, representing the time is over.
Wait for the touch button 6 to be pressed again, then turn the buzzer off.
Before the touch button 6 is pressed, the buzzer will keep buzzing all the time. Wait for the touch button 6 to be released, to prevent mistaken judgment.
Test the result
After upload, before the touch button 6 is pressed, you can set the time, pressing touch button 4 to decrease time, pressing button 2 to increase time. Open the serial monitor, and you can see the time. After that, press the touch button 6, and the countdown will start. At the end of the countdown, the buzzer will ring. Press button 6 again, then the buzzer will go off.
Q: Although we have implemented the countdown, we can't see the process of it, how can we see the subtraction of time?
A: Each time of the time subtracting 1s, print it out, so that we can see the time on the serial monitor.
Declare a variable time. Release the touch button 6, and set the countdown time.
Press the touch button 4 , and the time adds 1; press button 2, and the time subtracts 1.
Display the countdown process, and the buzzer will ring at the end of that.
Use every time program block. Because it is countdown, the execution time is from time to 1, and the step is -1. During each execution, reduce the variable time by 1, and send the second number saved in time to computer, and then delay 1s.
Technical Specification
Related Cases
FAQ
|