#include <Adafruit_NeoPixel.h>
#define LED_PIN 2
#define BUZZER_PIN 3
#define TOTAL_LEDS 30
#define NUM_ROWS 10
#define DEBOUNCE_DELAY 100
#define BUTTON_PIN A5
Adafruit_NeoPixel strip(TOTAL_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
int correctPattern[NUM_ROWS], currentRow = 0;
bool gameOver = false;
const int buttonRanges[3][2] = {
{975, 1023}, // Knop 1 bereik
{800, 975}, // Knop 2 bereik
{200, 800} // Knop 3 bereik
};
void setup() {
Serial.begin(9600);
strip.begin();
strip.show();
pinMode(BUZZER_PIN, OUTPUT);
randomSeed(analogRead(A0));
playTone(700, 200); playTone(900, 200);
generatePattern();
showPattern(5000);
}
void loop() {
if (gameOver) return;
int guess = getUserInput();
if (guess == correctPattern[currentRow]) {
strip.setPixelColor(getLedIndex(currentRow, guess), strip.Color(0, 255, 0));
strip.show();
playTone(1000, 200);
if (++currentRow == NUM_ROWS) showWin();
}
else {
strip.setPixelColor(getLedIndex(currentRow, guess), strip.Color(255, 0, 0));
strip.show();
playTone(300, 500);
delay(1000);
currentRow = 0;
showPattern(3000);
}
}
int getUserInput() {
int buttonValue;
do {
buttonValue = analogRead(BUTTON_PIN);
Serial.print("Button Voltage: ");
Serial.println(buttonValue);
for (int i = 0; i < 3; i++) {
if (buttonValue >= buttonRanges[i][0] && buttonValue <= buttonRanges[i][1]) {
waitForRelease();
return i;
}
}
} while (true);
}
void waitForRelease() {
while (analogRead(BUTTON_PIN) > buttonRanges[2][0]);
delay(DEBOUNCE_DELAY);
}
void generatePattern() {
for (int i = 0; i < NUM_ROWS; i++)
correctPattern[i] = random(3);
}
void showPattern(int duration) {
strip.clear();
for (int i = 0; i < NUM_ROWS; i++)
strip.setPixelColor(getLedIndex(i, correctPattern[i]), strip.Color(0, 0, 255));
strip.show();
delay(duration);
strip.clear(); strip.show();
}
void showWin() {
for (int wave = 0; wave < 5; wave++) {
for (int row = 0; row < NUM_ROWS; row++) {
for (int col = 0; col < 3; col++)
strip.setPixelColor(getLedIndex(row, col), strip.Color(0, 255, 0));
strip.show();
playTone(500 + row * 100, 50);
delay(100);
}
strip.clear(); strip.show();
}
gameOver = true;
}
int getLedIndex(int row, int col) {
return (col == 0) ? row : (col == 1) ? 19 - row : 20 + row;
}
void playTone(int frequency, int duration) {
tone(BUZZER_PIN, frequency, duration);
delay(duration);
}