Make a stopwatch which can time accurately, with one touch button controlling to start timing, and another controlling to stop timing, and display the time on OLED at the same time.
Suitable kits: mCookie302
- USB cable*1
- Sensor cable*2
- Sensor shell
- Lego or other fixation structure
- Assemble modules and sensors:
- Stick the red core module, battery base, OLED and green extension module together;
- Connect the touch buttons to pin 6/7 and 4/5 of Hub;
- Insert one end of USB cable into the battery base, and connect the other to computer;
Required Program Blocks
When the start is pressed, it begins to time. Not until the stop button is pressed are the calculating time and display programs executed repeatedly. When the stop button is pressed, stop timing, the current time is kept on the screen.
Start to Program
Declare a variable, wait for the touch button to be pressed, and then start to time.
Use variable to save the second number to display（in form of float）.
Use variable s_timer to save the millisecond number read from the timer (in form of integer).
Wait for the "start" button to be pressed.
Timer1 starts to time.
When the touch button 4 is free, repeat executing.
Repeat executing before the "stop" button is pressed.
Save the value of timer1 into s_timer.
Divide s_timer by10（remove the last number, namely the 0.001）.
Save s_timer into s.
Divide s by 100（move the tenth and the percentile to the right of the decimal point）.
Display it in form of "s: second" on OLED.
Test the result
After upload the program, press the touch button of pin 6 to start timing, and then you can see the time changing on OLED; press the touch button of pin 4, then the timing ends, the time won't change any longer; press the touch button of pin 6 again, it will start another time.
Q: How to reset the data in the stopwatch?
A. After the end of timing, assign 0 to the variable to store time.
Add these behind all programs.
Wait for the "reset" button to be pressed (after the end of timing, the start starts to serve as reset button).
Wait for the "reset" button to be released.
Set the value of s to 0.
Display the second number after reset on OLED.