Make a smart piggy bank, which can record the coin number and display it on OLED, and emit "ding" sound every time throw a coin into it.
Suitable kit: 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 line finder to pin A0/A1 of Hub;
- Connect the buzzer to pin 6/7 of Hub;
- Insert one end of USB cable into the battery base, and connect the other to computer;
Required Program Blocks
Judge whether the distance value of the line finder is less than 180. If it is, there was coins thrown in (according to the material of the coins, the judgement value can be adjusted appropriately), the buzzer will produce a ding sound, the coin number will increase 1, and it will be displayed on the screen.Finally wait for the distance of the line finder to be larger than 220.
Start to Program
Judge if a coin has passed
Declare a variable coin to record the coin number. If the distance value detected by the line finder is less than 180, there are coins in front of the sensor.
The buzzer rings, the number counts, and it is displayed on the screen.
The buzzer produce a "ding" sound, variable coin adds 1. Pay attention to adjusting the direction of the screen according to the installation direction. Display the value of coin on the OLED. Wait for the distance value of the line finder to be larger than 220 (the coin fell into the bank.).
Test the result
After upload, approach the line finder with a coin, and you can hear a "ding" sound produced by the buzzer, and the number on OLED counts once to the current coin number.
Q: If I take all coins in the bank away, how to reset the record?
A: Add a touch button. Judge whether the touch button has been pressed with "if-do" program block. If it has, reset the variable coin.
If the "reset" button was pressed, the reset program will be executed. The buzzer will produce "ding" sound, and the value of variable coin will change into 0. Display the coin number again.