mBot Distance Color LED: Green, Yellow, Red by Distance
As an object gets closer to the front of the mBot, the two LEDs on the board change color: green when far, yellow in the middle, red when near. No extra parts are needed. The limits are 20 and 50 centimeters; the distance is measured ten times a second.

Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// mBot Distance Color LED: Green, Yellow, Red by Distance
4// ─────────────────────────────────────────
5// As an object gets closer to the front of the mBot, the two LEDs on the
6// board change color: green when far, yellow in the middle, red when near.
7// No extra parts are needed.
8//
9// Pins:
10// D13 (PORT_7) -> 2 onboard RGB LEDs
11// PORT_3 (A3) -> Ultrasonic sensor
12
13#include <MeMCore.h>
14
15const uint8_t LED_COUNT = 2; // Number of LEDs on the board
16const uint8_t ALL_LEDS = 0; // Number 0 picks both LEDs
17
18// ============ SETTINGS ============
19const uint8_t LEVEL = 60; // Brightness, 0-255
20const float NEAR_CM = 20; // Closer than this is red (cm)
21const float FAR_CM = 50; // Farther than this is green (cm)
22const unsigned long READ_MS = 100; // Time between readings (ms)
23
24MeRGBLed led(PORT_7, LED_COUNT);
25MeUltrasonicSensor ultrasonic(PORT_3);
26uint8_t lastZone = 255; // The zone shown last
27
28// Returns the zone number for a distance: 0 near, 1 middle, 2 far.
29uint8_t zoneOf(float cm) {
30 if (cm < NEAR_CM) {
31 return 0;
32 }
33 if (cm < FAR_CM) {
34 return 1;
35 }
36 return 2;
37}
38
39// Writes the color of the zone to both LEDs: near red, middle yellow, far green.
40void showZone(uint8_t zone) {
41 if (zone == 0) {
42 led.setColor(ALL_LEDS, LEVEL, 0, 0);
43 } else if (zone == 1) {
44 led.setColor(ALL_LEDS, LEVEL, LEVEL, 0);
45 } else {
46 led.setColor(ALL_LEDS, 0, LEVEL, 0);
47 }
48 led.show();
49}
50
51void setup() {
52}
53
54void loop() {
55 uint8_t zone = zoneOf(ultrasonic.distanceCm());
56 // The LEDs are written only when the zone changes.
57 if (zone != lastZone) {
58 showZone(zone);
59 lastZone = zone;
60 }
61 delay(READ_MS);
62}
63FAQ
How do I set the distance where the colors change?
Change the numbers `NEAR_CM` and `FAR_CM` at the top of the code. `NEAR_CM` is where red starts and `FAR_CM` is where green starts; yellow is in between. With 10 and 30, for example, the colors change closer to the robot. `NEAR_CM` must always be smaller than `FAR_CM`, otherwise the yellow zone disappears.
How does the ultrasonic sensor measure distance?
One of the sensor's two eyes sends out a sound too high for us to hear, and the other listens for it to bounce back from an object. The time the sound takes to go and return gives the distance; the library does the math and returns the result in centimeters. The sensor measures from about 3 cm to 400 cm; with nothing in front it returns 400.
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.
