MB-7mBotBeginner

mBot First Drive: Forward, Backward, Right and Left

Press the button on the mBot's board and the robot does the four basic moves in turn: it drives forward, comes back, turns right on the spot and turns left. Each move is a function of its own. No extra parts are needed. In a turn the two wheels spin in opposite directions; the TURN_MS time sets the angle.

0.0(0)
mBot First Drive: Forward, Backward, Right and Left
The project that teaches the mBot its four directions. You learn how changing the signs of the two motors gives forward, backward, right and left, how the robot turns on the spot by spinning its wheels in opposite directions, how putting each move in a named function makes the code readable, and that the turning angle is set with time. These four functions are used unchanged in the driving projects that follow.

Video

Circuit Diagram

mBot First Drive: Forward, Backward, Right and Left - Circuit Diagram

Source Code

C++ (Arduino IDE)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   mBot First Drive: Forward, Backward, Right and Left
4// ─────────────────────────────────────────
5// Press the button on the mBot's board and the robot does the four basic
6// moves in turn: it drives forward, comes back, turns right on the spot
7// and turns left. Each move is a function of its own. No extra parts are
8// 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 int SPEED = 150;                    // Straight driving speed, 0-255
21const int TURN_SPEED = 100;               // Turning speed, 0-255
22const unsigned long MOVE_MS = 1000;       // Straight driving time (ms)
23const unsigned long TURN_MS = 455;        // Quarter turn time (ms)
24const unsigned long PAUSE_MS = 500;       // Stop between moves (ms)
25const unsigned long START_DELAY_MS = 1000; // Wait after the button (ms)
26const int PRESS_LIMIT = 100;              // Below this value means pressed
27const unsigned long POLL_MS = 20;         // How often the button is checked (ms)
28
29MeDCMotor motorLeft(M1);                  // Left motor
30MeDCMotor motorRight(M2);                 // Right motor
31
32// Returns true while the button is down. The button pulls A7 to GND, so the reading is small while
33// pressed.
34bool isPressed() {
35  return analogRead(PIN_BUTTON) < PRESS_LIMIT;
36}
37
38// Drives the robot straight forward. The motors are mounted mirrored, so the left motor gets a
39// negative speed and the right motor a positive one.
40void forward(int speed) {
41  motorLeft.run(-speed);
42  motorRight.run(speed);
43}
44
45// Drives the robot straight backward: both motors turn the opposite way to forward.
46void backward(int speed) {
47  motorLeft.run(speed);
48  motorRight.run(-speed);
49}
50
51// Turns the robot right on the spot: the left wheel goes forward, the right wheel backward.
52void turnRight(int speed) {
53  motorLeft.run(-speed);
54  motorRight.run(-speed);
55}
56
57// Turns the robot left on the spot: the right wheel goes forward, the left wheel backward.
58void turnLeft(int speed) {
59  motorLeft.run(speed);
60  motorRight.run(speed);
61}
62
63// Stops both motors.
64void stopMotors() {
65  motorLeft.stop();
66  motorRight.stop();
67}
68
69// Stops the motors and waits a short while.
70void pauseRobot() {
71  stopMotors();
72  delay(PAUSE_MS);
73}
74
75void setup() {
76  stopMotors();                           // Start with the motors stopped
77}
78
79void loop() {
80  // Do nothing until the button is pressed; look again a little later.
81  if (!isPressed()) {
82    delay(POLL_MS);
83    return;
84  }
85  delay(START_DELAY_MS);                  // Time to move your hand away
86  forward(SPEED);
87  delay(MOVE_MS);
88  pauseRobot();
89  backward(SPEED);
90  delay(MOVE_MS);
91  pauseRobot();
92  turnRight(TURN_SPEED);
93  delay(TURN_MS);
94  pauseRobot();
95  turnLeft(TURN_SPEED);
96  delay(TURN_MS);
97  stopMotors();
98}
99

FAQ

Why is the turn not exactly a quarter turn?

The angle depends on the TURN_MS time and the TURN_SPEED speed. The value 455 in the code is a calculated starting point; the real angle changes with the batteries, the floor and the wheels. If the robot turns too little make TURN_MS larger, if it turns too far make it smaller. Change it by 20 or 30 at a time and try again; on carpet the value comes out larger.

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.

Does uploading this code erase the factory program on the robot?

Yes. The board holds one program at a time, so your code replaces the factory program and the ready-made modes, such as driving with the remote or line following, stop working. Bringing them back is easy: open mBlock, connect the mBot over USB and load the factory firmware again from the Firmware Update section.