Jukebox — Mini (Python)
This example is written twice, once for each firmware. Pick the tab that matches the firmware of your Mini: the tab you pick is remembered on the other examples.
Firmware 3.0 renamed the Python names a program writes, and kept none of the old ones: a Mini on 2.x does not know the 3.0 names, and a Mini on 3.0 does not know the 2.x ones. The name map lists every change. If you open the example from inside MoBot, it picks the version for the firmware of your Mini. If you download a file from this page, use the one in the tab for your firmware.
The Mini knows sixteen songs. The Play sound block offers seven of them. This program plays seven of the other nine, the ones only Python can reach, and then four of the seven in the block’s menu.
This is the version for a robot on firmware 3.0. For a Mini that still runs firmware 2.x, MoBot opens the 2.x version of this example instead.
Project for firmware 3.0: 03-mini-jukebox.firmware-3.mobot. Open it in MoBot.
⚠ A fresh Mini ignores your program
Section titled “⚠ A fresh Mini ignores your program”The Mini starts in Manual driving and only runs your main.py once it is set
to Programmable. Press Save and answer Switch to Programmable, or tap
the Programmable tile in the MoBot app. Full explanation in 01-mini-drive.
What you need
Section titled “What you need”- A MoBot Smart Car Mini. Nothing to plug in.
- A USB cable and the serial monitor.
- Ears.
How to run it
Section titled “How to run it”Open 03-mini-jukebox.firmware-3.mobot, press Save, and listen. It runs for less than a minute.
What you should see and hear
Section titled “What you should see and hear”-
The monitor prints all sixteen song names the robot really knows.
-
Then seven tunes you cannot get from the blocks, each with its own colour on the RGB light and LED 1 blinking in time:
song sounds like klaxona two-tone honk, orange light ambulancenee-naw, red light birthdayhappy birthday, pink light pianoa rising chord, white light animalsa short nursery tune, green light jinglejingle bells, blue light single_beepone short beep, yellow light -
Then four tunes you do know from the block menu:
start,success,error,siren. The block callssirenPolice. -
Last, a short tune written right in the program.
Songs only Python can play
Section titled “Songs only Python can play”The Play sound block on the Mini has seven songs in its menu: Start,
Finish, Success, Error, Notification, Demo and Police.
The Mini knows nine more: startup, klaxon, ambulance, mario_end,
piano, birthday, animals, jingle and single_beep. In Python you can
ask for any of them by name:
from indicators import SONGSrobot.buzzer.play_song(SONGS["klaxon"])A song plays once. play_song(song, loop=True) repeats it until
robot.buzzer.stop().
toggle()
Section titled “toggle()”To make an LED flash you do not have to remember whether it is on or off; it can flip itself:
robot.led1.toggle()From firmware 3.0 the big Smart Car’s LEDs have toggle() too. There is no
block for it on either robot.
The program
Section titled “The program”This is a reading copy of the program in the project: editing it changes nothing, so open the project and edit it there.
# main.py — Jukebox (Python) (mini)## A reading copy of one file inside 03-mini-jukebox.firmware-3.mobot.# Editing this copy changes nothing: open the project and edit it there.# This is the entry point — the board runs it as main.py.# Written for firmware 3.0; the 2.x version is 03-mini-jukebox.main.py.
"""Jukebox - MoBot Smart Car Mini.
The Mini knows sixteen songs. The Play sound block offers seven of them.
This program plays seven of the other nine - klaxon, ambulance, birthday,piano, animals, jingle, single_beep - with the lights doing something differentfor each one. It uses toggle() to flash an LED: on becomes off, off becomes on.
Then it plays four of the seven from the block menu: start, success, error andsiren (the block calls it Police)."""
import timefrom indicators import SONGS
buzzer = robot.buzzerlight = robot.rgb_led
# The Mini knows these; the Play sound block does not offer them.HIDDEN = ("klaxon", "ambulance", "birthday", "piano", "animals", "jingle", "single_beep")
# What colour to shine while each one plays.COLOURS = { "klaxon": (255, 120, 0), "ambulance": (255, 0, 0), "birthday": (255, 0, 200), "piano": (255, 255, 255), "animals": (0, 200, 60), "jingle": (0, 120, 255), "single_beep": (255, 255, 0),}
def play_and_wait(name, seconds): """Start a song, light up, and wait for it - or for the time to run out.
play_song() does NOT wait for the song to end: it starts it and comes straight back. So we have to ask 'is it still playing?' ourselves. """ song = SONGS.get(name) if song is None: print(" this robot does not know", name) return colour = COLOURS.get(name, (60, 60, 60)) light.set_color(colour[0], colour[1], colour[2]) buzzer.play_song(song)
end = time.ticks_add(time.ticks_ms(), int(seconds * 1000)) while buzzer.is_playing() and time.ticks_diff(end, time.ticks_ms()) > 0: # toggle() flips an LED over: on becomes off, off becomes on. robot.led1.toggle() time.sleep(0.15)
buzzer.stop() robot.led1.off() light.off() time.sleep(0.4)
print("This Mini knows", len(SONGS), "songs:")print(" ", ", ".join(sorted(SONGS.keys())))print("")
print("First, the ones no block can play:")for name in HIDDEN: print(" ", name) play_and_wait(name, 6)
print("")print("And now some you will recognise from the blocks:")for name in ("start", "success", "error", "siren"): print(" ", name) play_and_wait(name, 5)
print("")print("Last one: a tune written right here, not in the robot.")light.set_color(120, 0, 255)buzzer.set_tempo(3)buzzer.play_song("0 G4 2 0;2 G4 2 0;4 A4 4 0;8 G4 4 0;12 C5 4 0;16 B4 8 0")time.sleep(5)buzzer.stop()light.off()print("Jukebox closed.")Try changing it
Section titled “Try changing it”- Change the
COLOURSsobirthdayis your favourite colour. - Play
ambulancewithloop=Trueand drive the Mini around — it is a siren until you callbuzzer.stop(). - Write your own tune at the bottom:
"<beat> <note> <length> 0", joined with semicolons.
The Mini knows sixteen songs. The Play sound block offers seven of them. This program plays seven of the other nine, the ones only Python can reach, and then four of the seven in the block’s menu.
It also uses toggle(), a way of flipping an LED over that only the Mini has.
Project for firmware 2.x: 03-mini-jukebox.mobot. Open it in MoBot.
⚠ A fresh Mini ignores your program
Section titled “⚠ A fresh Mini ignores your program”The Mini starts in Manual driving and only runs your main.py once it is set
to Programmable. Press Save and answer Switch to Programmable, or tap
the Programmable tile in the MoBot app. Full explanation in 01-mini-drive.
What you need
Section titled “What you need”- A MoBot Smart Car Mini. Nothing to plug in.
- A USB cable and the serial monitor.
- Ears.
How to run it
Section titled “How to run it”Open 03-mini-jukebox.mobot, press Save, and listen. It runs for less than a minute.
What you should see and hear
Section titled “What you should see and hear”-
The monitor prints all sixteen song names the robot really knows.
-
Then seven tunes you cannot get from the blocks, each with its own colour on the RGB light and LED 1 blinking in time:
song sounds like klaxona two-tone honk, orange light ambulancenee-naw, red light birthdayhappy birthday, pink light pianoa rising chord, white light animalsa short nursery tune, green light jinglejingle bells, blue light single_beepone short beep, yellow light -
Then four tunes you do know from the block menu:
start,success,error,siren. The block callssirenPolice. -
Last, a short tune written right in the program.
Songs only Python can play
Section titled “Songs only Python can play”The Play sound block on the Mini has seven songs in its menu: Start,
Finish, Success, Error, Notification, Demo and Police.
The Mini knows nine more: startup, klaxon, ambulance, mario_end,
piano, birthday, animals, jingle and single_beep. In Python you can
ask for any of them by name:
from indicators import SONGSmobotMINI.buzzer.play(SONGS["klaxon"], looping=False)toggle() — a Mini-only convenience
Section titled “toggle() — a Mini-only convenience”On the big Smart Car, to make an LED flash you have to remember whether it is on or off. The Mini’s LED can flip itself:
mobotMINI.led1.toggle()There is no block for it on either robot.
The program
Section titled “The program”This is a reading copy of the program in the project: editing it changes nothing, so open the project and edit it there.
# main.py — Jukebox (Python) (mini)## A reading copy of one file inside 03-mini-jukebox.mobot.# Editing this copy changes nothing: open the project and edit it there.# This is the entry point — the board runs it as main.py.
"""Jukebox - MoBot Smart Car Mini.
The Mini knows sixteen songs. The Play sound block offers seven of them.
This program plays seven of the other nine - klaxon, ambulance, birthday, piano,animals, jingle, single_beep - with the lights doing something different foreach one. It also uses toggle(), a way of switching an LED that only the Minihas.
Then it plays four of the seven from the block menu: start, success, error andsiren (the block calls it Police)."""
import timefrom indicators import SONGS
buzzer = mobotMINI.buzzerrgb = mobotMINI.rgb
# The Mini knows these; the Play sound block does not offer them.HIDDEN = ("klaxon", "ambulance", "birthday", "piano", "animals", "jingle", "single_beep")
# What colour to shine while each one plays.COLOURS = { "klaxon": (255, 120, 0), "ambulance": (255, 0, 0), "birthday": (255, 0, 200), "piano": (255, 255, 255), "animals": (0, 200, 60), "jingle": (0, 120, 255), "single_beep": (255, 255, 0),}
def play_and_wait(name, seconds): """Start a song, light up, and wait for it - or for the time to run out.
play() does NOT wait for the song to end: it starts it and comes straight back. So we have to ask 'is it still playing?' ourselves. """ song = SONGS.get(name) if song is None: print(" this robot does not know", name) return colour = COLOURS.get(name, (60, 60, 60)) rgb.set_color(colour[0], colour[1], colour[2]) buzzer.play(song, looping=False)
end = time.ticks_add(time.ticks_ms(), int(seconds * 1000)) while buzzer.is_playing() and time.ticks_diff(end, time.ticks_ms()) > 0: # toggle() flips an LED over: on becomes off, off becomes on. # The Smart Car does not have it; you have to remember which state # it was in and set the opposite. The Mini just knows. mobotMINI.led1.toggle() time.sleep(0.15)
buzzer.stop() mobotMINI.led1.off() rgb.off() time.sleep(0.4)
print("This Mini knows", len(SONGS), "songs:")print(" ", ", ".join(sorted(SONGS.keys())))print("")
print("First, the ones no block can play:")for name in HIDDEN: print(" ", name) play_and_wait(name, 6)
print("")print("And now some you will recognise from the blocks:")for name in ("start", "success", "error", "siren"): print(" ", name) play_and_wait(name, 5)
print("")print("Last one: a tune written right here, not in the robot.")rgb.set_color(120, 0, 255)buzzer.set_tempo(3)buzzer.play("0 G4 2 0;2 G4 2 0;4 A4 4 0;8 G4 4 0;12 C5 4 0;16 B4 8 0", looping=False)time.sleep(5)buzzer.stop()rgb.off()print("Jukebox closed.")Try changing it
Section titled “Try changing it”- Change the
COLOURSsobirthdayis your favourite colour. - Play
ambulancewithlooping=Trueand drive the Mini around — it is a siren until you callbuzzer.stop(). - Write your own tune at the bottom:
"<beat> <note> <length> 0", joined with semicolons.