/*
Arduino | coding-help
reaction speed bug
Saturday, January 17, 2026 2:03 PM
soli
persisting issue with the buzzer not making any noise,
2nd white led not lighting up and the button respective to
that led not working at all, any help?
*/
const int NUM_BTNS = 2;
const int LED_PINS[] = {11, 10, 9, 8, 6};
const int BTN_PINS[] = {12, 5};
const int BUZZ_PIN = 7;
const unsigned long LED_DELAY = 1000;
bool ledState = false;
bool isDone = false;
int ledCount = 1;
int btnState[NUM_BTNS];
int oldBtnState[NUM_BTNS] = {1, 1};
unsigned long prevLEDTime = 0;
int checkButtons() {
int btnNumber = 0;
for (int i = 0; i < NUM_BTNS; i++) {
// check each button
btnState[i] = digitalRead(BTN_PINS[i]);
if (btnState[i] != oldBtnState[i]) { // if it changed
oldBtnState[i] = btnState[i]; // remember state for next time
if (btnState[i] == LOW) { // was just pressed
btnNumber = i + 1;
Serial.print("Button ");
Serial.print(btnNumber);
Serial.println(" pressed.");
} else { // was just released
// do release stuff
}
delay(20); // debounce
}
}
return btnNumber;
}
void cycleColors() {
if (millis() - prevLEDTime >= LED_DELAY) {
prevLEDTime = millis();
for (int i = 1; i < 4; i++) {
digitalWrite(LED_PINS[i], LOW);
}
//ledState = !ledState;
digitalWrite(LED_PINS[ledCount], HIGH);
//if (ledState) tone(BUZZ_PIN, 440, 100);
tone(BUZZ_PIN, 440, 100);
//if (!ledState) ledCount++;
ledCount++;
if (ledCount > 4) {
ledCount = 1;
for (int i = 0; i < 5; i++) {
digitalWrite(LED_PINS[i], LOW);
}
//digitalWrite(LED_PINS[0], LOW);
//digitalWrite(LED_PINS[4], LOW);
isDone = false;
}
}
}
void setup() {
Serial.begin(115200);
for (int i = 0; i < 5; i++) {
pinMode(LED_PINS[i], OUTPUT);
}
pinMode(BTN_PINS[0], INPUT_PULLUP);
pinMode(BTN_PINS[1], INPUT_PULLUP);
}
void loop() {
int btnNumber = checkButtons();
if (btnNumber == 1) {
digitalWrite(LED_PINS[4], HIGH);
} else if (btnNumber == 2) {
digitalWrite(LED_PINS[0], HIGH);
}
if (btnNumber) isDone = true;
if (isDone) cycleColors();
}
/*
while (buttonPressed == 0) {
digitalWrite(white1, HIGH);
digitalWrite(white2, HIGH);
if (digitalRead(button1) == 0) {
buttonPressed = 1;
digitalWrite(white2, LOW);
} else if (digitalRead(button2) == 0) {
buttonPressed = 1;
digitalWrite(white1, LOW);
}
}
delay(2000);
digitalWrite(white1, LOW);
digitalWrite(white2, LOW);
buttonPressed = 0;
*/