mBot Stop at the Line: Brake When It Sees Black Tape
Press the button and the mBot drives straight ahead, then stops as soon as the line sensor underneath sees a black line. Without a line it stops by itself after five seconds. The line sensor is in port 2; a strip of black tape is all you need for the line.

Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// mBot Stop at the Line: Brake When It Sees Black Tape
4// ─────────────────────────────────────────
5// Press the button and the mBot drives straight ahead, then stops as soon
6// as the line sensor underneath sees a black line. Without a line it stops
7// by itself after five seconds.
8//
9// Pins:
10// M1 (D6, D7) -> Left motor
11// M2 (D5, D4) -> Right motor
12// PORT_2 (D9, D10) -> Line follower sensor
13// A7 -> Onboard button
14
15#include <MeMCore.h>
16
17const uint8_t PIN_BUTTON = A7; // Onboard button
18
19// ============ SETTINGS ============
20const int SPEED = 100; // Motor speed, 0-255
21const unsigned long MAX_DRIVE_MS = 5000; // Longest drive without a line (ms)
22const unsigned long LINE_READ_MS = 5; // How often the line is checked (ms)
23const unsigned long START_DELAY_MS = 1000; // Wait after the button (ms)
24const int PRESS_LIMIT = 100; // Below this value means pressed
25const unsigned long POLL_MS = 20; // How often the button is checked (ms)
26
27MeDCMotor motorLeft(M1); // Left motor
28MeDCMotor motorRight(M2); // Right motor
29MeLineFollower lineFollower(PORT_2);
30
31// Returns true while the button is down. The button pulls A7 to GND, so the reading is small while
32// pressed.
33bool isPressed() {
34 return analogRead(PIN_BUTTON) < PRESS_LIMIT;
35}
36
37// Drives the robot straight forward. The motors are mounted mirrored, so the left motor gets a
38// negative speed and the right motor a positive one.
39void forward(int speed) {
40 motorLeft.run(-speed);
41 motorRight.run(speed);
42}
43
44// Stops both motors.
45void stopMotors() {
46 motorLeft.stop();
47 motorRight.stop();
48}
49
50// Returns true when at least one of the two sensors sees the black line. A sensor reads 0 on black
51// and 1 on white.
52bool onLine() {
53 return lineFollower.readSensors() != S1_OUT_S2_OUT;
54}
55
56void setup() {
57 stopMotors(); // Start with the motors stopped
58}
59
60void loop() {
61 // Do nothing until the button is pressed; look again a little later.
62 if (!isPressed()) {
63 delay(POLL_MS);
64 return;
65 }
66 delay(START_DELAY_MS); // Time to move your hand away
67 forward(SPEED);
68 unsigned long startTime = millis();
69 // Keep driving until the line is seen or the time runs out.
70 while (!onLine() && millis() - startTime < MAX_DRIVE_MS) {
71 delay(LINE_READ_MS);
72 }
73 stopMotors();
74}
75FAQ
What do I make the line from?
Stick black electrical tape about 2 centimeters wide on a light, matte floor; a thick line drawn with a black marker on white cardboard also works. Lay the tape across the robot's path. On shiny or dark floors the sensor may not tell the line apart.
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.
