#include <stdlib.h> // Voor de random functie
const int ledPins[] = {13, 12, 11, 10, 9, 8, 7, 6}; // Array van LED pinnen
const int buttonPin = 4; // Knop pin
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: Alle Aan, 2: Sequentieel, 3: Binnen-Buiten, 4: Willekeurig, 5: Golf, 6: Willekeurig Knipperen, 7: Afwisselend, 8: Knipperen
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
digitalWrite(ledPins[i], LOW); // Zet alle LED's uit bij opstarten
}
pinMode(buttonPin, INPUT_PULLUP); // Stel knop pin in als input met interne pull-up weerstand
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
handleButtonPress(); // Behandel knopdrukken
delay(300); // 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:
allOn();
break;
case 2:
animateSequential();
break;
case 3:
animateInsideOut();
break;
case 4:
randomLedOnOff();
break;
case 5:
animateRandomChase();
break;
case 6:
animateRandomBlink();
break;
case 7:
animateAlternate();
break;
case 8:
animateBlink();
break;
}
}
}
}
// Behandel knopdrukken
void handleButtonPress() {
animationType = (animationType + 1) % 9;
animationRunning = animationType != 0;
if (!animationRunning) {
allOff(); // Schakel alle LED's uit
}
Serial.print("Animatie ");
Serial.print(animationType);
Serial.print("/8: ");
switch (animationType) {
case 0:
Serial.println("Uit");
break;
case 1:
Serial.println("Alle Aan");
break;
case 2:
Serial.println("Sequentieel");
break;
case 3:
Serial.println("Binnen-Buiten");
break;
case 4:
Serial.println("Willekeurig Aan/Uit");
break;
case 5:
Serial.println("Random Chase");
break;
case 6:
Serial.println("Willekeurig Knipperen");
break;
case 7:
Serial.println("Afwisselend");
break;
case 8:
Serial.println("Knipperen");
break;
}
}
// Zet alle LEDs aan
void allOn() {
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], HIGH);
}
}
// Sequentieel
// Deze animatie zet de LEDs één voor één aan en uit in een volgorde.
// Volgorde waarin LEDs worden geschakeld: 13-12-11-10-9-8-7-6-13-12-11-10-9-8-7-6 (herhaalt).
void animateSequential() {
digitalWrite(ledPins[animationState], LOW);
// Verhoog de animatiestaat en zet de volgende LED aan
animationState = (animationState + 1) % 8;
digitalWrite(ledPins[animationState], HIGH);
}
// Binnen-Buiten
// Bij deze animatie gaan de LEDs van binnen naar buiten en weer terug.
// Volgorde waarin LEDs worden geschakeld: 13-6-12-7-11-8-10-9-9-10-8-11-7-12-6-13 (herhaalt).
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);
}
// Willekeurig Aan/Uit
// De LEDs worden willekeurig aan of uit geschakeld.
// De volgorde kan verschillen bij elke uitvoering.
// Willekeurig LED aan/uit
void randomLedOnOff() {
int randomLed = random(0, 8);
digitalWrite(ledPins[randomLed], !digitalRead(ledPins[randomLed]));
}
// Random Chase Animation
// LEDs light up in a random sequence, creating an unpredictable effect.
void animateRandomChase() {
static int previousLed = -1; // Variable to track the previously lit LED
// Turn off the previously lit LED
if (previousLed != -1) {
digitalWrite(ledPins[previousLed], LOW);
}
// Generate a random LED index
int randomLed = random(0, 8);
// Turn on the randomly selected LED
digitalWrite(ledPins[randomLed], HIGH);
// Update the previous LED index
previousLed = randomLed;
}
// Willekeurig Knipperen
// De LEDs knipperen willekeurig aan en uit.
// De volgorde kan verschillen bij elke uitvoering.
void animateRandomBlink() {
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], random(0, 2)); // Willekeurig elke LED aan of uit zetten
}
}
// Afwisselend Knipperen
// Deze animatie laat de LEDs afwisselend aan en uit gaan.
// Volgorde waarin LEDs worden geschakeld: 13-12-13-12-13-12-13-12 (herhaalt).
void animateAlternate() {
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], i % 2 == animationState % 2);
}
animationState = (animationState + 1) % 2;
}
// Knipperen
// De LEDs knipperen allemaal tegelijk aan en uit.
// De volgorde van schakelen is synchroon voor alle LEDs.
void animateBlink() {
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], animationState);
}
animationState = !animationState;
}
// Zet alle LEDs uit
void allOff() {
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], LOW);
}
}