Problem changing mode with button on a neopixel strip while code is running.

The problem is that the code seems to be requiring a certain timing to change the mode. The code is a one way scanner off/red/blue, and when the button is pressed, nothing happens. If the button is pressed before the scanner reaches the end of the strip and held down until it starts again from the beginning, it will change the color for the next round. This is a problem for me. I need it to change when the button is pressed. It can start the scanner from the top, that's not a problem.

#include <Adafruit_NeoPixel.h>

#define PIXEL_PIN    6    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 144

    int val;                        // variable for reading the pin status
    int val2;                       // variable for reading the delayed status
    int buttonState;                // variable to hold the button state
    int Mode = 0;                   // What mode is the light in?
    int switchPin = 2;              // switch is connected to pin 2

    int wait_T=40; //This is the delay between moving back and forth and per pixel
    int refresh=400;
    int PixelCount=144; //Set this to the AMOUNT of Led's/Pixels you have or want to use on your strip And It can be used to tell where to Stop then return the eye at in the strip
    int Pixel_Start_End=0; //Set this to where you want it to Start/End at
    boolean UsingBar = false; //Set this to true If you are using the 8x1 Neopixel Bar Or you want to only use 3 leds for the scanner. 

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

    void setup() {
      pinMode(PIXEL_PIN, OUTPUT);
      pinMode(switchPin, INPUT);    // Set the switch pin as input
      strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  //Serial.begin(9600); //Used For pixel Count Debugging
      buttonState = digitalRead(switchPin);   // read the initial state
    }

    void loop(){
      val = digitalRead(switchPin);      // read input value and store it in val
      delay(10);                         // 10 milliseconds is a good amount of time
      val2 = digitalRead(switchPin);     // read the input again to check for bounces
      if (val == val2) {                 // make sure we got 2 consistant readings!
        if (val != buttonState) {          // the button state has changed!
          if (val == LOW) {                // check if the button is pressed
            if (Mode == 0) {          
              Mode = 1;               
            } else {
                if (Mode == 1) {        
                Mode = 2;           
            } else {
                if (Mode == 2) {      
                Mode = 0;           
            
            }
           }
          }
         }
        }
        buttonState = val;                 // save the new state in our variable
      }

      // Now do whatever the lightMode indicates
      if (Mode == 0) { 
       //Example: CylonEyeUp(Center_Dot_Color, Second_Dot_color, Third_Dot_color, wait_T, PixelCount, Pixel_Start_End);
  CylonEyeUp(strip.Color(0,0,0), strip.Color(0,0,0), strip.Color(0,0,0), wait_T, PixelCount, Pixel_Start_End);
  delay(wait_T);
  //Example: CylonEyeDown(Center_Dot_Color, Second_Dot_color, Third_Dot_color, wait_T, PixelCount, Pixel_Start_End);
  //CylonEyeDown(strip.Color(175,0,0), strip.Color(25,0,0), strip.Color(10,0,0), wait_T, PixelCount, Pixel_Start_End);
  //delay(wait_T);
  //Example: CylonEyeClear(wait_T, PixelCount, Pixel_Start_End);
  CylonEyeClear(wait_T, PixelCount, Pixel_Start_End);
  delay(refresh);
        
      }

      if (Mode == 1) { 
        //Example: CylonEyeUp(Center_Dot_Color, Second_Dot_color, Third_Dot_color, wait_T, PixelCount, Pixel_Start_End);
  CylonEyeUp(strip.Color(175,0,0), strip.Color(75,0,0), strip.Color(25,0,0), wait_T, PixelCount, Pixel_Start_End);
  delay(wait_T);
  //Example: CylonEyeDown(Center_Dot_Color, Second_Dot_color, Third_Dot_color, wait_T, PixelCount, Pixel_Start_End);
  //CylonEyeDown(strip.Color(175,0,0), strip.Color(25,0,0), strip.Color(10,0,0), wait_T, PixelCount, Pixel_Start_End);
  //delay(wait_T);
  //Example: CylonEyeClear(wait_T, PixelCount, Pixel_Start_End);
  CylonEyeClear(wait_T, PixelCount, Pixel_Start_End);
  delay(refresh);
        
      }

      if (Mode == 2) { 
        //Example: CylonEyeUp(Center_Dot_Color, Second_Dot_color, Third_Dot_color, wait_T, PixelCount, Pixel_Start_End);
  CylonEyeUp(strip.Color(0,0,175), strip.Color(0,0,75), strip.Color(0,0,25), wait_T, PixelCount, Pixel_Start_End);
  delay(wait_T);
  //Example: CylonEyeDown(Center_Dot_Color, Second_Dot_color, Third_Dot_color, wait_T, PixelCount, Pixel_Start_End);
  //CylonEyeDown(strip.Color(175,0,0), strip.Color(25,0,0), strip.Color(10,0,0), wait_T, PixelCount, Pixel_Start_End);
  //delay(wait_T);
  //Example: CylonEyeClear(wait_T, PixelCount, Pixel_Start_End);
  CylonEyeClear(wait_T, PixelCount, Pixel_Start_End);
  delay(refresh);
      
      }    
    }
    void CylonEyeClear(uint8_t Delay, int TotalPixels, int pStart) {
  for(int i=pStart; i<TotalPixels+2; i++) {
    strip.setPixelColor(i, strip.Color(0,0,0)); //Clears the dots
     strip.show();
    //Serial.println(i); //Used For pixel Count Debugging
    //delay(Delay);
  }
}
void CylonEyeUp(uint32_t Co, uint32_t Ct, uint32_t Ctt, uint8_t Delay, int TotalPixels, int pStart) {
  for(int i=pStart; i<TotalPixels; i++) {
   if(!UsingBar) { strip.setPixelColor(i+7, Ctt); } //Third Dot Color
    strip.setPixelColor(i+6, Ctt);   //Second Dot Color
    strip.setPixelColor(i+5, Ctt);   //Second Dot Color
    strip.setPixelColor(i+4, Ct);   //Second Dot Color
    strip.setPixelColor(i+3, Ct);   //Second Dot Color
    strip.setPixelColor(i+2, Ct);   //Second Dot Color
    strip.setPixelColor(i+1, Co);     //Center Dot Color
    strip.setPixelColor(i, Co);     //Center Dot Color
    strip.setPixelColor(i-1, Co);     //Center Dot Color
    strip.setPixelColor(i-2, Ct);   //Second Dot Color
    strip.setPixelColor(i-3, Ct);   //Second Dot Color
    strip.setPixelColor(i-4, Ct);   //Second Dot Color
    strip.setPixelColor(i-5, Ctt);   //Second Dot Color
    strip.setPixelColor(i-6, Ctt);   //Second Dot Color
    if(!UsingBar) { strip.setPixelColor(i-7, Ctt); } //Third Dot Color

    if(!UsingBar) {
      strip.setPixelColor(i-8, strip.Color(0,0,0)); //Clears the dots after the 3rd color
    } else {
      strip.setPixelColor(i-7, strip.Color(0,0,0)); //Clears the dots after the 2rd color
    }
    strip.show();
    //Serial.println(i); //Used For pixel Count Debugging
    delay(Delay);

  }
}
void CylonEyeDown(uint32_t Co, uint32_t Ct, uint32_t Ctt, uint8_t Delay, int TotalPixels, int pEnd) {
  for(int i=TotalPixels-1; i>pEnd; i--) {
     if(!UsingBar) { strip.setPixelColor(i+7, Ctt); } //Third Dot Color
    strip.setPixelColor(i+6, Ctt);   //Second Dot Color
    strip.setPixelColor(i+5, Ctt);   //Second Dot Color
    strip.setPixelColor(i+4, Ct);   //Second Dot Color
    strip.setPixelColor(i+3, Ct);   //Second Dot Color
    strip.setPixelColor(i+2, Ct);   //Second Dot Color
    strip.setPixelColor(i+1, Co);     //Center Dot Color
    strip.setPixelColor(i, Co);     //Center Dot Color
    strip.setPixelColor(i-1, Co);     //Center Dot Color
    strip.setPixelColor(i-2, Ct);   //Second Dot Color
    strip.setPixelColor(i-3, Ct);   //Second Dot Color
    strip.setPixelColor(i-4, Ct);   //Second Dot Color
    strip.setPixelColor(i-5, Ctt);   //Second Dot Color
    strip.setPixelColor(i-6, Ctt);   //Second Dot Color
    if(!UsingBar) { strip.setPixelColor(i-7, Ctt); } //Third Dot Color

    if(!UsingBar) {
      strip.setPixelColor(i-8, strip.Color(0,0,0)); //Clears the dots after the 3rd color
    } else {
      strip.setPixelColor(i-7, strip.Color(0,0,0)); //Clears the dots after the 2rd color
    }
    strip.show();
    //Serial.println(i); //Used For pixel Count Debugging
    delay(Delay);
  }
}

