Controlling Neopixels with a Button

I am working on a project using Neopixels from Adafruit and I am trying to control what they do based on a button press. Most of the program runs well until I want the Neopixels to change color to white. Normally, when the button is LOW or OFF, the Neopixels display in blue doing what they call a “colorWipe.” When you press the button, I have them turning red and have the animation moving faster–that works great. After holding in the button for 10 seconds, they are supposed to turn white and do the same animation as the red until the button goes LOW or turns OFF. They do turn white, but they get stuck in that loop, even after you lift your finger off the button. The display should go back to blue when you stop pressing the button. My code is below and I would appreciate any help anyone could give me figuring this out.

#include <Adafruit_NeoPixel.h>

// Define pin to connect Neopixels to
#define PIN 6

//Define pin for pushbutton to connect to
const int BUTTON = 2;

//Initialize value to determine button press HIGH or LOW
int val = 0;

//Initilize counter
long count = 0;

//Define variable to read millis()
unsigned long start;

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:

Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  pinMode(2, INPUT);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  
  val = digitalRead(BUTTON);
  
  unsigned long time;
  
  if (val == HIGH) {
    
    if (count == 0) {
      start = millis();
    }
    
    colorWipe2(strip.Color(139, 0, 0), 50); // Display all Neopixels as red
    clearStrip();
    count++;
    
    if ((millis() - start) >= 10000) {
      time = (millis() - start);
      do {
      colorWipe2(strip.Color(255, 255, 255), 50); // Display all Neopixels as white
      clearStrip();
      } while (val == HIGH && time >= 10000);
    }
  }
  
  else {
    colorWipe(strip.Color(0, 0, 255), 50); // Display all Neopixels as blue
    clearStrip();
  }
  
}

// Fill the dots one after the other with a color at speed 1
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(35);
  }
}

// Fill the dots one after the other with a color at speed 2
void colorWipe2(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(25);
  }
}

// Turn off all pixels
void clearStrip() {
  for( int i = 0; i<16; i++){
    strip.setPixelColor(i, 0x000000); strip.show();
  }
}

I would appreciate any help anyone could give me figuring this out.

It has already been suggested that you want to do something when the switch CHANGES state, not when the switch is IN some state. Quit ignoring advice you don't understand/like.

It has already been suggested that you get rid of EVERY call to delay. Quit ignoring advice you don't understand/like.

PaulS,

I don’t know who you think you are replying to, but I have viewed other posts on a similar topic, and I think you are referring to that other poster on this forum. I am a new user here and have never posted on this or any other topic. Frankly, I find your tone nasty and uncalled for. Also, if you’ll notice, no one has ever replied to this specific post and it is somewhat dissimilar to that other post you may be referring to. I am not IGNORING anything. I am not that other person and I request respectful and useful replies and questions.

Now, if anyone has something useful to add, I would appreciate the help. Thank you!

Here is your problem...

      do {
      colorWipe2(strip.Color(255, 255, 255), 50); // Display all Neopixels as white
      clearStrip();
      } while (val == HIGH && time >= 10000);

In the loop there is no code to change val or time, so it loops forever. Suggest you add

val = digitalRead(BUTTON);

to the loop. You probably don't need to check time.

Thanks David! Worked like a charm!

Is there any possability of posting the final code? Im looking to do something similar.