#include <stdlib.h> // Voor de random functie
const int ledPins[] = {13, 12, 11, 10, 9, 8, 7, 6}; // Array van LED pinnen
const int buttonPins[] = {5, 4, 3, 2}; // Array van knop pinnen
const int potPin = A1; // Potentiometer pin
int ledState[8] = {0}; // Array om LED statussen op te slaan
int animationState = 0; // Huidige animatie staat
unsigned long previousMillis = 0; // Tijd bijhouden voor animaties
bool animationRunning = false; // Vlag om aan te geven of een animatie bezig is
int animationType = 0; // 0: Geen animatie, 1: Sequentieel, 2: Binnen-Buiten, 3: Willekeurig
void setup() {
Serial.begin(9600); // Start seriële communicatie
for (int i = 0; i < 8; i++) {
pinMode(ledPins[i], OUTPUT); // Stel LED pinnen in als uitgang
}
for (int i = 0; i < 4; i++) {
pinMode(buttonPins[i], INPUT_PULLUP); // Stel knop pinnen in als input met interne pull-up weerstanden
}
}
void loop() {
int buttonState;
for (int i = 0; i < 4; i++) {
buttonState = digitalRead(buttonPins[i]);
if (buttonState == LOW) {
handleButtonPress(i); // Behandel knopdrukken
delay(400); // Debounce vertraging
}
}
if (animationRunning) {
unsigned long currentMillis = millis();
int intervalSpeed = map(analogRead(potPin), 0, 1023, 50, 1000); // Snelheid van animatie gebaseerd op potentiometer
if (currentMillis - previousMillis >= intervalSpeed) {
previousMillis = currentMillis;
switch (animationType) {
case 1:
animateSequential();
break;
case 2:
animateInsideOut();
break;
case 3:
randomLedOnOff();
break;
}
}
}
}
void handleButtonPress(int button) {
switch (button) {
case 0: // LED's toggelen
Serial.println("Knop 1 ingedrukt - LED's aan/uit toggelen");
animationRunning = false; // Stop elke lopende animatie
toggleLEDs();
break;
case 1: // Sequentiële animatie
Serial.println("Knop 2 ingedrukt - Sequentiële Animatie");
animationType = 1;
animationRunning = !animationRunning;
break;
case 2: // Binnen-Buiten animatie
Serial.println("Knop 3 ingedrukt - Binnen-Buiten Animatie");
animationType = 2;
animationRunning = !animationRunning;
break;
case 3: // Willekeurige LED aan/uit
Serial.println("Knop 4 ingedrukt - Willekeurige LED Aan/Uit");
animationType = 3;
animationRunning = !animationRunning;
break;
}
}
void toggleLEDs() {
for (int i = 0; i < 8; i++) {
ledState[i] = !ledState[i];
digitalWrite(ledPins[i], ledState[i]);
}
}
void animateSequential() {
digitalWrite(ledPins[animationState], LOW);
animationState = (animationState + 1) % 8;
digitalWrite(ledPins[animationState], HIGH);
}
void animateInsideOut() {
int insideOutOrder[] = {0, 7, 1, 6, 2, 5, 3, 4, 4, 3, 5, 2, 6, 1, 7, 0};
digitalWrite(ledPins[insideOutOrder[animationState]], LOW);
animationState = (animationState + 1) % 16;
digitalWrite(ledPins[insideOutOrder[animationState]], HIGH);
}
void randomLedOnOff() {
int randomLed = random(0, 8); // Selecteer willekeurige LED
digitalWrite(ledPins[randomLed], !digitalRead(ledPins[randomLed])); // Toggle LED status
}