Neopixel colorwipe in EEPROM

Hi there, i have modified code from Adafruit NeoPixel Library with the example buttoncycler and EEPROM. My goal is to save the switch case after power off. I achieved that, the issue is that colorwipe doesn’t start at start up and i have to press the button to start and i want to start from the beginning after power on.
I’ve done several things to get colorwipe starting, but I failed, any help will be great help for my project.


Simple demonstration on using an input device to trigger changes on your
// NeoPixels. Wire a momentary push button to connect from ground to a
// digital IO pin. When the button is pressed it will change to a new pixel
// animation. Initial state has all pixels off -- press the button once to
// start the first animation. As written, the button does not interrupt an
// animation in-progress, it works only when idle.
#include <SimpleRotary.h>
#include <EEPROM.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__

#endif

// Digital IO pin connected to the button. This will be driven with a
// pull-up resistor so the switch pulls the pin to ground momentarily.
// On a high -> low transition the button press logic will execute.
#define BUTTON_PIN   6
#define PIXEL_PIN    5  // Digital IO pin connected to the NeoPixels
#define PIXEL_COUNT 46  // Number of NeoPixels

SimpleRotary rotary(3,2,9);
int counter = 0;
int brightness = 22;
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

boolean oldState =HIGH;
int     mode     = 0;    // Currently-active animation mode, 0-9

void setup() {
   
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  strip.setBrightness(brightness);
  EEPROM.get(0, mode);
}

void loop() {
                      
  byte i; // 0 = not turning, 1 = CW, 2 = CCW
  i = rotary.rotate();
 
  if ( i == 1 ) {
    delay(20);
    brightness +=6;
    Serial.print("CW: ");
    Serial.println(brightness);
  }
  if ( i == 2 ) {
    delay(20);
    brightness -=6;
    Serial.print("CCW: ");
    Serial.println(brightness);   
  }
     if(brightness > 244){
      brightness = 244;
     }
     if(brightness < 0){
      brightness = 4;
     }  
     strip.setBrightness(brightness);
     strip.show();
 
 
  // Get current button state.
  boolean 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) { // Yes, still low
      if(++mode > 11) mode = 0;
      EEPROM.put(0, mode);
       // Advance to next mode, wrap around after #8
      switch(mode) {           // Start the new animation...
        case 0:
          colorWipe(strip.Color(  215,   110,   20), 20);    // Bronze BMW
          break;
        case 1:
          colorWipe(strip.Color(147,   26,   1), 20);    // Orange
          break;
        case 2:
          colorWipe(strip.Color(  254, 0,   0), 20);    // Red
          break;
        case 3:
          colorWipe(strip.Color(  255,   255,   0), 20);    // Yellow
          break;
        case 4:
          colorWipe(strip.Color(  66,   255,   0), 20);  // Yellow-green
          break;
        case 5:
          colorWipe(strip.Color(  0,   255,   0), 20); // Green
          break;
        case 6:
          colorWipe(strip.Color(  0,   255,   255), 20); // Cyan
          break;
        case 7:
          colorWipe(strip.Color(  21,   163,   199), 20); // Cyan-Blue
          break;
        case 8:
          colorWipe(strip.Color(  0,   0,   255), 20); // Blue
          break;
        case 9:
          colorWipe(strip.Color(  89,   0,   255), 20); // Pink
          break;
        case 10:
          colorWipe(strip.Color(  255,   0,   255), 20); // Magenta
          break;
        case 11:
          colorWipe(strip.Color(  229,   160,   29), 20); // Bronze
          break;
      
      }
    }
  }

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

// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}

Move your switch-case to a new function. Call that function from where the switch-case used to be. Also call the function from setup().

Alternatively, create an array so you don't need the switch-case at all:

const unsigned long myColours[12] = {
          strip.Color(  215,   110,   20),    // Bronze BMW
          strip.Color(147,   26,   1),    // Orange
          strip.Color(  254, 0,   0),    // Red
          strip.Color(  255,   255,   0),    // Yellow
          strip.Color(  66,   255,   0),  // Yellow-green
          strip.Color(  0,   255,   0), // Green
          strip.Color(  0,   255,   255), // Cyan
          strip.Color(  21,   163,   199), // Cyan-Blue
          strip.Color(  0,   0,   255), // Blue
          strip.Color(  89,   0,   255), // Pink
          strip.Color(  255,   0,   255), // Magenta
          strip.Color(  229,   160,   29) // Bronze
}

Then you don't need any new function, you can just use

colorWipe(myColours[mode], 20);

from setup() and loop().

PaulRB, thank you very much, your solution is a perfect fit of that what i needed. Working flawlessly now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.