Tag Archives: REG_LED

RGB LED Strips

I love the individually programmable NeoPixel strips and the animations that you can come up with when using an Arduino or other microcontroller. Adafruit taught me the basics and I took off from there.

My favorite animation from Adafruit is the “rainbowCycle” animation. It is included in the Adafruit_NeoPixel.h library examples inside the strandtest sketch. I took that example out of the sketch by deleting everything I didn’t need and renaming the sketch.
I didn’t like the direction that the animation flows from the far end of the strip to the end near the Arduino, so I figured a way to alter that.

Here is the edited version of the sketch.

#include <Adafruit_NeoPixel.h>
 
#define PIN 9
#define numberOfPixels 60
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numberOfPixels, PIN, NEO_GRB + NEO_KHZ800);
 
void setup() {
  strip.begin();
  strip.setBrightness(5); // LOW brightness
  allOff();
  strip.show(); // Initialize all pixels to 'off'
}
 
void loop() {
rainbowCycle(0);
}
 
void allOff() {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, strip.Color(0, 0, 0));
      strip.show();
  }
}
 
// Slightly different, this makes the rainbow equally distributed throughout
// I divided the number of pixels by three and got 20. I set up that
// there are three equally distributed rainbows.
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;
 
  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< 20; i++) {
      strip.setPixelColor(19-i, Wheel(((i * 256 / 20) + j) & 255));
      strip.setPixelColor(19-i+20, Wheel(((i * 256 / 20) + j) & 255));
      strip.setPixelColor(19-i+40, Wheel(((i * 256 / 20) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}
 
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}


The original section in the sketch looks like this:

void rainbowCycle(uint8_t wait) {
uint16_t i, j;
 
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}


The original code has the rainbow colors spread evenly over the length of the LED strip and cycles the colors from the far end toward the Arduino.

The strip has 60 pixels and I wanted to have the animation appear to flow from the Arduino toward the far end. I also wanted the rainbow to be spread evenly three times over the length of the LED strip.

All of the changes I needed to make are in this section of code:

for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}


First of all, to make the animation run in the opposite direction I just put “59-” in front of the “i” where it says “strip.setPixelColor(i, Wheel…”. It looks like:

for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(59-i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}

Yes, I could have had the for loop count down, but adding three characters was much easier. Also, “for(i=strip.numPixels()-1; i > -1; i–)” doesn’t seem to work and trouble shooting it would take longer than just adding three characters.

Instead of 59 I could have used strip.numPixels()-1. I was just making it easier for me with the strip I have.

To get three evenly distributed rainbows across the 60 NeoPixels I had to divide the total number (60) by three. So, I have three sets of twenty.

The three sets are separated out by i, i+20 and i+40. The count goes from 0 to 19, which is twenty numbers. To make the animation run backwards I had to put “19-” in front of the “i”. The section of code looks like:

for(i=0; i< 20; i++) {
strip.setPixelColor(19-i, Wheel(((i * 256 / 20) + j) & 255));
strip.setPixelColor(19-i+20, Wheel(((i * 256 / 20) + j) & 255));
strip.setPixelColor(19-i+40, Wheel(((i * 256 / 20) + j) & 255));
}

In the void loop() I called rainbowCycle(0);. The zero means no delay. I wanted it to run as quickly as possible on the Arduino UNO.

Here, again, is the final sketch.

#include <Adafruit_NeoPixel.h>
 
#define PIN 9
#define numberOfPixels 60
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numberOfPixels, PIN, NEO_GRB + NEO_KHZ800);
 
void setup() {
  strip.begin();
  strip.setBrightness(5); // LOW brightness
  allOff();
  strip.show(); // Initialize all pixels to 'off'
}
 
void loop() {
rainbowCycle(0);
}
 
void allOff() {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, strip.Color(0, 0, 0));
      strip.show();
  }
}
 
// Slightly different, this makes the rainbow equally distributed throughout
// I divided the number of pixels by three and got 20. I set up that
// there are three equally distributed rainbows.
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;
 
  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< 20; i++) {
      strip.setPixelColor(19-i, Wheel(((i * 256 / 20) + j) & 255));
      strip.setPixelColor(19-i+20, Wheel(((i * 256 / 20) + j) & 255));
      strip.setPixelColor(19-i+40, Wheel(((i * 256 / 20) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}
 
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}