Skip to content

Search the documentation

Kit
Firmware

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 blocks of this example, from top to bottom.
Click the picture to view it larger.Open full sizeThe same program written as Python is further down this page, in The Python code.
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."
  • A Smart Car. The buzzer is on the board.
  • A USB cable.
  • Ears. This is the one example you cannot test by looking.
  1. Open 05-sound-machine.mobot, plug in, press Send to Device.
  2. Sit still and listen for about 25 seconds.
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.

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.

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 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.

05-sound-machine.firmware-3.main.py
# 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 Pin
import time
from 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.')
  • Open a Play block’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 Stop block one line earlier, so it lands on the siren sooner.
  • Delete the wait 10 under Tom and Jerry. The tune is cut off almost immediately — proof that the block really does not block.