Hello, I'm currently working on a school project using a neopixel strip to light up an interactive map of Lewis' and Clark's expedition path. I don't know much about arduino or C++ so I had a friend come help me program and do all the coding. Everything works well, except that when you press the button, there is a long delay before it does anything the next time you press the button. Or in other words, if you press the button, it will light up and do it's thing, but then if you try and click the button again to move onto the next light sequence, it won't do anything, and you have to spam press the button a bunch of times before it works. Does anyone have any idea why this could be? I don't know what I'm doing very well, so explaining it simply or just revising the code for me would be greatly appreciated!
#include <Keyboard.h>
#include <Adafruit_NeoPixel.h>
#define LED_PIN 3
#define LED_COUNT 100
#define BUTTON_PIN 2
int clickCount = 0;
bool buttonWasPressed = false;
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600);
strip.begin();
strip.clear();
strip.show();
strip.setBrightness(250); // adjust brightness
}
void loop() {
int btn = digitalRead(BUTTON_PIN);
// Detect button press (LOW means pressed)
if (btn == LOW && !buttonWasPressed) {
buttonWasPressed = true;
clickCount++;
Serial.print("Click count: ");
Serial.println(clickCount);
delay(10);
}
if (btn == HIGH) {
buttonWasPressed = false; // reset when released
}
// Run different loops depending on clickCount
if (clickCount == 0) {
// Strip stays off
strip.clear();
strip.show();
}
else if (clickCount == 1) {
// Example: light pixel 9 red
strip.clear();
strip.setPixelColor(9, strip.Color(255, 0, 0));
strip.show();
}
else if (clickCount == 2) {
// Example: chase from pixel 14 down to 0
for (int i = 8; i >= 3; i--) {
strip.setPixelColor(i, strip.Color(0, 255, 0));
strip.show();
delay(500);
}
strip.setPixelColor(2, strip.Color(255, 0, 0));
strip.show();
}
else if (clickCount == 3) {
// Example: fill pixels 0–29 one at a time
for (int i = 1; i >= 0; i--) {
strip.setPixelColor(i, strip.Color(0, 255, 0));
strip.show();
delay(500);
}
for (int i = 34; i<= 37; i++) {
strip.setPixelColor(i, strip.Color(0, 255, 0));
strip.show();
delay(500);
}
strip.setPixelColor(38, strip.Color(255, 0, 0));
strip.show();
}else if (clickCount == 4){
for (int i = 39; i<= 46; i++) {
strip.setPixelColor(i, strip.Color(0, 255, 0));
strip.show();
delay(500);
}
strip.setPixelColor(47, strip.Color(255, 0, 0));
strip.show();
} else if (clickCount == 5){
for (int i = 48; i<= 51; i++) {
strip.setPixelColor(i, strip.Color(0, 255, 0));
strip.show();
delay(500);
}
strip.setPixelColor(52, strip.Color(255, 0, 0));
strip.show();
}else if (clickCount==6){
for (int i = 53; i<= 61; i++) {
strip.setPixelColor(i, strip.Color(0, 255, 0));
strip.show();
delay(500);
}
strip.setPixelColor(62, strip.Color(255, 0, 0));
strip.show();
}else if(clickCount==7){
strip.clear();
for (int i = 62; i>= 34; i--) {
strip.setPixelColor(i, strip.Color(0, 0, 255));
strip.show();
delay(500);
} for (int i =0; i <=9; i++){
strip.setPixelColor(i, strip.Color(0, 0, 255));
strip.show();
delay(500);
}
clickCount ++;
}
else{
void(* resetFunc) (void) = 0; // declare reset function at address 0
int clickCount = 0;
bool buttonWasPressed = false;
resetFunc();
// call it → restart
}
}

