Left over artifacts in the low position

Hello all,

I am seeing a re-occuring issue with my NeoPixel project. I have it down to where when my switch is in the up position, I have a default pattern of LEDs that works fine. My issue comes around in the Low position area. To try and explain what is going here is my order of operation.

  1. re-uploaded my Arduino code. HIGH pattern works fine.
  2. I press on my trigger and the default value of 0 is applied to my LEDs and that works fine.
  3. I go into the Serial console and press 1 for a different reaction when the trigger is pressed down but the reaction for 0 plays out instead. I press the trigger again but this time the reaction for value 1 plays out from then and there on until I enter value 0 again.
  4. I went to the Serial console and submitted 0 and pressed my trigger down again. This time the reaction for reaction 1 plays out fully and then immediately following is part of the reaction for reaction 0. I press the trigger down again and only reaction 0 plays out.

sigh when I try to make a youtube video, it starts working more consistently. I run some more tests and the incorrect operation is still there, just not as frequently as I have seen it. I have adjusted nothing on my hardware either, no additional soldering, unplugging and re-plugging, nothing.

#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LEDS 30
// Parameter 1 = number of strip in strip
// Parameter 2 = pin number (most are valid)
// Parameter 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 strip, WS2811 drivers)
//   NEO_GRB     strip are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     strip are wired for RGB bitstream (v1 FLORA strip, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);
int delayval = 10; // delay for half a second
int reaction_selection = 0;
int reaction_1_red = 0;
int reaction_1_green = 0;
int reaction_1_blue = 0;
int reaction_1_white = 255;
int reaction_2_red_a = 100;
int reaction_2_green_a = 100;
int reaction_2_blue_a = 0;
int reaction_2_white_a = 0;
int reaction_2_red_b = 0;
int reaction_2_green_b = 0;
int reaction_2_blue_b = 100;
int reaction_2_white_b = 100;
int main_count = 0;
int main_count_limit = 4;
int minor_count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(7, INPUT_PULLUP);
  strip.begin();
  strip.show(); // Initialize all strip to 'off'
}

