Sound machine — Smart Car
The buzzer knows eleven tunes. This plays four of them, and — more importantly — proves that Stop playing sound really does cut a song off half way, instead of waiting politely for it to finish.
This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Car.
The program
Section titled “The program”Read the program as text
- MoBot Start
- Print to serial
"Sound machine. Turn the volume of the room down!" - wait 1 seconds
- Print to serial
"1. Start" - Set LED 1 ON
- Play Start sound
- wait 4 seconds
- Print to serial
"2. Success" - Play Success sound
- wait 3 seconds
- Print to serial
"3. Police siren" - Play Police sound
- wait 4 seconds
- Print to serial
"Cutting the siren off in the middle - listen!" - Stop playing sound
- Set LED 1 OFF
- wait 2 seconds
- Print to serial
"4. Tom and Jerry - the long one" - Set LED 2 ON
- Play Tom and Jerry sound
- wait 10 seconds
- Stop playing sound
- Set LED 2 OFF
- Print to serial
"Silence. The end."
What you need
Section titled “What you need”- A Smart Car. The buzzer is on the board.
- A USB cable.
- Ears. This is the one example you cannot test by looking.
How to run it
Section titled “How to run it”- Open 05-sound-machine.mobot, plug in, press Send to Device.
- Sit still and listen for about 25 seconds.
What you should see and hear
Section titled “What you should see and hear”| serial line | what you should hear | LED |
|---|---|---|
1. Start |
a short rising fanfare | LED 1 on |
2. Success |
three quick cheerful notes | LED 1 on |
3. Police siren |
a siren, up and down | LED 1 on |
Cutting the siren off in the middle - listen! |
the siren stops instantly, mid-note | LED 1 off |
4. Tom and Jerry - the long one |
a long, fast, familiar tune | LED 2 on |
Silence. The end. |
nothing at all, forever | LED 2 off |
The test passes if you hear four different tunes and if the siren is cut off in the middle rather than finishing tidily. That mid-note chop is the whole point of the Stop block.
Something worth knowing
Section titled “Something worth knowing”Play sound does not wait. The robot starts the tune and moves straight on to
the next block while it is still playing. That is why every tune here is
followed by a wait — the wait is what gives it time to be heard. Take the
waits out and you will hear only the last tune, because each new
Play … sound replaces the one before it.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
mobot_playsound |
starts one of eleven built-in tunes and returns immediately. |
mobot_stopound |
silences the buzzer at once, wherever the tune had got to. |
mobot_ledon |
a light to match the sound, for anyone testing without ears. |
The Python code
Section titled “The Python code”The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.
# Sound machine — robotics (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 05-sound-machine.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.# The firmware 2.x version is 05-sound-machine.main.py.
from machine import Pinimport timefrom indicators import SONGS
print("[MOBOT]", 'Sound machine. Turn the volume of the room down!')time.sleep(1)print("[MOBOT]", '1. Start')robot.led1.set(1)robot.buzzer.play_song(SONGS.get("start"))time.sleep(4)print("[MOBOT]", '2. Success')robot.buzzer.play_song(SONGS.get("success"))time.sleep(3)print("[MOBOT]", '3. Police siren')robot.buzzer.play_song(SONGS.get("siren"))time.sleep(4)print("[MOBOT]", 'Cutting the siren off in the middle - listen!')robot.buzzer.stop()robot.led1.set(0)time.sleep(2)print("[MOBOT]", '4. Tom and Jerry - the long one')robot.led2.set(1)robot.buzzer.play_song(SONGS.get("tom_jerry"))time.sleep(10)robot.buzzer.stop()robot.led2.set(0)print("[MOBOT]", 'Silence. The end.')# Sound machine — robotics## This is what MoBot puts on the board as main.py when you open# 05-sound-machine.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.
from machine import Pinimport timefrom indicators import SONGS
print("[MOBOT]", 'Sound machine. Turn the volume of the room down!')time.sleep(1)print("[MOBOT]", '1. Start')mobotROBOTICS.led1.setState(1)mobotROBOTICS.buzzer.play(SONGS.get("start"), looping=False)time.sleep(4)print("[MOBOT]", '2. Success')mobotROBOTICS.buzzer.play(SONGS.get("success"), looping=False)time.sleep(3)print("[MOBOT]", '3. Police siren')mobotROBOTICS.buzzer.play(SONGS.get("siren"), looping=False)time.sleep(4)print("[MOBOT]", 'Cutting the siren off in the middle - listen!')mobotROBOTICS.buzzer.stop()mobotROBOTICS.led1.setState(0)time.sleep(2)print("[MOBOT]", '4. Tom and Jerry - the long one')mobotROBOTICS.led2.setState(1)mobotROBOTICS.buzzer.play(SONGS.get("tom_jerry"), looping=False)time.sleep(10)mobotROBOTICS.buzzer.stop()mobotROBOTICS.led2.setState(0)print("[MOBOT]", 'Silence. The end.')Try changing it
Section titled “Try changing it”- Open a
Playblock’s menu and pick one of the seven tunes this example does not use: Finish, Error, Notification, Demo, Wiwo, Harry Potter, Fairy Tale. - Move the
Stopblock one line earlier, so it lands on the siren sooner. - Delete the
wait 10under Tom and Jerry. The tune is cut off almost immediately — proof that the block really does not block.