Improve the game machine, implementing the function of storing the top record.
- USB cable*1
- Sensor cable*4
- Sensor shell
- Building blocks or other fixation structures
- Assemble modules and sensor together:
- Stick the red core module, battery base, OLED and green Hub together;
- Connect the Buzzer to pin 8/9 of Hub;
- Connect the Potentiometer to pin A0 of Hub;
- Connect the ColorLED to pin 6/7 of Hub;
- Connect the Joystick to pin A2 of Hub;
- Plug one end of the USB cable into the battery base and connect the other to the computer.
Add the code of storing the top record and comparison, and other programming thought is same with that in the last course. At the beginning of the game, the top record is displayed. At the end of the game, judge whether the top record is broken, if it is, it will be updated.
Required Program Blocks
Start to Program
1. Declare variables for storing data
Variable top is for storing the top record
'2. Function setup
Change the setup function into: save the value in 0 of the EEPROM address into top. Display top on OLED.
3. Function win
Change the content of function win into:
If the score reaches 15, compare the duration time to the top record, if time is less than top (the record is broken), on the OLED will displayed “you are the best” and the score.
Save time into the address 0 of EEPROM (update the record) and play the music for celebration. Otherwise only display the score and the green LED lights 1s.
4.Funcation start is unchanged
5.Function display is unchanged
6.Fucntion BINGO is unchanged
7.Function time_out is unchanged
Test the result
After upload, on OLED will display the top record, press the Joystick then the game begins. The random number will be displayed on the first line and it will change once each 3s. “123456” will be displayed on the second line, and SCORE: 0 will be displayed on the third line. Turn the potentiometer to move the “123456”, after a period of alignment can it score, the score displayed on the third line will change. When the score reaches 15, if the duration is shorter (breaking the record), on OLED will “you are the best” and the score be displayed, and it will play the music for celebration, otherwise, only display the score and the green LED lights 1s. Wait for the Joystick to be pressed to restart the game.
Q: In the address where we store the top record of EEPROM maybe there data stored a long time ago, so we need the function of clearing the top record directly for the game.
A: We can write a larger time value in it to replace the smallest one.
Based on the programming thought of Bingo04, add the program of clearing the top record
Add function clear
If the Joystick is turned to left, write 99 (99s, a very slow speed. If your write o, it won't be beaten forever), display “clear top” on the OLED and blue LED lights 1s.
Execute the functions