// *** REPLACE FROM HERE ***
void loop() {
// This prints out the reaction_selection variable.  Neat for troubleshooting.
//  delay(delayval);
  if (Serial.available() > 0)
  {
    reaction_selection = Serial.parseInt();
  }
  switch (digitalRead(7)) {
    case HIGH:
// The below code allows for the gentle glowing sequence that I enjoy so much.
// I would like to keep the below four lines commented out but still
// present for documentation purposes in how I got it to work in the first place.
//    void eatitloop() { 
//      RGBLoop();
//    }
//    void RGBLoop(){

// This does the over all count for the whole string of LEDs.  If the count is at 0 then red.  If 3 then white.
      for(int main_count = 0; main_count < 4; main_count++ ) {
// If the digitalRead value changes to pressed down, then it will goto the bailout section.
        if (digitalRead(7) == LOW) {
          goto bailout;
        }
// This is how color value goes from 0 to 254 (fade in).  As the colors get brighter, this value is going up.
        for(int main_fade_in = 0; main_fade_in < 256; main_fade_in++) {
//          The below If clause goes to the bailout section if true.
          if (digitalRead(7) == LOW) {
            goto bailout;
          }
          switch(main_count) {
            case 0: setAll(main_fade_in,0,0,0); break;
            case 1: setAll(0,main_fade_in,0,0); break;
            case 2: setAll(0,0,main_fade_in,0); break;
            case 3: setAll(0,0,0,main_fade_in); break;
          }
          showStrip();
          delay(0);
        }
// Fade OUT section. This is how color value goes from 254 to 0 (fade out).  As the colors get dimer, this value is going down.
        for(int main_fade_out = 255; main_fade_out >= 0; main_fade_out--) {
//          The below If clause goes to the bailout section if true.
          if (digitalRead(7) == LOW) {
            goto bailout;
          }
          switch(main_count) {
            case 0: setAll(main_fade_out,0,0,0); break;
            case 1: setAll(0,main_fade_out,0,0); break;
            case 2: setAll(0,0,main_fade_out,0); break;
            case 3: setAll(0,0,0,main_fade_out); break;
          }
          showStrip();
          delay(0);
        }
      }
      break;
// Keep the below commented out line for documentation as well.
//    }
    bailout:
    case LOW:
    if (reaction_selection == 0) {
// This FOR is what gives this reaction its reaction time.
      for(int reaction_1 = 0; reaction_1 < 256; reaction_1++) {
      int Pixel = random(NUM_LEDS);
      setPixel(Pixel,reaction_1_red,reaction_1_green,reaction_1_blue,reaction_1_white);
      showStrip();
      delay(50);
      setPixel(Pixel,reaction_1_red,reaction_1_green,reaction_1_blue,reaction_1_white);
      }
    }
    else if (reaction_selection == 1){
// This is part of how the green lights stay twinkling. do 20 cycles of chasing
      for (int j=0; j<20; j++) {
        for (int q=0; q < 3; q++) {
// This is how every third pixel is turned on.
// This section controls half of the colors that you see going around.  They have to be different than the values below to be effective.
          for (int i=0; i < NUM_LEDS; i=i+3) {
            setPixel(i+q, reaction_2_red_a,reaction_2_green_a,reaction_2_blue_a,reaction_2_white_a);    //turn every third pixel on
          }
          strip.show();
// This is the other part of how the lights stay on twinkling.
          delay(70);
// This section controls half of the colors that you see going around.  They have to be different than the values above to be effective.
          for (int i=0; i < NUM_LEDS; i=i+3) {
            setPixel(i+q, reaction_2_red_b,reaction_2_green_b,reaction_2_blue_b,reaction_2_white_b);        //turn every third pixel off
            strip.show();
            strip.begin();
          }
        }
      }
    }
  }
}
// *** REPLACE TO HERE ***

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue, byte white) {
 #ifdef ADAFRUIT_NEOPIXEL_H 
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue, white));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H 
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
   leds[Pixel].w = white;
 #endif
}

void setAll(byte red, byte green, byte blue, byte white) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue, white); 
  }
  showStrip();
}

Not going to jump in on your thread. But I have been having a similar issue using a toggle switch and button using "HIGH" and "LOW". So, I'll see how yours plays out. Maybe there will be some info I can use as well.

goto bailout;

me too

Nobody?

There's a hunk of hard to follow code there that would really benefit from being cut up into
smaller, well-named functions, each doing one thing...

There seems to be strip.show() used sometimes, showStrip() sometimes, I suspect they should all
be the same?

Show me what section you are talking about and I will definitely see if I can comment it up. Anything to help you help me and some other people. lol

Ahhhh... I see what you are talking about. "strip.show() and showStrip()" It took me a double take to see what you were talking about. I will make the adjustments to see if it works. Thank you.

Well, I solved my problem. I decided to redesign my code from bottom up. I looked in the NeoPixel example code and I found this sample called buttoncycler. I tried it out and it works way better than anything I coded. sigh Maybe I will just go back to playing with paper clips on my desk.

lonesoac0:
Well, I solved my problem. I decided to redesign my code from bottom up. I looked in the NeoPixel example code and I found this sample called buttoncycler. I tried it out and it works way better than anything I coded. sigh Maybe I will just go back to playing with paper clips on my desk.

I was having a similar issue with a toggle and button. Toggle was either in "run" or "off" (was only controlling an led and speaker). The button is a "safety" in case the toggle is bumped and accidentally turned on. After a restart, I could switch to off and then back to run. Light would flash and speaker would beep just like designed. Tap the button and light/beep stopped. Great! Then turn the switch back to the off position and the light would flash once and I would get a beep. NOT COOL! Was driving me nuts. Nowhere was it written to beep/flash then. No idea what was causing it.

I did some clean up and modified a few things here and there. And somewhere along the way, I accidentally fixed it. No idea. Completely clueless as to what it was or what I did. But it works as intended now.

I had hoped someone would have seen the issue in your code and that it would relate to mine somehow. But oh well.