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);
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);
}

View file

@ -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};

View file

@ -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);
}
}

View file

@ -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