Overview
Optimize the program of position detection, preventing scoring through turning the potentiometer casually.
Module List
Module
OtherEquipment
- USB cable*1
- Sensor cable*2
- Sensor shell
- Building blocks or other fixation structures
Module Setup
Setup
- Assemble modules and sensors together:
- Stick the red core module, battery base, OLED and green Hub together;
- Connect the Buzzer to pin 8/9 of Hub;
- Connect the rotating potentiometer to pin A0 of Hub;
- Plug one end of USB cable into the battery base and connect the other to the computer.
Programming
Programming Thought
After the alignment judgment is matched, if the value of the potentiometer keeps unchanged after 100ms, the alignment is completed and other program keeps unchanged.
Required Program Blocks
Start to Program
1. Declare variables for storing data
Variable a is for storing the random number, a_random is for storing random changed number, b is for storing the movement range of texts, score is for storing score, bingo is for storing the alignment state of numbers, hold is for storing the initial value of the potentiometer at the moment when numbers are aligned.
2. Change the contend of function bingo
If numbers are aligned, save the value of the potentiometer in hold (the one used here is the initial value without conversion, because the initial value is more exact), then delay for 100ms, after that, if the value of the potentiometer is equivalent to hold (during the 100ms, the potentiometer isn’t turned), that can be thought as the numbers are aligned, execute the program that score and buzzer buzzes.
3. Start function and display function needn’t to be changed
4. Execute functions
Finally, just execute every functions.
Test the result
After upload, display a new random number on the first line of OLED, display “1 2 3 4 5 6” on the second line, and display SCORE:0 on the third. Move “1 2 3 4 5 6” with the potentiometer, when the random number is aligned to the corresponding number, and the potentiometer keeps still for 100ms, the score on the third line will increase 1, and the buzzer will buzz.
Q: Now, only after the numbers are aligned can the random number change, how to make it can change once in a while, to increase the difficulty of the game?
A: We can add a timer function, if a certain time is over, reproduce a random number
Time after producing the random number, when 3000ms is over, the buzzer will buzz lowly, and red LED will flash once, set miss to 1 (reproduce a random number).
1. Declare variables for storing data
Add a variable miss for recording the timeout state.
Execute function time_out (timeout judgement).
2. Change function start
If bingo is 1 or miss is 1 (it is timeout), the program to reproduce a random number will be executed, reset variable miss, timer1 begins (restart to time).
3. The content of function time_out.
If timer1 is larger than 3s (it is 3s since the random number was produced, however the numbers hasn’t been aligned), the red LED will flash once and buzzer will buzz lowly once.
Set variable miss to 1 (inform function start is timeout).
4. Display function and bingo function needn’t to be changed.
Technical Specification
Relative Cases
Q&A
|