ButtonOneWay.ino (7.67 KB)

Yes its working but i mean something else,

Maybe my explanation is not so good, what I actually mean is, if I press the button, the withe leds must be flashes and when i unpress the button he must go further with red, blue yellow ect ect.

If i press the button now, hes first waiting till te last color has been light up (delay), but what ever the color is, red, blue or yellow, when i press the button he must directly flashing white color.

I hope this explanation is better, please let me know i men understand it.
Thanks alot for responsing.

Mars

Paul__B:
Go and read the instructions, then go back and modify your post to mark up the code as such so we can examine it comfortably and accurately.

Mars, what part of the above request did you not understand?

sorry excuse me, i have place my reaction in the wrong topic :frowning:

Paul__B:
Right, first things first.

Go and read the instructions, then go back and modify your post to mark up the code as such so we can examine it comfortably and accurately.

Sorry about that, was supposed to save draft, miss click :frowning:

I believe the Adafruit library is blocking so your push button timing is important.

LarryD:
I believe the Adafruit library is blocking so your push button timing is important.

How could I fix that?

Maybe use an interrupt pin but, I think the library disables interrupts also.

Thanks for fixing the code tags, Mars.

I think the problem is all the delay() use, and the way the code is structured. This is what blocks the detection of button presses.

LarryD:
Maybe use an interrupt pin but, I think the library disables interrupts also.

Looks like they are disabled, I didn't seem to get the interrupts to work :confused:

djdeeno:
sorry excuse me, I have place my reaction in the wrong topic :frowning:

You may delete it then. It only looks funny if someone has responded to it (sic.). You cannot delete the first post in a thread - it should be obvious why.