Skip to content

Search the documentation

Kit
Firmware

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.

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.

  • A MoBot Smart Car Mini. Nothing to plug in.
  • A USB cable and the serial monitor.
  • Ears.

Open 03-mini-jukebox.firmware-3.mobot, press Save, and listen. It runs for less than a minute.

  1. The monitor prints all sixteen song names the robot really knows.

  2. 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
    klaxon a two-tone honk, orange light
    ambulance nee-naw, red light
    birthday happy birthday, pink light
    piano a rising chord, white light
    animals a short nursery tune, green light
    jingle jingle bells, blue light
    single_beep one short beep, yellow light
  3. Then four tunes you do know from the block menu: start, success, error, siren. The block calls siren Police.

  4. Last, a short tune written right in the program.

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 SONGS
robot.buzzer.play_song(SONGS["klaxon"])

A song plays once. play_song(song, loop=True) repeats it until robot.buzzer.stop().

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.

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
# 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 different
for 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 and
siren (the block calls it Police).
"""
import time
from indicators import SONGS
buzzer = robot.buzzer
light = 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.")
  • Change the COLOURS so birthday is your favourite colour.
  • Play ambulance with loop=True and drive the Mini around — it is a siren until you call buzzer.stop().
  • Write your own tune at the bottom: "<beat> <note> <length> 0", joined with semicolons.