Tag Archives: NeoPixel

NeoPixel Purse

I finished the code for the purse I was working on. I included a push button on an Arduino Nano to cycle through the colors. There are 16 different choices. Eight of the choices are animations.

NeoPixel Purse

I believe I saw this design on Adafruit.com. I could not find it again on their site, nor could I find it by searching on Google.

I invented some code to go with this purse so Linda would be able to choose what she wanted it to look like at the time.

#include <Adafruit_NeoPixel.h>

#define PIN 9
#define BUTTON_PIN 2
#define number_of_pixels 34
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
Adafruit_NeoPixel strip = Adafruit_NeoPixel(number_of_pixels, PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType = 0;
long time_counter = 0;
long increment = 25000;
int chase_position = 0;
int old_chase_position = 0;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.setBrightness(15); // LOW brightness
  for (uint16_t i = 0; i < number_of_pixels; i++) {
       strip.setPixelColor(i, strip.Color(255,0,0));
      }
  strip.show();
}

void loop() {
    // Get current button state.
  bool newState = digitalRead(BUTTON_PIN);

  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {
      showType++;
      if (showType > 15)
        showType=0;
      startShow(showType);
    }
  }

  // Set the last button state to the old state.
  oldState = newState;

  if(time_counter == increment) {
    time_counter = 0;
    check_running(showType);
  }
  time_counter += 1;
}

// red purple blue white pink magenta green
// all theater chase
// rainbow cycle all at a time.
// rainbow chase like it already has but continuous

void startShow(int i) {
  switch(i){
    case 0: colorWipe(strip.Color(255, 0, 0));    // Red
            break;
    case 1: colorWipe(strip.Color(128, 0, 128));  // Purple
            break;
    case 2: colorWipe(strip.Color(0, 0, 255));  // Blue
            break;
    case 3: colorWipe(strip.Color(128, 128, 128));  // White
            break;
    case 4: colorWipe(strip.Color(255, 80, 80));  // Pink
            break;
    case 5: colorWipe(strip.Color(255, 0, 255));  // Magenta
            break;
    case 6: colorWipe(strip.Color(0, 128, 0));  // Green
            break;
    case 7: theaterChase2(strip.Color(255, 0, 0));    // Red
            break;
    case 8: theaterChase2(strip.Color(128, 0, 128));  // Purple
            break;
    case 9: theaterChase2(strip.Color(0, 0, 255));  // Blue
            break;
    case 10: theaterChase2(strip.Color(128, 128, 128)); // White
            break;
    case 11: theaterChase2(strip.Color(255, 128, 128));  // Pink
            break;
    case 12: theaterChase2(strip.Color(255, 0, 255));  // Magenta
            break;
    case 13: theaterChase2(strip.Color(0, 128, 0));  // Green
            break;
    case 14: rainbow1();
            break;
    case 15: rainbowCycle(1);
            break;
  }
}

void check_running(int i) {
  switch(i){
    case 0: break;
    case 1: break;
    case 2: break;
    case 3: break;
    case 4: break;
    case 5: break;
    case 6: break;
    case 7: theaterChase2(strip.Color(255, 0, 0));    // Red
            break;
    case 8: theaterChase2(strip.Color(128, 0, 128));  // Purple
            break;
    case 9: theaterChase2(strip.Color(0, 0, 255));  // Blue
            break;
    case 10: theaterChase2(strip.Color(128, 128, 128)); // White
            break;
    case 11: theaterChase2(strip.Color(255, 80, 80));  // Pink
            break;
    case 12: theaterChase2(strip.Color(255, 0, 255));  // Magenta
            break;
    case 13: theaterChase2(strip.Color(0, 128, 0));  // Green
            break;
    case 14: break;
    case 15: rainbowCycle(1);
            break;
  }
}

// Fill the LEDs with a color
void colorWipe(uint32_t c) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
  }
}

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

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;
  j = 0;
  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);
  }
}

// Theater-style crawling lights -edited-
void theaterChase2(uint32_t c) {
  for (int i=0; i < strip.numPixels(); i=i+3) {
    strip.setPixelColor(i+old_chase_position, 0);
  }
  for (int i=0; i < strip.numPixels(); i=i+3) {
    strip.setPixelColor(i+chase_position, c);
  }
  strip.show();
  old_chase_position = chase_position;
  chase_position +=1;
  if (chase_position > 2) {
    chase_position = 0;
  }
}

// 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) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

I got the base code from the Arduino web site and changed it to do what I wanted. I didn’t like that the original code would cycle through animations and not allow you to select the next one until it was finished. I change that in the void loop() section by including a counter with an increment. When the counter meets the increment reset it to zero and go do the animation one more step. Mainly this is for the theater style crawling lights. If you want the lights to crawl faster make this number smaller. (I don’t mean in font size.)

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);
  }
}