const int numLeds = 8;
const int ledPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
const int buttonPins[] = {2, 3, 4, 5};
int ledStates[numLeds];
int buttonStates[4];
int lastButtonStates[4];
int tempo = 100;
enum Richting {
VOORWAARTS = 1,
ACHTERWAARTS = -1
};
Richting richting = VOORWAARTS;
bool lichtshow = false;
void setup() {
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
ledStates[i] = LOW; // Initialize each LED state to LOW
}
for (int i = 0; i < 4; i++) {
pinMode(buttonPins[i], INPUT);
lastButtonStates[i] = HIGH;
}
}
void toggleAllLeds() {
lichtshow = !lichtshow; // Toggle the light show state
while (lichtshow) {
for (int i = 0; i < numLeds; i++) {
ledStates[i] = !ledStates[i];
digitalWrite(ledPins[i], ledStates[i]);
delay(tempo);
}
if (richting == ACHTERWAARTS) {
for (int i = numLeds - 1; i >= 0; i--) {
ledStates[i] = !ledStates[i];
digitalWrite(ledPins[i], ledStates[i]);
delay(tempo);
}
}
}
// Turn off all LEDs when light show is stopped
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW);
}
}
void versnel() {
tempo = max(10, tempo - 25);
}
void vertraag() {
tempo = min(1000, tempo + 25);
}
void veranderRichting() {
richting = (richting == VOORWAARTS) ? ACHTERWAARTS : VOORWAARTS;
}
void handleButton(int buttonIndex, void (*action)()) {
int reading = digitalRead(buttonPins[buttonIndex]);
if (reading != lastButtonStates[buttonIndex]) {
delay(50); // Debouncing delay
if (reading != lastButtonStates[buttonIndex]) {
buttonStates[buttonIndex] = reading;
action();
}
}
lastButtonStates[buttonIndex] = reading;
}
void loop() {
handleButton(0, toggleAllLeds);
handleButton(1, versnel);
handleButton(2, vertraag);
handleButton(3, veranderRichting);
}