Buttoncycler - rainbow stopping

Hello, I'm trying to make a buttoncycler code for my LED's, everything works fine, but the problem is with Rainbow loop, in buttoncycler Rainbow runs only one full cycle and that's it, but when I launch Rainbow as separate it works fine without stopping, I just don't know what could be the problem.

THE CODE:

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
 #include <avr/power.h> 
#endif

#define BUTTON_PIN   5
#define PIXEL_PIN    8
#define PIXEL_COUNT 16  

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_RGB + NEO_KHZ800);

boolean oldState = HIGH;
int     mode     = 0;    

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  strip.show();  // Initialize all pixels to 'off'
}

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


  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 > 5) mode = 0; // Advance to next mode, wrap around after #8
      switch(mode) {           // Start the new animation...
        case 0:
          colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
          break;
        case 1:
          colorWipe(strip.Color(255,   0,   0), 50);    // Red
          break;
        case 2:
          colorWipe(strip.Color(  0, 255,   0), 50);    // Green
          break;
        case 3:
          colorWipe(strip.Color(  0,   0, 255), 50);    // Blue
          break;
        case 4:
          colorWipe(strip.Color(127, 127, 127), 50); // White
          break;
        case 5:
          rainbow(55);
          break;

      }
    }
  }

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

}

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

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+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);
  }
}

You never update oldState.

a7

If you edited the original code you posted, don't do that anymore, it makes the thread a bit confusing.

Please post the code you are running, in a new message in this thread right here.

Ppl are loathe to waste time looking at your code, only to find you've bungled posting it. I have no confidence that you left other thing behind.

Autoformat your code, select and Copy for Forum the entire thing using those tools in the IDE. Then come here and paste it.

TIA

a7

I can't run your code but it looks like each button press moves to the next effect, which effect is then done once.

If you want the effect chosen by the number to run over and over, separate the logic that reads and acts on the switch (increment/rollover) from the switch case that runs the current animation.

Now you have the switch inside that button logic, so it only runs anything once.

You can just cut the while switch statement with all its cases and place it right before you do that update of oldState.

loop
     handle button

     run an animation

Added: the effects 0 - 4 are lame as they just paint all the pixels - repeating them doesn't change the LEDs.

Effect 5 needs to run over and over, my suggestion will do that.

And (but) you will notice your program goes blind to the button whilst running effect 5, as that takes 15 seconds or so, the button only gets checked every 15 seconds (as was that case already), so that's lame and...

You'll have to do things differently if you want 1) meaningful effects over and over cases 0 - 5 ands 2) good rapid response to the "change" button.

BTW you could fix it to wrap after 5 instead of 8, since you only have 6 real effects you have to pushbutton your way through some "no effect" animations.

a7

Ok, thanks I'll try that

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