MB-6mBotBeginner

mBot Drive Forward and Stop: First Move with the Motors

Press the button on the mBot's board: the robot waits one second, drives straight forward for two seconds and stops. Every press repeats the move. No extra parts are needed. The two motors are plugged into M1 and M2; to go straight the left motor gets a negative speed and the right motor a positive one.

0.0(0)
mBot Drive Forward and Stop: First Move with the Motors
The project that moves the mBot for the first time. You learn to define the two motors in code, that speed runs from 0 to 255, why one motor gets a negative speed to go straight when the motors are mounted mirrored, how a wait sets the length of a move, and how to start the robot with the button. The forward and stop functions are the base of every driving project that follows.

Video

Circuit Diagram

mBot Drive Forward and Stop: First Move with the Motors - Circuit Diagram

Source Code

C++ (Arduino IDE)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   mBot Drive Forward and Stop: First Move with the Motors
4// ─────────────────────────────────────────
5// Press the button on the mBot's board: the robot waits one second, drives
6// straight forward for two seconds and stops. Every press repeats the
7// move. No extra parts are needed.
8//
9// Pins:
10//   M1 (D6, D7) -> Left motor
11//   M2 (D5, D4) -> Right motor
12//   A7 -> Onboard button
13
14#include <MeMCore.h>
15
16const uint8_t PIN_BUTTON = A7;            // Onboard button
17
18// ============ SETTINGS ============
19const int SPEED = 150;                    // Motor speed, 0-255
20const unsigned long DRIVE_MS = 2000;      // Time to drive forward (ms)
21const unsigned long START_DELAY_MS = 1000; // Wait after the button (ms)
22const int PRESS_LIMIT = 100;              // Below this value means pressed
23const unsigned long POLL_MS = 20;         // How often the button is checked (ms)
24
25MeDCMotor motorLeft(M1);                  // Left motor
26MeDCMotor motorRight(M2);                 // Right motor
27
28// Returns true while the button is down. The button pulls A7 to GND, so the reading is small while
29// pressed.
30bool isPressed() {
31  return analogRead(PIN_BUTTON) < PRESS_LIMIT;
32}
33
34// Drives the robot straight forward. The motors are mounted mirrored, so the left motor gets a
35// negative speed and the right motor a positive one.
36void forward(int speed) {
37  motorLeft.run(-speed);
38  motorRight.run(speed);
39}
40
41// Stops both motors.
42void stopMotors() {
43  motorLeft.stop();
44  motorRight.stop();
45}
46
47void setup() {
48  stopMotors();                           // Start with the motors stopped
49}
50
51void loop() {
52  // Do nothing until the button is pressed; look again a little later.
53  if (!isPressed()) {
54    delay(POLL_MS);
55    return;
56  }
57  delay(START_DELAY_MS);                  // Time to move your hand away
58  forward(SPEED);
59  delay(DRIVE_MS);
60  stopMotors();
61}
62

FAQ

How far does the robot go, and how do I change the distance?

The distance depends on speed times time. To go farther make DRIVE_MS larger (3000 means three seconds) or raise SPEED; the maximum is 255. The exact distance changes with the batteries and the floor: the same code covers less ground on carpet and more on a smooth floor. Tune it by trying on your own robot.

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.