MB-8mBotBeginner

mBot Square Drawing Robot: Four Sides with a Loop

Press the button on the mBot's board and the robot draws a square on the floor: it drives along one side, makes a quarter turn to the right on the spot and repeats this four times. It ends where it started. No extra parts are needed. The SIDE_MS time sets the side length and the TURN_MS time sets the turning angle.

0.0(0)
mBot Square Drawing Robot: Four Sides with a Loop
The project in which the mBot draws its first shape. You learn to repeat the same two moves four times with a for loop, that the side length is set by speed and time and the corner angle by the turning time, how a small angle error adds up over four corners, and how to tune the values for your own robot by trying. Change the number of sides and the same code draws a triangle or a hexagon.

Video

Circuit Diagram

mBot Square Drawing Robot: Four Sides with a Loop - Circuit Diagram

Source Code

C++ (Arduino IDE)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   mBot Square Drawing Robot: Four Sides with a Loop
4// ─────────────────────────────────────────
5// Press the button on the mBot's board and the robot draws a square on the
6// floor: it drives along one side, makes a quarter turn to the right on
7// the spot and repeats this four times. It ends where it started. No extra
8// 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 uint8_t SIDES = 4;                  // Number of sides
21const int SPEED = 150;                    // Straight driving speed, 0-255
22const int TURN_SPEED = 100;               // Turning speed, 0-255
23const unsigned long SIDE_MS = 1500;       // Time to drive one side (ms)
24const unsigned long TURN_MS = 455;        // Quarter turn time (ms)
25const unsigned long PAUSE_MS = 300;       // Stop between moves (ms)
26const unsigned long START_DELAY_MS = 1000; // Wait after the button (ms)
27const int PRESS_LIMIT = 100;              // Below this value means pressed
28const unsigned long POLL_MS = 20;         // How often the button is checked (ms)
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// Turns the robot right on the spot: the left wheel goes forward, the right wheel backward.
47void turnRight(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  // The square: for every side drive straight first, then turn right.
76  for (uint8_t side = 0; side < SIDES; side++) {
77    forward(SPEED);
78    delay(SIDE_MS);
79    pauseRobot();
80    turnRight(TURN_SPEED);
81    delay(TURN_MS);
82    pauseRobot();
83  }
84}
85

FAQ

Why does the square not close exactly?

If the turn at each corner is not exactly a quarter turn, the error adds up over four corners and the robot does not arrive where it started. Tune TURN_MS first: try a single turn, make the value larger if the robot turns too little and smaller if it turns too far. The 455 in the code is a calculated starting point; batteries, floor and wheels change the result. Bent sides mean the robot is not driving straight.

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.

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.

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.