// https://forum.arduino.cc/t/delay-help-with-neopixel-strip/1414520
#include <Adafruit_NeoPixel.h>
#define LED_PIN 3
#define LED_COUNT 100
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
#define BUTTON_PIN 2
int clickCount = 0, start, stop, added;
bool currentButtonRead, lastButtonRead, currentButtonState, done = 0;
int index;
unsigned long buttonTimer, buttonTimeout = 50;
unsigned long caseTimer, caseTimeout = 50;
int casePixels[][3] = { // [row][col = 3]
{9, 9, 9}, // case 1 {start pixel, end pixel, added pixel}
{8, 3, 2}, // case 2
{29, 1, 0}, // case 3a
{34, 37, 38}, // case 3b
{39, 46, 47}, // casae 4a
{48, 51, 52}, // case 4b
{53, 61, 62}, // case 5
{62, 34, 34}, // case 6
{29, 1, 0} // case 7
};
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP); // configure button pin
Serial.begin(115200); // Serial communications
strip.begin();
strip.setBrightness(0); // adjust brightness
strip.clear(); // clear buffer
strip.show(); // display buffer
strip.setBrightness(255); // adjust brightness
Serial.print("Click count: ");
Serial.println(clickCount);
}
void loop() {
showStrip(); // Adafruit_NeoPixel library calls interfere with button reading
readButton(); // button reading routine
}
void showStrip() {
switch (clickCount) { // count the button presses
case 0: // starting value, clear pixels
strip.clear();
break;
case 1: // light pixel 9
lightPixels();
// strip.setPixelColor(9, strip.Color(255, 0, 0)); // red
break;
case 2:
done = 0;
lightPixels();
break;
case 3: // fill pixels 34 - 38 one at a time
lightPixels();
break;
case 4:
lightPixels();
break;
case 5:
lightPixels();
break;
case 6:
lightPixels();
break;
case 7:
lightPixels();
break;
default:
break;
}
}
void lightPixels() {
start = casePixels[clickCount - 1][0];
stop = casePixels[clickCount - 1][1];
added = casePixels[clickCount - 1][2];
if ((millis() - caseTimer > caseTimeout) && !done) { // next pixel lighting time
Serial.print(start);
Serial.print(",");
Serial.print(stop);
Serial.print(",");
Serial.println(added);
caseTimer = millis(); // reset timer
strip.setPixelColor(start + index, strip.Color(0, 255, 0)); // green
strip.show(); // display the pixels
if (stop - start > 0) // increasing pixel index
index++; // increment counter
else if (stop - start < 0) // decreasing pixel index
index--; // decrement counter
if (start + index == stop) { // at end of pixel count
index = stop;
done = 1; // do not re-enter
}
strip.show();
}
strip.setPixelColor(added, strip.Color(255, 0, 0)); // red
strip.show();
}
void readButton() {
bool currentButtonRead = digitalRead(BUTTON_PIN); // read button pin
if (currentButtonRead != lastButtonRead) { // if button pin changes...
buttonTimer = millis(); // ...start a timer
lastButtonRead = currentButtonRead; // ... and store current state
}
if ((millis() - buttonTimer) > buttonTimeout) { // if button change was longer than debounce timeout
if (currentButtonState == HIGH && lastButtonRead == LOW) { // ... and State is "NOT pressed" while Button "IS PRESSED"
digitalWrite(LED_BUILTIN, HIGH); // The button was pressed...
clickCount++; // increment count
if (clickCount == 8) // check clickCount
clickCount = 0; // reset clickCount
Serial.print("Click count: ");
Serial.println(clickCount);
digitalWrite(LED_BUILTIN, LOW); // "L" LED off
}
currentButtonState = currentButtonRead; // update button state
}
}0
10
20
30
40
50
60
70
80
90
9
19
29
39
49
59
69
79
89
99