Menu
CoddyTech

Rainbow Ring

Part of the Displays and Sensors section of Coddy's Arduino journey. Lesson 17 of 24.

Choosing red, green and blue for a rainbow is hard. Hue is easier: one number that goes round the colour wheel, from red through yellow, green, cyan, blue and magenta, and back to red.

ring.ColorHSV(hue) turns a hue into a colour. The hue goes from 0 to 65535: 0 is red, about 21845 is green, about 43690 is blue, and 65536 would be red again.

To spread one rainbow over 16 pixels, give each pixel a step of 65536 / 16 = 4096 more than the one before. Pixel 15 needs 61440, more than an int holds, so keep the hue in a long and write the number as 4096L; the L makes the multiplication use long too:

for (int i = 0; i < 16; i++) {
  long hue = i * 4096L;
  ring.setPixelColor(i, ring.ColorHSV(hue));
}
ring.show();
challenge icon

Challenge

Medium

Paint a rainbow round the ring:

  • pixel 0 red (hue 0)
  • each next pixel 4096 further round the wheel, with ring.ColorHSV()
  • so the 16 pixels go once round

Press Run, then Check.

Try it yourself

#include <Adafruit_NeoPixel.h>

// 16 pixels, data on pin 6
Adafruit_NeoPixel ring(16, 6, NEO_GRB + NEO_KHZ800);

void setup() {
  ring.begin();

  // give each pixel its own hue, red at pixel 0

  ring.show();
}

void loop() {
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Displays and Sensors

Practice on your own: Arduino playground