Stopping and leaving outputs
A program turns things on: wheels, lights, sound. What happens to them when the program is over? The answer depends on how the program ended and on which firmware the robot runs.
This page uses three words:
- Outputs are the wheels, the lights and the sound.
- Turned off means the robot switches them off by itself. The wheels are let go and roll to a halt, like
stop(), notbrake(). The lights go out. A song stops. - Left as they are means the robot does nothing. A wheel keeps turning and a light stays on until something changes them.
The short answer
Section titled “The short answer”- Firmware 3.0.1 and later. A program that ends by itself leaves the robot as it set it. A light it turned on stays on. A wheel it left turning keeps turning. Stop, a mode change and a computer taking over still turn the outputs off.
- Firmware 2.1.0 and 3.0.0. The robot turns the outputs off whenever your program ends, by itself or not.
- Firmware 2.0.0. Nothing in the firmware turns them off when a program ends. MicroPython does not touch the hardware on its way out, so a wheel keeps its last setting.
Do you not know which firmware you have? See Firmware versions.
Case by case
Section titled “Case by case”This table is about a program that the robot started, because it is in Programmable mode and the program is saved as main.py. See Modes and programs.
| What happens | 3.0.1 and later | 2.1.0 and 3.0.0 |
|---|---|---|
The program reaches its last line, stops with an error, or calls sys.exit() |
Left as they are. The robot waits in Programmable, stopped. | Turned off. |
| You press Stop in the IDE | Turned off. | Turned off. |
| You choose another mode in the app | Turned off, then the new mode starts. | Turned off, then the new mode starts. |
| A computer takes the robot: Run, Save, Send to Device, or a Ctrl-C from any program | Turned off. | Turned off. |
After a program ended by itself on 3.0.1 or later, the outputs stay on until you do something. Press Stop in the IDE, choose another mode in the app, or switch the robot off. Stop also works when the program has already ended.
A built-in mode of the app that cannot run turns the outputs off, and the robot stays in that mode, stopped.
Programs that you start with Run
Section titled “Programs that you start with Run”Run in the IDE does not hand the program to the robot’s mode runner. The IDE takes the robot first, which turns the outputs off on firmware 2.1.0 and later. Then the program runs from the Python prompt.
- When it ends by itself, the outputs are left as they are, on any firmware.
- When you press Stop, the IDE stops the wheels and the buzzer. It does not switch lights off and it does not release servos, so a light your program turned on stays on.
On firmware 2.0.0
Section titled “On firmware 2.0.0”The robot has no mode runner. A mode change restarts the robot, and the wheels stop first. When you press Stop in the IDE, it stops the wheels and the buzzer. Lights stay as they were.
The Smart Home
Section titled “The Smart Home”The Smart Home has no mode runner and nothing switches its outputs off when a program ends. Stop ends your program, and nothing else changes. A fan keeps its speed and a light stays on until your program, or you, switch it off.
An example
Section titled “An example”The first example of the Smart Car, Drive a square, ends with these two lines on firmware 3.0:
robot.led2.on()print("Finished. Did it come back to where it started?")On firmware 3.0.1 and later, LED 2 stays on after the program ends. The example’s page says that LED 2 comes on at the end. On firmware 2.1.0 and 3.0.0 the robot turns LED 2 off again as soon as the program is over.
Switch things off yourself
Section titled “Switch things off yourself”Do not rely on the robot to tidy up. Whatever your program starts, it can also end. This is how the Mini’s first example finishes, with the colour light switched off at the end:
left.brake()right.brake()robot.rgb_led.set_color(0, 255, 0)print("Finished.")time.sleep(1)robot.rgb_led.off()The example Drive a square stops the wheels with halt(), which calls stop() on both motors. A program that has while True: in it can use a try: and finally: block to switch things off when it is interrupted. The firmware gives your program a short moment to finish a finally: block when the app asks for another mode.
Housekeeping when you leave Programmable
Section titled “Housekeeping when you leave Programmable”When the robot leaves Programmable, because the mode changes or a computer takes over and later gives the robot back, it also tidies up after your program. It rebuilds the robot’s parts, stops timers your program started, removes the pin handlers it set up, releases the servo pulses on the Smart Car, and deletes the names your program made.