NEOPIXEL BREAK FOR LOOP

Hi All,

Once again i’m stuck with a piece of code. I am building a mood lamp that cycles through different animations depending on a switchcounter value. My posted code works as expected, with good response time from the switch. My problem is i cannot get the “rgbFadeInAndOut” function to work properly, it just jumps to the first RGB colour and keeps repeating that colour also it does not exit the while loop when i press the switch. Any help or advise is always welcome.

#include <Adafruit_NeoPixel.h>
//#include <EEPROM.h>
int Panel_Switch = 2;
volatile int Switchcounter = 0 ;
int NumPixels = 12;
int CNT = 12;
//int address = 0;
int ledFadeTime = 5;
unsigned long button_time = 0;
unsigned long last_button_time = 0;
const unsigned long debounceTime = 25;
#define PIN 6
#define DELAY_MULTIPLER 100
Adafruit_NeoPixel strip = Adafruit_NeoPixel(CNT, PIN, NEO_GRB + NEO_KHZ800);


void setup() {

  pinMode (Panel_Switch, INPUT);
  attachInterrupt(0, TAG, RISING);
  pinMode(Panel_Switch, INPUT);
  strip.begin();
  strip.show();
  // Switchcounter = EEPROM.read(address);

}

void loop() {

  if (Switchcounter == 0 ) {
    blackout();
  }
  if (Switchcounter == 1 ) {
     rgbFadeInAndOut(152, 245, 255, ledFadeTime);
    rgbFadeInAndOut(0, 191, 255, ledFadeTime);
    rgbFadeInAndOut(30, 144, 255, ledFadeTime);
    rgbFadeInAndOut(0, 100, 0, ledFadeTime);
    rgbFadeInAndOut(34, 139, 34, ledFadeTime);
    rgbFadeInAndOut(124, 252, 0, ledFadeTime);

  }
  if (Switchcounter == 2 ) {
    RED_FLAME ();
  }
  
  }
  
  void TAG() {

  button_time = millis();
  if (button_time - last_button_time > debounceTime) {
    Switchcounter++;
    //EEPROM.write(address,Switchcounter);
    last_button_time = button_time;
    if (Switchcounter > 2) {
      Switchcounter = 0;

    }
  }
}

void blackout() {

  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);
    strip.show();
    delay(300);
  }
}

void rgbFadeInAndOut(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait) {
  boolean state = true;
  while (state == true) {

    for (uint8_t b = 50; b < 255; b++) {

      for (uint8_t i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);
        byte switchState = digitalRead (Panel_Switch);
        if (switchState == HIGH) {
          state = false;
          return;

        }
      }

      strip.show();
      delay(wait);
    }

    for (uint8_t b = 255; b > 50; b--) {
      for (uint8_t i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);

      }

      strip.show();
      delay(wait);

    }
  }
}

void RED_FLAME () {

  boolean state = true;
  while (state == true) {
    //  Regular (golden) flame:
    int r = 255, g = 120, b = 0;
    for (int i = 0; i < strip.numPixels(); i++) {
      int flicker = random(0, 100);
      int r1 = r - flicker;
      int g1 = g - flicker;
      int b1 = b - flicker;
      if (g1 < 0) g1 = 0;
      if (r1 < 0) r1 = 0;
      if (b1 < 0) b1 = 0;
      strip.setPixelColor(i, r1, g1, b1);
      byte switchState = digitalRead (Panel_Switch);

      if (switchState == HIGH) {
        state = false;
        return;

      }
    }
    strip.show();
    delay(random(10, 153));
  }
}

What is the point of the boolean, state, in rgbFadeInAndOut? If you discover that the switch is pressed, you set the boolean to false, and return.

The boolean is used in a while loop, that will only end when the switch is pressed. I don't think that whole while loop is what you intended.

Why do you check the switch only in the first nested for loop, and not in the second one?

Hi Paul and thank you for your time, my thinking was that using the while loop would exit the fadeinout routine as it does in the other routines. I have spent a lot of time trying to sort this out but just can't get it. My thinking is that it's the while loop that is the problem as it gets stuck on the first RGB color and doesn't continue to the next colors. I tried checking the switch in both for loops but it didn't work.

Hi Paul.

Yippee the code now works as intended. Thank You.

#include <Adafruit_NeoPixel.h>
//#include <EEPROM.h>
int Panel_Switch = 2;
volatile int Switchcounter = 0 ;
int NumPixels = 12;
int CNT = 12;
//int address = 0;
int ledFadeTime = 5;
unsigned long button_time = 0;
unsigned long last_button_time = 0;
const unsigned long debounceTime = 25;
#define PIN 6
#define DELAY_MULTIPLER 100
Adafruit_NeoPixel strip = Adafruit_NeoPixel(CNT, PIN, NEO_GRB + NEO_KHZ800);


void setup() {

  pinMode (Panel_Switch, INPUT);
  attachInterrupt(0, TAG, RISING);
  pinMode(Panel_Switch, INPUT);
  strip.begin();
  strip.show();
  // Switchcounter = EEPROM.read(address);

}

void loop() {

  if (Switchcounter == 0 ) {
    blackout();
  }
  if (Switchcounter == 1 ) {
     RED_FLAME ();
    

  }
  if (Switchcounter == 2 ) {
   rgbFadeInAndOut(152, 245, 255, ledFadeTime);
    rgbFadeInAndOut(0, 191, 255, ledFadeTime);
    rgbFadeInAndOut(30, 144, 255, ledFadeTime);
    rgbFadeInAndOut(0, 100, 0, ledFadeTime);
    rgbFadeInAndOut(34, 139, 34, ledFadeTime);
    rgbFadeInAndOut(124, 252, 0, ledFadeTime);
  }

}

void TAG() {

  button_time = millis();
  if (button_time - last_button_time > debounceTime) {
    Switchcounter++;
    //EEPROM.write(address,Switchcounter);
    last_button_time = button_time;
    if (Switchcounter > 2) {
      Switchcounter = 0;

    }
  }
}

void blackout() {

  for (int i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);
    strip.show();
    delay(300);
  }
}

void rgbFadeInAndOut(uint8_t red, uint8_t green, uint8_t blue, uint8_t wait) {
  
   for (uint8_t b = 50; b < 255; b++) {
    for (uint8_t i = 0; i < strip.numPixels(); i++) {
     strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);

    }
    
    byte switchState = digitalRead (Panel_Switch);
     if (switchState == HIGH) {
      return;
    }

    strip.show();
    delay(wait);
  }

  for (uint8_t b = 255; b > 50; b--) {
    for (uint8_t i = 0; i < strip.numPixels(); i++) {
      
      strip.setPixelColor(i, red * b / 255, green * b / 255, blue * b / 255);

    }
     byte switchState = digitalRead (Panel_Switch);
      if (switchState == HIGH) {
       return;
    }
    strip.show();
    delay(wait);

  }
   }


void RED_FLAME () {

  boolean state = true;
  while (state == true) {
    //  Regular (golden) flame:
    int r = 255, g = 120, b = 0;
    for (int i = 0; i < strip.numPixels(); i++) {
      int flicker = random(0, 100);
      int r1 = r - flicker;
      int g1 = g - flicker;
      int b1 = b - flicker;
      if (g1 < 0) g1 = 0;
      if (r1 < 0) r1 = 0;
      if (b1 < 0) b1 = 0;
      strip.setPixelColor(i, r1, g1, b1);
      byte switchState = digitalRead (Panel_Switch);

      if (switchState == HIGH) {
        state = false;
        return;

      }
    }
    strip.show();
    delay(random(10, 153));
  }
}