int blue = 2;
int yellow = 3;
int green = 4;
int red = 5;
int ledpins[] = {blue, yellow, green, red};
int buzz = 10;
int blue_tone = 200;
int yellow_tone = 400;
int green_tone = 600;
int red_tone = 800;
int tones[] = {blue_tone, yellow_tone, green_tone, red_tone};
int buttonb = 6;
int buttony = 7;
int buttong = 8;
int buttonr = 9;
int buttons[] = {buttonb, buttony, buttong, buttonr};
int sequence[10] = {0};
int current_input = 0;
int startbutton = 11;
bool game_started = false;
void setup() {
Serial.begin(9600); // initialize serial communication
for (int i = 0; i < 4; i++) {
pinMode(ledpins[i], OUTPUT);
pinMode(buttons[i], INPUT);
}
pinMode(startbutton, INPUT);
pinMode(buzz, OUTPUT);
randomSeed(analogRead(A0));
}
void startup_sequence(int leds, int tonez)
{
digitalWrite(ledpins[leds], HIGH);
tone(buzz, tonez);
delay(500);
digitalWrite(ledpins[leds], LOW);
noTone(buzz);
delay(100);
}
void my_seq() {
if (current_input == 1) {
// if this is the first color in the sequence, play the full sequence
for (int i = 0; i < current_input; i++)
{
int led = sequence[i];
int tonez = tones[led];
startup_sequence(led, tonez);
delay(50);
}
} else {
// otherwise, play only the new color
int new_color = sequence[current_input - 1];
int tonez = tones[new_color];
startup_sequence(new_color, tonez);
}
}
int user_input() {
while (true) {
for (int i = 0; i < 4; i++) {
int button = buttons[i];
if (digitalRead(button) == LOW) {
while (digitalRead(button) == LOW) {} // Wait for button release
return i;
}
}
delay(1);
}
}
void loser() {
game_started = false;
Serial.print("LOSER! score - ");
Serial.println(current_input - 1);
digitalWrite(ledpins[0], HIGH);
digitalWrite(ledpins[1], HIGH);
digitalWrite(ledpins[2], HIGH);
digitalWrite(ledpins[3], HIGH);
tone(buzz, 100);
delay(1000);
digitalWrite(ledpins[0], LOW);
digitalWrite(ledpins[1], LOW);
digitalWrite(ledpins[2], LOW);
digitalWrite(ledpins[3], LOW);
noTone(buzz);
delay(500);
}
void checkuserinput()
{
int i = 0;
while (i < current_input) {
int correctbutton = sequence[i];
int userbutton = user_input();
if (correctbutton != buttons[userbutton]) {
loser();
return false;
} else {
int tonez = tones[correctbutton];
startup_sequence(correctbutton, tonez);
delay(500); // Wait before playing the next tone
i++;
}
}
// play the winning tone
digitalWrite(ledpins[0], HIGH);
digitalWrite(ledpins[1], HIGH);
digitalWrite(ledpins[2], HIGH);
digitalWrite(ledpins[3], HIGH);
tone(buzz, 1200);
delay(500);
noTone(buzz);
digitalWrite(ledpins[0], LOW);
digitalWrite(ledpins[1], LOW);
digitalWrite(ledpins[2], LOW);
digitalWrite(ledpins[3], LOW);
// increment the sequence length and play the next round
current_input++;
delay(500);
my_seq();
game_started = true;
}
void loop() {
if (!game_started) {
if (digitalRead(startbutton) == HIGH) {
// start a new game
for (int i = 0; i < 10; i++) {
sequence[i] = random(0, 4);
}
current_input = 1;
my_seq();
game_started = true;
}
} else {
// check the user input
bool input_result = checkuserinput();
if (!input_result) {
// if the user input is incorrect, end the game
current_input = 0;
game_started = false;
} else if (current_input == 10) {
// if the user input is correct for all 10 rounds, the player wins
current_input = 0;
game_started = false;
}
}
}