arduino-toy-projects/super_simon/src/main.cpp

46 lines
942 B
C++
Raw Normal View History

2021-04-29 17:00:09 +02:00
#include <Arduino.h>
#include <LiquidCrystal.h>
#include "main.h"
#include "utils.h"
2021-04-29 17:00:09 +02:00
LiquidCrystal lcd(9, 8, 4, 5, 6, 7);
2021-04-29 17:38:54 +02:00
byte sequence[MAX_GAME] = {};
int8_t currentPosition = -1;
2021-04-29 20:23:21 +02:00
bool win = true;
2021-04-29 20:25:18 +02:00
bool over = false;
2021-04-29 17:38:54 +02:00
2021-04-29 17:00:09 +02:00
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
lcd.begin(LCD_COLS, LCD_ROWS);
2021-04-29 17:38:54 +02:00
lcd.print("Super Simon");
randomSeed(analogRead(A0));
2021-04-29 17:00:09 +02:00
pinMode(BUZZER, OUTPUT);
2021-04-29 20:23:21 +02:00
configure();
deactivateAll();
2021-04-29 17:38:54 +02:00
digitalWrite(LED_BUILTIN, LOW);
2021-04-29 17:00:09 +02:00
}
void loop() {
2021-04-29 17:38:54 +02:00
if (currentPosition + 1 >= MAX_GAME) {
2021-04-29 20:25:18 +02:00
over = true;
}
if (over) {
2021-04-29 20:23:21 +02:00
endGame(lcd, win, currentPosition);
2021-04-29 17:38:54 +02:00
return;
}
2021-04-29 17:38:54 +02:00
2021-04-30 08:10:21 +02:00
delay(1200);
2021-04-29 17:38:54 +02:00
currentPosition += 1;
byte newItem = random(0, 4);
sequence[currentPosition] = newItem;
2021-04-29 20:23:21 +02:00
2021-04-29 17:38:54 +02:00
playSequence(sequence, currentPosition);
2021-04-29 20:23:21 +02:00
if (!userSequence(sequence, currentPosition)) {
win = false;
2021-04-29 20:25:18 +02:00
over = true;
2021-04-29 20:23:21 +02:00
}
}