Project 5.02 Using ColorHSV

There are multiple ways to set the color of the Neopixels. Until now we have used the Color function to set the RGB color of the Neopixel. We can also use the function ColorHSV to set the color a different way. HSV stands for Hue, Saturation, and Value. Hue refers to the color of the Neopixel, Saturation refers to how white the Neopixel is, and Value refers to how bright the Neopixel is. Hue in this case is a 16-bit value (0 - 65535), while Saturation and Value are 8-bit values (0 - 255). Which color method you use is totally up to you and mostly comes down to preference and application.

Note: Using ColorHSV does make it easier to loop through the entire color spectrum.

Project Code:

////////////////////////////////////////////////////////
// 5.02 - Using ColorHSV

#include <Adafruit_NeoPixel.h>
byte saturation = 255;
byte value = 255;

byte dataPin = 10;
byte numberOfLEDs = 2;
byte brightness = 10;

Adafruit_NeoPixel pixels(numberOfLEDs, dataPin, NEO_GRB + NEO_KHZ800);
void setup() {
  Serial.begin(9600);
  pixels.begin();
  pixels.setBrightness(brightness);
}

void loop() {
  for (long hue = 0; hue < 65536; hue++) {
    pixels.setPixelColor(0, pixels.ColorHSV(hue, saturation, value));
    pixels.setPixelColor(1, pixels.ColorHSV(hue, saturation, value));
    pixels.show();
    delayMicroseconds(3); 
  }

}
////////////////////////////////////////////////////////

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

Two variables are used to set the saturation and value parameters. These values are not changed throughout the course of the program. Feel free to change these values and see how it effects the program!

byte saturation = 255;
byte value = 255;

In the loop section of the program, all possible hue values are looped through and displayed. Remember, hue is represented by a 16-bit value (0 - 65535).

void loop() {
  for (long hue = 0; hue < 65536; hue++) {
    pixels.setPixelColor(0, pixels.ColorHSV(hue, saturation, value));
    pixels.setPixelColor(1, pixels.ColorHSV(hue, saturation, value));   

Every time the hue value is changed, the Neopixels are updated to provide a smooth transition between colors. 

pixels.show();

A small delay is incorporated to slow down the color cycling. This delay function uses microseconds to delay instead of milliseconds. 1 second is 1000 milliseconds and 1 millisecond is 1000 microseconds. That means that we are delaying 3 millionths of a second here! We use microseconds becuase we want the color to transition smoothly. There are 65,536 colors being displayed in the for-loop. Using the regular delay function between each one would make it look choppy and make the full color cycle take too long. Change the delayMicroseconds to delay and see what happens!

    delayMicroseconds(3); 
  }
}
////////////////////////////////////////////////////////
Previous
Previous

Project 5.03 Individually addressing Neopixels using buttons

Next
Next

Project 5.01 Cycling Colors with a Button