Change Colors with the mBot Button: A New Color on Every Press
Every press of the button on the mBot's board moves the two RGB LEDs to the next color: red, green, blue, yellow, purple, cyan, white and off. Holding the button does not change the color again; one press is one step. No extra parts are needed. The button is read as an analog value on pin A7: close to 0 while pressed, up at 1023 when released.

Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// Change Colors with the mBot Button: A New Color on Every Press
4// ─────────────────────────────────────────
5// Every press of the button on the mBot's board moves the two RGB LEDs to
6// the next color: red, green, blue, yellow, purple, cyan, white and off.
7// Holding the button does not change the color again; one press is one
8// step. No extra parts are needed.
9//
10// Pins:
11// D13 (PORT_7) -> 2 onboard RGB LEDs
12// A7 -> Onboard button
13
14#include <MeMCore.h>
15
16const uint8_t LED_COUNT = 2; // Number of LEDs on the board
17const uint8_t ALL_LEDS = 0; // Number 0 picks both LEDs
18const uint8_t PIN_BUTTON = A7; // Onboard button
19
20// ============ SETTINGS ============
21const uint8_t LEVEL = 60; // Brightness, 0-255
22const int PRESS_LIMIT = 100; // Below this value means pressed
23const unsigned long DEBOUNCE_MS = 30; // Wait for button bounce (ms)
24
25// The color table: one color per row. The three numbers say whether red, green and blue are on (1
26// on, 0 off).
27const uint8_t COLORS[][3] = {
28 {1, 0, 0}, // Red
29 {0, 1, 0}, // Green
30 {0, 0, 1}, // Blue
31 {1, 1, 0}, // Yellow
32 {1, 0, 1}, // Purple
33 {0, 1, 1}, // Cyan
34 {1, 1, 1}, // White
35 {0, 0, 0}, // Off
36};
37const uint8_t COLOR_COUNT = sizeof(COLORS) / sizeof(COLORS[0]);
38
39MeRGBLed led(PORT_7, LED_COUNT);
40uint8_t colorIndex = COLOR_COUNT - 1; // Place in the table; starts at off
41bool wasPressed = false; // Was it down at the last reading
42
43// Returns true while the button is down. The button pulls A7 to GND, so the reading is small while
44// pressed.
45bool isPressed() {
46 return analogRead(PIN_BUTTON) < PRESS_LIMIT;
47}
48
49// Writes the color from the table to both LEDs and shows it.
50void showColor(uint8_t index) {
51 led.setColor(ALL_LEDS, COLORS[index][0] * LEVEL, COLORS[index][1] * LEVEL, COLORS[index][2] * LEVEL);
52 led.show();
53}
54
55void setup() {
56 showColor(colorIndex); // Start with the LEDs off
57}
58
59void loop() {
60 bool pressed = isPressed();
61 // The color changes only at the moment of the press: down now, not down at the last reading.
62 if (pressed && !wasPressed) {
63 colorIndex = (colorIndex + 1) % COLOR_COUNT;
64 showColor(colorIndex);
65 }
66 wasPressed = pressed;
67 delay(DEBOUNCE_MS);
68}
69FAQ
Why is the button read with analogRead?
On the mBot's board the button is wired to pin A7, and that pin works only as an analog input; digitalRead cannot read it. While the button is released, a resistor pulls the pin up to 5 V and the reading is about 1023. Pressing it connects the pin to GND and the value drops to 0. The code treats anything below 100 as pressed.
How do I add my own color to the list?
Add a new row to the COLORS table: the three numbers say whether red, green and blue are on. COLOR_COUNT counts the rows by itself, so nothing else needs to change. In this table every color is either fully on or off, which is why there are at most eight different colors.
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.
