diff --git a/super_simon/src/main.cpp b/super_simon/src/main.cpp index 0c29931..6dd18d2 100644 --- a/super_simon/src/main.cpp +++ b/super_simon/src/main.cpp @@ -5,10 +5,15 @@ LiquidCrystal lcd(9, 8, 4, 5, 6, 7); +byte sequence[MAX_GAME] = {}; +int8_t currentPosition = -1; + void setup() { pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, HIGH); lcd.begin(LCD_COLS, LCD_ROWS); + lcd.print("Super Simon"); + randomSeed(analogRead(A0)); pinMode(BUZZER, OUTPUT); for (byte i = 0; i < 4; i++) { const byte led = LEDS[i]; @@ -17,24 +22,23 @@ void setup() { activate(i); delay(300); } - digitalWrite(LED_BUILTIN, LOW); deactivateAll(); + digitalWrite(LED_BUILTIN, LOW); + delay(2000); } void loop() { - if (buttonIsPressed(GREEN)) { - activate(GREEN); - } - else if (buttonIsPressed(YELLOW)) { - activate(YELLOW); - } - else if (buttonIsPressed(BLUE)) { - activate(BLUE); - } - else if (buttonIsPressed(RED)) { - activate(RED); - } - else { - deactivateAll(); + if (currentPosition + 1 >= MAX_GAME) { + lcd.clear(); + lcd.print("Bravo!"); + delay(10000); + return; } + + currentPosition += 1; + byte newItem = random(0, 4); + sequence[currentPosition] = newItem; + playSequence(sequence, currentPosition); + + delay(2000); } diff --git a/super_simon/src/main.h b/super_simon/src/main.h index c4fbc1c..b8fb041 100644 --- a/super_simon/src/main.h +++ b/super_simon/src/main.h @@ -31,6 +31,8 @@ #define BLUE 2 #define RED 3 +#define MAX_GAME 10 + const uint8_t LEDS[] = {LED_GREEN, LED_YELLOW, LED_BLUE, LED_RED}; const uint8_t BUTTONS[] = {BUTTON_GREEN, BUTTON_YELLOW, BUTTON_BLUE, BUTTON_RED}; const uint16_t TONES[] = {TONE_GREEN, TONE_YELLOW, TONE_BLUE, TONE_RED}; diff --git a/super_simon/src/utils.cpp b/super_simon/src/utils.cpp index 13e4951..3728828 100644 --- a/super_simon/src/utils.cpp +++ b/super_simon/src/utils.cpp @@ -31,3 +31,12 @@ void deactivateAll() { digitalWrite(led, LOW); } } + +void playSequence(const byte sequence[], byte upTo) { + for (byte i = 0; i <= upTo; i++) { + activate(sequence[i]); + delay(300); + deactivateAll(); + delay(15); + } +} diff --git a/super_simon/src/utils.h b/super_simon/src/utils.h index b6db2cf..2daeb8e 100644 --- a/super_simon/src/utils.h +++ b/super_simon/src/utils.h @@ -9,5 +9,6 @@ void activate(byte index); void buzz(byte index, unsigned long duration = 0); bool buttonIsPressed(byte index); void deactivateAll(); +void playSequence(const byte sequence[], byte upTo); #endif //SUPER_SIMON_UTILS_H