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.)

Leave a Reply

Your email address will not be published. Required fields are marked *