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();
}
}