/*
Forum: https://forum.arduino.cc/t/another-millis-question/1445678
Wokwi: https://wokwi.com/projects/465177114393767937
2026/05/27
ec2021
This example shows how to use arrays to handle
several switches and leds with for-loops
for easier maintenance of the sketch
Debouncing of the switches and the button is still missing;
it's not required for this application though it would be
cleaner.
The switches and the button have been changed to
use INPUT_PULLUP and the leds have been arranged for
easier wiring in opposite order compared to the original
sketch.
*/
constexpr byte ledPin[] = { 8, 7, 6, 5, 4, 3};
constexpr byte levelPin[] = {A0, A1, A2, A3, A4, A5};
constexpr int noOfPins = sizeof(ledPin) / sizeof(ledPin[0]);
constexpr byte Buzzer = 2;
constexpr byte Button = 9;
constexpr unsigned long wantedPeriod = 4000;
byte BuzzerState;
unsigned long startMillis;
unsigned long period = 0;
void setup() {
for (int i = 0; i < noOfPins; i++) {
pinMode(ledPin[i], OUTPUT);
pinMode(levelPin[i], INPUT_PULLUP);
}
pinMode(Button, INPUT_PULLUP);
pinMode(Buzzer, OUTPUT);
}
void loop() {
BuzzerState = LOW;
for (int i = 0; i < noOfPins; i++) {
if (!digitalRead(levelPin[i])) {
digitalWrite(ledPin[i], HIGH);
BuzzerState = HIGH;
} else {
digitalWrite(ledPin[i], LOW);
}
}
if (!digitalRead(Button)) {
startMillis = millis();
period = wantedPeriod;
}
if (millis() - startMillis < period) {
BuzzerState = LOW;
}
digitalWrite(Buzzer, BuzzerState);
}