Skip to content

Search the documentation

Kit
Firmware

Does the map block work? — Mini

A twenty-second test of one block. It is a three-line program, built so that anybody with a Mini can check that the map block works. It prints three numbered lines, so you can see exactly how far the program got.

This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Mini.

The blocks of this example, from top to bottom.
Click the picture to view it larger.Open full sizeThe same program written as Python is further down this page, in The Python code.
Read the program as text
  • MoBot Mini Start
  • Print to serial "1 of 3: about to use the map block ..."
  • set Motor 1 to Direction 1 with speed (map values of Read from joystick the value of Y from min : -128 max : 127 to min : 0 max : 100) %
  • Print to serial "2 of 3: the map block worked! The left wheel should be turning."
  • wait 3 seconds
  • set Motor 1 to Direction 1 with speed 0 %
  • Print to serial "3 of 3: finished."

A joystick gives numbers from -128 to 127. A wheel takes a speed from 0 to 100. They do not fit. The map block is the block that stretches one range onto the other, so a joystick pushed half way gives half speed.

08-ble-joystick shows the problem it is meant to solve.

  • A MoBot Mini, charged and switched on.
  • A USB cable.
  • Nothing else. You do not even need to connect a phone.

⚠️ First time? Read the “Before you send it” section of Blink and fade.

  1. Open 09-ble-map-check.mobot in MoBot and press Send to Device.
  2. Open the serial monitor. Read what it says. That is the whole test.
[MOBOT] 1 of 3: about to use the map block ...
[MOBOT] 2 of 3: the map block worked! The left wheel should be turning.
[MOBOT] 3 of 3: finished.

and the left wheel turns for three seconds, at about half speed — because with the joystick sitting at 0, half way between -128 and 127, the map block gives about 50.

Stand the Mini on a book first, so the wheel spins in the air.

If the monitor shows an AttributeError about map_range after line 1 of 3, your Mini has older firmware, which had no map block. Update the firmware and try again.

block what it does
mobot_blemap Stretches a number from one range onto another.
mobot_blejoystick The number being stretched — the joystick’s up-and-down position.
mobot_motordirspeed Where the stretched number ends up: the wheel’s speed.
mobot_print The three numbered lines that tell you exactly how far the program got.

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.

09-ble-map-check.firmware-3.main.py
# Does the map block work? — mini (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 09-ble-map-check.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 09-ble-map-check.main.py.
from machine import Pin
import time
from mobot import map_range
print("[MOBOT]", '1 of 3: about to use the map block ...')
robot.left_motor.forward(map_range(robot.gamepad.y, (-128), 127, 0, 100))
print("[MOBOT]", '2 of 3: the map block worked! The left wheel should be turning.')
time.sleep(3)
robot.left_motor.forward(0)
print("[MOBOT]", '3 of 3: finished.')