Play songs — Mini
The Mini has a buzzer that plays songs. The Play … sound block has seven songs in its menu: Start, Finish, Success, Error, Notification, Demo and Police. This program plays all seven, one after another. Before each song it writes the song’s name in the serial monitor and gives the colour light a colour of its own, so you can follow along by listening, by reading or by looking.
This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Mini.
The program
Section titled “The program”Read the program as text
- MoBot Mini Start
- Print to serial
"Songs on the Mini. Listen for each one." - Set RGB LED brightness to 40 %
- repeat forever
- Print to serial
"Start" - Set RGB LED to R 0 G 255 B 0
- Play Start sound
- wait 4.5 seconds
- Print to serial
"Finish" - Set RGB LED to R 255 G 0 B 0
- Play Finish sound
- wait 4.5 seconds
- Print to serial
"Success" - Set RGB LED to R 255 G 255 B 0
- Play Success sound
- wait 2 seconds
- Print to serial
"Error" - Set RGB LED to R 255 G 120 B 0
- Play Error sound
- wait 2 seconds
- Print to serial
"Notification" - Set RGB LED to R 128 G 0 B 255
- Play Notification sound
- wait 2 seconds
- Print to serial
"Demo" - Set RGB LED to R 255 G 0 B 200
- Play Demo sound
- wait 6 seconds
- Print to serial
"Police" - Set RGB LED to R 0 G 0 B 255
- Play Police sound
- wait 2 seconds
- Print to serial
"Stopping Police now" - Stop playing sound
- Turn RGB LED off
- Print to serial
"--- starting again ---" - wait 2 seconds
- Print to serial
What you need
Section titled “What you need”- A MoBot Mini, charged and switched on.
- A USB cable.
- Nothing to plug in. The buzzer is on the board.
⚠️ First time? Read the “Before you send it” section of Blink and fade.
How to run it
Section titled “How to run it”- Open 07-play-songs.mobot in MoBot.
- Plug the Mini in and choose its port.
- Press Send to Device, and open the serial monitor.
- Listen. Each song is named before it plays.
What you should see and hear
Section titled “What you should see and hear”| serial monitor | the light | what you hear |
|---|---|---|
[MOBOT] Start |
green | seven notes, each higher than the last |
[MOBOT] Finish |
red | the same seven notes coming back down |
[MOBOT] Success |
yellow | three notes, each higher than the last |
[MOBOT] Error |
orange | one short, steady note |
[MOBOT] Notification |
purple | two short beeps |
[MOBOT] Demo |
pink | a longer tune |
[MOBOT] Police |
blue | two notes, one higher and one lower, taking turns like a siren |
[MOBOT] Stopping Police now |
blue, then off | the siren stops partway through |
Then the serial monitor says --- starting again ---, and two seconds later
the whole thing starts again from Start.
One round takes 25 seconds. The program never ends on its own. Press Stop, or send another program, to stop it.
Something worth knowing
Section titled “Something worth knowing”Play sound does not wait. The Mini starts the song and carries straight on
to the next block while the song plays in the background. That is why every
song here is followed by a wait: the first six waits are longer than their
songs, so each song can finish before the next one starts. The last wait, under
Police, is shorter than the song on purpose.
A song stops by itself when it reaches its end. Stop playing sound is for cutting one off early. Police lasts almost five seconds, and the program stops it after two.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
mobot_playsound |
Play … sound starts one of the seven songs in its menu and carries on at once. |
mobot_stopsound |
Stop playing sound cuts the song off wherever it has got to. Here it ends Police early. |
wait |
gives a song time to be heard. |
mobot_print |
writes the name of the song in the serial monitor. |
mobot_rgbsetcolor, mobot_rgbbrightness, mobot_rgboff |
the colour light: a colour for each song, a brightness of 40 %, and off at the end of each round. |
repeat_forever |
plays the round again and again. |
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.
# Play songs — mini (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 07-play-songs.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 07-play-songs.main.py.
from machine import Pinimport timefrom indicators import SONGS
print("[MOBOT]", 'Songs on the Mini. Listen for each one.')robot.rgb_led.set_brightness(40)while True: print("[MOBOT]", 'Start') robot.rgb_led.set_color(0, 255, 0) robot.buzzer.play_song(SONGS.get("start")) time.sleep(4.5) print("[MOBOT]", 'Finish') robot.rgb_led.set_color(255, 0, 0) robot.buzzer.play_song(SONGS.get("stop")) time.sleep(4.5) print("[MOBOT]", 'Success') robot.rgb_led.set_color(255, 255, 0) robot.buzzer.play_song(SONGS.get("success")) time.sleep(2) print("[MOBOT]", 'Error') robot.rgb_led.set_color(255, 120, 0) robot.buzzer.play_song(SONGS.get("error")) time.sleep(2) print("[MOBOT]", 'Notification') robot.rgb_led.set_color(128, 0, 255) robot.buzzer.play_song(SONGS.get("notification")) time.sleep(2) print("[MOBOT]", 'Demo') robot.rgb_led.set_color(255, 0, 200) robot.buzzer.play_song(SONGS.get("demo")) time.sleep(6) print("[MOBOT]", 'Police') robot.rgb_led.set_color(0, 0, 255) robot.buzzer.play_song(SONGS.get("siren")) time.sleep(2) print("[MOBOT]", 'Stopping Police now') robot.buzzer.stop() robot.rgb_led.off() print("[MOBOT]", '--- starting again ---') time.sleep(2)# Play songs — mini## This is what MoBot puts on the board as main.py when you open# 07-play-songs.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]", 'Songs on the Mini. Listen for each one.')mobotMINI.rgb.set_brightness(40)while True: print("[MOBOT]", 'Start') mobotMINI.rgb.set_color(0, 255, 0) mobotMINI.buzzer.play(SONGS.get("start"), looping=False) time.sleep(4.5) print("[MOBOT]", 'Finish') mobotMINI.rgb.set_color(255, 0, 0) mobotMINI.buzzer.play(SONGS.get("stop"), looping=False) time.sleep(4.5) print("[MOBOT]", 'Success') mobotMINI.rgb.set_color(255, 255, 0) mobotMINI.buzzer.play(SONGS.get("success"), looping=False) time.sleep(2) print("[MOBOT]", 'Error') mobotMINI.rgb.set_color(255, 120, 0) mobotMINI.buzzer.play(SONGS.get("error"), looping=False) time.sleep(2) print("[MOBOT]", 'Notification') mobotMINI.rgb.set_color(128, 0, 255) mobotMINI.buzzer.play(SONGS.get("notification"), looping=False) time.sleep(2) print("[MOBOT]", 'Demo') mobotMINI.rgb.set_color(255, 0, 200) mobotMINI.buzzer.play(SONGS.get("demo"), looping=False) time.sleep(6) print("[MOBOT]", 'Police') mobotMINI.rgb.set_color(0, 0, 255) mobotMINI.buzzer.play(SONGS.get("siren"), looping=False) time.sleep(2) print("[MOBOT]", 'Stopping Police now') mobotMINI.buzzer.stop() mobotMINI.rgb.off() print("[MOBOT]", '--- starting again ---') time.sleep(2)Try changing it
Section titled “Try changing it”- Delete the
waitunder Start. Finish starts at once and replaces it, so Start is cut off almost as soon as it begins. - Change the
waitunder Police from 2 to 6. Police now plays to its end and Stop playing sound has nothing left to stop. - Give a song a colour of your own by changing the three numbers of its colour block.
- On firmware 3.0, the Read sound volume (%) block gives how loud the songs play, as a number from 0 to 100. Put it in a Print to serial block to read it.
- There is no block to make songs louder or quieter, or faster or slower. In
Python on firmware 3.0,
set_volumeandset_tempodo it: see the Buzzer reference.