mBot Speed Levels: Slow, Medium, Fast
Press the button on the mBot's board and the robot drives forward and back at three speeds: slow first, then medium, then fast. The time is always the same; the higher the speed, the farther the robot goes. No extra parts are needed. The speeds sit in the SPEEDS table: 100, 170 and 255.

Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// mBot Speed Levels: Slow, Medium, Fast
4// ─────────────────────────────────────────
5// Press the button on the mBot's board and the robot drives forward and
6// back at three speeds: slow first, then medium, then fast. The time is
7// always the same; the higher the speed, the farther the robot goes. No
8// extra parts are needed.
9//
10// Pins:
11// M1 (D6, D7) -> Left motor
12// M2 (D5, D4) -> Right motor
13// A7 -> Onboard button
14
15#include <MeMCore.h>
16
17const uint8_t PIN_BUTTON = A7; // Onboard button
18
19// ============ SETTINGS ============
20const unsigned long RUN_MS = 1000; // Time to drive each way (ms)
21const unsigned long PAUSE_MS = 500; // Stop between moves (ms)
22const unsigned long START_DELAY_MS = 1000; // Wait after the button (ms)
23const int PRESS_LIMIT = 100; // Below this value means pressed
24const unsigned long POLL_MS = 20; // How often the button is checked (ms)
25
26// The speed table (0-255): slow, medium, fast.
27const int SPEEDS[] = {100, 170, 255};
28const uint8_t SPEED_COUNT = sizeof(SPEEDS) / sizeof(SPEEDS[0]);
29
30MeDCMotor motorLeft(M1); // Left motor
31MeDCMotor motorRight(M2); // Right motor
32
33// Returns true while the button is down. The button pulls A7 to GND, so the reading is small while
34// pressed.
35bool isPressed() {
36 return analogRead(PIN_BUTTON) < PRESS_LIMIT;
37}
38
39// Drives the robot straight forward. The motors are mounted mirrored, so the left motor gets a
40// negative speed and the right motor a positive one.
41void forward(int speed) {
42 motorLeft.run(-speed);
43 motorRight.run(speed);
44}
45
46// Drives the robot straight backward: both motors turn the opposite way to forward.
47void backward(int speed) {
48 motorLeft.run(speed);
49 motorRight.run(-speed);
50}
51
52// Stops both motors.
53void stopMotors() {
54 motorLeft.stop();
55 motorRight.stop();
56}
57
58// Stops the motors and waits a short while.
59void pauseRobot() {
60 stopMotors();
61 delay(PAUSE_MS);
62}
63
64void setup() {
65 stopMotors(); // Start with the motors stopped
66}
67
68void loop() {
69 // Do nothing until the button is pressed; look again a little later.
70 if (!isPressed()) {
71 delay(POLL_MS);
72 return;
73 }
74 delay(START_DELAY_MS); // Time to move your hand away
75 // Drive forward and come back for the same time at every speed in the table.
76 for (uint8_t i = 0; i < SPEED_COUNT; i++) {
77 forward(SPEEDS[i]);
78 delay(RUN_MS);
79 pauseRobot();
80 backward(SPEEDS[i]);
81 delay(RUN_MS);
82 pauseRobot();
83 }
84}
85FAQ
Why is 255 the highest speed value?
The motor speed is set by switching the power on and off very quickly; this method is called PWM. The value says how long the power stays on, as a number from 0 to 255: 0 is never, 255 is always. The library brings anything above 255 down to 255. The speed value is not a unit like centimeters; the same value turns the motor faster on fresh batteries.
How do I test the robot safely the first time?
For the first try put the robot on a box or hold it in your hand so the wheels spin in the air; that way you see which wheel turns which way. Then unplug the USB cable, put the robot on the floor, not on a table, and keep the space in front of it clear. After you press the button the robot waits one second, so you have time to move your hand away.
Why does one motor get a negative speed?
The two motors are mounted on the chassis mirrored, facing opposite ways. Give both the same sign and the wheels turn in opposite directions, so the robot spins on the spot. To drive straight forward the left motor (M1) gets a negative speed and the right motor (M2) a positive one. The code does this inside the forward function.
Which board do I pick for the mBot in the Arduino IDE?
The mBot's board, the mCore, uses the same processor as the Arduino Uno (ATmega328P). Choose Arduino Uno under Tools > Board and the robot's port under Tools > Port. For the code to compile, the MakeBlock Drive Updated library must be installed from the Library Manager; the code pulls it in under the name MeMCore.h.
