Skip to content

Search the documentation

Kit
Firmware

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

Everything the Mini’s colour light can do. Plain colours, hex colours, a brightness dial, sixteen built-in presets, a wink, a rainbow, and a blink.

Plain colours, the brightness dial and the wink have blocks (and so does turning the light off). The rest are Python only — and one of them, tick(), is the reason the rainbow block does not exist.

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: 02-mini-rainbow.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 — the light is on the board.
  • A USB cable and the serial monitor.
  • A slightly dark room makes parts 2 and 3 much more fun.

Open 02-mini-rainbow.firmware-3.mobot, press Save, and watch the light. It runs for about a minute.

part what the light does
1 red, green, blue, orange, white, then hot pink — each for about a second
2 stays blue while getting dimmer and dimmer, almost out, then bright again
3 steps through sixteen preset colours (number 0 is off), and the monitor prints the red/green/blue numbers of each one
4 a blue light that winks yellow three times and goes back to blue after each wink
5 a moving rainbow for six seconds — a new colour about three times a second
6 nothing at all for four seconds: the light stays dark
7 cyan, blinking on and off

Part 6 is the whole lesson. It asks for exactly the same rainbow as part 5 and gets a light that never even starts.

Setting a colour happens instantly. A rainbow does not — it is a colour, then a wait, then the next colour, forever. The light cannot do the waiting on its own while your program is off doing something else, so it needs you to check in:

light.rainbow()
while True:
light.tick() # <- without this line, nothing ever moves
time.sleep(0.02)

tick() means “a moment has passed, carry on if it is time”. Call it every time round your loop — twenty to fifty times a second is right. This is a real pattern that appears everywhere in programming, and the Mini’s light is a very good first place to meet it.

flash() is a single wink, and it does not need tick(): the program waits while the wink happens (120 ms in part 4), and then the light gets back the colour it had before. blink() is the one that keeps going, and that is why it needs tick().

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 — Rainbow (Python) (mini)
#
# A reading copy of one file inside 02-mini-rainbow.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 02-mini-rainbow.main.py.
"""Rainbow - MoBot Smart Car Mini.
The Mini has one bright colour light on top. Four blocks can use it: set a
colour, flash a colour, set the brightness, turn it off. This program uses
those and everything else too.
The big idea in here is TICK.
Some of the light's tricks - rainbow, and blinking - are not one instruction.
They are something that has to keep happening. The light cannot do that by
itself while your program is busy, so it needs you to look in on it:
light.tick()
every time round your loop. Forget tick() and the rainbow starts... and never
moves. That is not a broken light. That is a light waiting for you.
"""
import time
light = robot.rgb_led
# ------------------------------------------------------------------ 1 -------
# Plain colours. Each of red, green and blue goes from 0 to 255.
print("Part 1: plain colours.")
for name, colour in (
("red", (255, 0, 0)),
("green", (0, 255, 0)),
("blue", (0, 0, 255)),
("orange", (255, 100, 0)),
("white", (255, 255, 255)),
):
print(" ", name)
light.set_color(colour[0], colour[1], colour[2])
time.sleep(0.7)
# The same thing written the way web pages write colours: 0xRRGGBB.
print(" hot pink, written as 0xFF1493")
light.set_hex(0xFF1493)
time.sleep(1)
# ------------------------------------------------------------------ 2 -------
# Brightness is a separate dial, 0 to 100 per cent. The colour stays the same.
print("Part 2: same colour, dimming down and back up.")
light.set_color(0, 0, 255)
for percent in (100, 70, 40, 20, 5, 20, 40, 70, 100):
print(" brightness", percent, "%")
light.set_brightness(percent)
time.sleep(0.4)
# ------------------------------------------------------------------ 3 -------
# The sixteen built-in presets, by number.
print("Part 3: the sixteen presets, 0 to 15.")
for preset in range(16):
light.set_preset(preset)
print(" preset", preset, "->", light.color)
time.sleep(0.3)
# ------------------------------------------------------------------ 4 -------
# flash() is a single wink. The program waits while it happens, and then the
# light goes back to the colour it had before.
print("Part 4: three quick yellow winks on a blue light.")
light.set_color(0, 0, 255)
for _ in range(3):
light.flash(255, 255, 0, duration_ms=120)
time.sleep(0.4)
# ------------------------------------------------------------------ 5 -------
# A rainbow, which only moves while you keep calling tick().
print("Part 5: rainbow WITH tick() - it moves.")
light.rainbow()
for _ in range(300): # about 6 seconds
light.tick()
time.sleep(0.02)
print("Part 6: rainbow WITHOUT tick() - nothing will happen. Watch.")
light.off()
light.rainbow()
time.sleep(4) # no tick() anywhere in here
print(" ...told you.")
print("Part 7: blinking. Also needs tick().")
light.blink(0, 255, 255)
for _ in range(250):
light.tick()
time.sleep(0.02)
light.off()
print("Finished. brightness is still", light.brightness, "per cent.")
  • Make your own rainbow: pick five colours and step through them in a loop.
  • Wink red on a green light whenever something happens in your program.
  • light.set_hex(...) takes any web colour. Look one up and try it.