// Schakelaars
const int switchPins[4][2] = {
{2, 3}, // Switch 1
{4, 5}, // Switch 2
{6, 7}, // Switch 3
{8, 9} // Switch 4
};
const int outputLed = 13; // LED 5 (groen)
const int buttonPin = 12; // Drukknop
// RGB LED pinnen (PWM pins en digitale pins voor LED3 en LED4)
const int ledPins[4][3] = {
{3, 5, 6}, // LED 1 PWM pins
{9, 10, 11}, // LED 2 PWM pins
{14, 15, 16}, // LED 3 digitale pinnen (A0, A1, A2)
{17, 18, 19} // LED 4 digitale pinnen (A3, A4, A5)
};
// Kleurparen per LED (2 opties per LED)
const int colorOptions[4][2][3] = {
{ {0, 255, 0}, {255, 0, 0} }, // LED 1: groen = rechts, rood = links
{ {0, 0, 255}, {255, 255, 0} }, // LED 2: blauw = rechts, geel = links
{ {0, 255, 255}, {255, 0, 255} }, // LED 3: cyaan = rechts, magenta = links
{ {255, 255, 255}, {255, 128, 0}} // LED 4: wit = rechts, oranje = links
};
// Wat is de juiste stand per kleur? 0 = links, 1 = rechts
const int correctSide[4][2] = {
{1, 0}, // LED 1: groen = rechts, rood = links
{1, 0}, // LED 2
{1, 0}, // LED 3
{1, 0} // LED 4
};
// Actieve kleur per LED (gekozen bij opstart)
int currentColor[4];
bool blinkingDone = false;
bool blinkingInProgress = false;
bool buttonPressed = false;
void setup() {
// Setup pinnen voor schakelaars
for (int i = 0; i < 4; i++) {
pinMode(switchPins[i][0], INPUT_PULLUP);
pinMode(switchPins[i][1], INPUT_PULLUP);
for (int j = 0; j < 3; j++) {
pinMode(ledPins[i][j], OUTPUT);
}
}
pinMode(outputLed, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
// Willekeurige kleuren kiezen bij opstart
randomSeed(analogRead(0));
for (int i = 0; i < 4; i++) {
currentColor[i] = random(0, 2); // 0 of 1
}
}
void loop() {
bool allCorrect = true;
for (int i = 0; i < 4; i++) {
int side = currentColor[i];
setColor(i, colorOptions[i][side]);
bool left = digitalRead(switchPins[i][0]) == LOW;
bool right = digitalRead(switchPins[i][1]) == LOW;
int userPosition = -1;
if (left && !right) userPosition = 0;
else if (!left && right) userPosition = 1;
if (userPosition != correctSide[i][side]) {
allCorrect = false;
}
}
if (allCorrect) {
if (!blinkingDone && !blinkingInProgress) {
blinkingInProgress = true;
blinkLed5();
blinkingDone = true;
blinkingInProgress = false;
}
// Check drukknop na knipperen
if (blinkingDone && digitalRead(buttonPin) == LOW && !buttonPressed) {
buttonPressed = true;
}
if (buttonPressed) {
digitalWrite(outputLed, HIGH); // LED 5 aan
} else {
digitalWrite(outputLed, LOW); // Wacht op knop
}
} else {
digitalWrite(outputLed, LOW); // Fout? LED 5 uit
}
}
void setColor(int ledIndex, const int color[3]) {
for (int c = 0; c < 3; c++) {
int pin = ledPins[ledIndex][c];
// PWM pins op Uno: 3,5,6,9,10,11
if (pin == 3 || pin == 5 || pin == 6 || pin == 9 || pin == 10 || pin == 11) {
analogWrite(pin, color[c]);
} else {
digitalWrite(pin, color[c] > 127 ? HIGH : LOW);
}
}
}
void blinkLed5() {
for (int i = 0; i < 5; i++) {
digitalWrite(outputLed, HIGH);
delay(300);
digitalWrite(outputLed, LOW);
delay(300);
}
}