MB-2mBotBeginner

Color Fade with mBot: A Rainbow on the RGB LEDs

The two RGB LEDs on the mBot's board fade smoothly through the colors of the rainbow: from red to green, green to blue and blue back to red. The left LED runs a little ahead of the right one on the color wheel, so the two always show different shades. No extra parts. A single number (0-255) holds the place on the wheel, and the code turns it into red, green and blue values.

0.0(0)
Color Fade with mBot: A Rainbow on the RGB LEDs
A project that teaches you to calculate colors instead of typing them one by one. You learn what a color wheel is, how a single number from 0 to 255 becomes a mix of red, green and blue, how to fade smoothly by lowering one color while raising the next, how to increase a variable on every pass and let it wrap around after 255, how to give two LEDs different places on the same wheel, and how to set the speed with a waiting time. This is the basis of night lights and light shows.

Video

Circuit Diagram

Color Fade with mBot: A Rainbow on the RGB LEDs - Circuit Diagram

Source Code

C++ (Arduino IDE)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   Color Fade with mBot: A Rainbow on the RGB LEDs
4// ─────────────────────────────────────────
5// The two RGB LEDs on the mBot's board fade smoothly through the colors of
6// the rainbow: from red to green, green to blue and blue back to red. The
7// left LED runs a little ahead of the right one on the color wheel, so the
8// two always show different shades. No extra parts.
9//
10// Pins:
11//   D13 (PORT_7) -> 2 onboard RGB LEDs
12
13#include <MeMCore.h>
14
15const uint8_t LED_COUNT = 2;              // Number of LEDs on the board
16const uint8_t RIGHT_LED = 1;              // Right LED
17const uint8_t LEFT_LED = 2;               // Left LED
18
19// ============ SETTINGS ============
20const uint8_t LEVEL = 60;                 // Brightness, 0-255
21const unsigned long STEP_MS = 20;         // Wait between two steps (ms)
22const uint8_t LEFT_OFFSET = 64;           // Lead of the left LED on the wheel
23
24MeRGBLed led(PORT_7, LED_COUNT);
25uint8_t wheelPos = 0;                     // Place on the color wheel, 0-255
26
27// Turns a number from 0 to 85 into a brightness from 0 to LEVEL.
28uint8_t scale(uint8_t part) {
29  return (uint16_t)part * LEVEL / 85;
30}
31
32// The color wheel: 0 is red, 85 green, 170 blue and 255 red again. In between, one color falls
33// while the next one rises.
34void setWheelColor(uint8_t index, uint8_t pos) {
35  if (pos < 85) {
36    led.setColor(index, LEVEL - scale(pos), scale(pos), 0);
37  } else if (pos < 170) {
38    pos -= 85;
39    led.setColor(index, 0, LEVEL - scale(pos), scale(pos));
40  } else {
41    pos -= 170;
42    led.setColor(index, scale(pos), 0, LEVEL - scale(pos));
43  }
44}
45
46void setup() {
47  led.setColor(0, 0, 0);                  // Start with the LEDs off
48  led.show();
49}
50
51void loop() {
52  setWheelColor(RIGHT_LED, wheelPos);
53  setWheelColor(LEFT_LED, (uint8_t)(wheelPos + LEFT_OFFSET));
54  led.show();
55  wheelPos++;                             // Wraps back to 0 after 255
56  delay(STEP_MS);
57}
58

FAQ

How do I make the fade faster or slower?

STEP_MS is the wait between two steps. The wheel has 256 steps; at 20 ms one full turn takes about 5 seconds. Set it to 5 and a turn drops to 1.3 seconds, set it to 50 and it grows to 13 seconds. With very small values the colors blur together for the eye.

Why are the two LEDs not the same color?

The left LED gets the color 64 steps further along the wheel (LEFT_OFFSET). 64 is a quarter of the wheel; while the right LED is red, the left one is a yellowish green. If you want both the same, set LEFT_OFFSET to 0; for opposite shades try 128.

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.