Fix end of game and add play sequence

This commit is contained in:
Gabriel Augendre 2021-04-29 17:38:54 +02:00
parent ac6fcae9b5
commit fb42e4d6b5
4 changed files with 31 additions and 15 deletions

View file

@ -5,10 +5,15 @@
LiquidCrystal lcd(9, 8, 4, 5, 6, 7); LiquidCrystal lcd(9, 8, 4, 5, 6, 7);
byte sequence[MAX_GAME] = {};
int8_t currentPosition = -1;
void setup() { void setup() {
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); digitalWrite(LED_BUILTIN, HIGH);
lcd.begin(LCD_COLS, LCD_ROWS); lcd.begin(LCD_COLS, LCD_ROWS);
lcd.print("Super Simon");
randomSeed(analogRead(A0));
pinMode(BUZZER, OUTPUT); pinMode(BUZZER, OUTPUT);
for (byte i = 0; i < 4; i++) { for (byte i = 0; i < 4; i++) {
const byte led = LEDS[i]; const byte led = LEDS[i];
@ -17,24 +22,23 @@ void setup() {
activate(i); activate(i);
delay(300); delay(300);
} }
digitalWrite(LED_BUILTIN, LOW);
deactivateAll(); deactivateAll();
digitalWrite(LED_BUILTIN, LOW);
delay(2000);
} }
void loop() { void loop() {
if (buttonIsPressed(GREEN)) { if (currentPosition + 1 >= MAX_GAME) {
activate(GREEN); lcd.clear();
} lcd.print("Bravo!");
else if (buttonIsPressed(YELLOW)) { delay(10000);
activate(YELLOW); return;
}
else if (buttonIsPressed(BLUE)) {
activate(BLUE);
}
else if (buttonIsPressed(RED)) {
activate(RED);
}
else {
deactivateAll();
} }
currentPosition += 1;
byte newItem = random(0, 4);
sequence[currentPosition] = newItem;
playSequence(sequence, currentPosition);
delay(2000);
} }

View file

@ -31,6 +31,8 @@
#define BLUE 2 #define BLUE 2
#define RED 3 #define RED 3
#define MAX_GAME 10
const uint8_t LEDS[] = {LED_GREEN, LED_YELLOW, LED_BLUE, LED_RED}; 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 uint8_t BUTTONS[] = {BUTTON_GREEN, BUTTON_YELLOW, BUTTON_BLUE, BUTTON_RED};
const uint16_t TONES[] = {TONE_GREEN, TONE_YELLOW, TONE_BLUE, TONE_RED}; const uint16_t TONES[] = {TONE_GREEN, TONE_YELLOW, TONE_BLUE, TONE_RED};

View file

@ -31,3 +31,12 @@ void deactivateAll() {
digitalWrite(led, LOW); 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);
}
}

View file

@ -9,5 +9,6 @@ void activate(byte index);
void buzz(byte index, unsigned long duration = 0); void buzz(byte index, unsigned long duration = 0);
bool buttonIsPressed(byte index); bool buttonIsPressed(byte index);
void deactivateAll(); void deactivateAll();
void playSequence(const byte sequence[], byte upTo);
#endif //SUPER_SIMON_UTILS_H #endif //SUPER_SIMON_UTILS_H