/*
Simon Game for Arduino
Copyright (C) 2016, Uri Shaked
Released under the MIT License.
Modified 2023, Robin Gniwotta
*/
// Include headerfile
#include "functions.h"
// Constants - speed values should be modified carefully
const byte ledPins[] = {9, 10, 11, 12}; // LED Pins
const byte buttonPins[] = {2, 3, 4, 5}; // BUTTON Pins
const int sequenceSpeed = 200; // Sequence display speed
const int sequenceBlink = 300; // Led light up time in sequence
const int levelUpBlink = 200; // Blink speed when indicating level up
const int failDelay[] = {250, 500, 750}; // Led light up time in game over sequence
const int failDelayOff = 200; // Time led stays off in game over sequence
const int scoreShow = 3000; // Time the bcd score is shown
const int resetDelay = 1000; // Time after a reset before normal operation is resumed
const int idleSpeed = 2000 / 10; // Change first number for idle animation speed
const int idleTime = 15000; // Set the time until the system is considered idle
const int generalDelay = 300; // General game delay
const int loopDelay = 300; // Time between each game loop (time to release button in game)
#define MAX_GAME_LENGTH 100
// Global variables - store the game state
byte gameSequence[MAX_GAME_LENGTH] = {0};
byte gameIndex = 0;
byte score = 0;
unsigned long lastPress = 0;
// Debug
bool debug = false;
// Set up the Arduino board and perform power-on-test
void setup() {
byte ledsOn = 0;
byte ledState[] = {LOW, LOW, LOW, LOW};
digitalWrite(LED_BUILTIN, HIGH);
// Set pinModes and turn on leds
for (byte i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
digitalWrite(ledPins[i], HIGH);
ledState[i] = HIGH;
ledsOn++;
}
// Prime the random number generator
randomSeed(analogRead(A0));
// Perform power-on-test
while (ledsOn != 0) {
for (byte i = 0; i < 4; i++) {
if (digitalRead(buttonPins[i]) == LOW && ledState[i] == HIGH) {
digitalWrite(ledPins[i], LOW);
ledState[i] = LOW;
ledsOn--;
lastPress = millis();
}
}
}
if (digitalRead(buttonPins[0]) == LOW && digitalRead(buttonPins[1]) == LOW && digitalRead(buttonPins[2]) == LOW && digitalRead(buttonPins[3]) == LOW) {
debug = true;
Serial.begin(9600);
Serial.println("debug enabled"); //DEBUG
}
else {
digitalWrite(LED_BUILTIN, LOW);
}
}
//The main game loop
void loop() {
if (resetButtons()) {
reset();
delay(resetDelay);
}
else if (scoreButtons()) {
displayScore();
delay(generalDelay);
}
else if (idle()) {
idleAnimation();
ledsAction(0);
delay(generalDelay);
}
else {
delay(loopDelay);
gameSequence[gameIndex] = random(0, 4);
gameIndex++;
if (gameIndex >= MAX_GAME_LENGTH) {
gameIndex = MAX_GAME_LENGTH - 1;
}
playSequence();
if (!checkUserSequence()) {
gameOver();
delay(generalDelay);
}
else if (gameIndex > 0) {
delay(generalDelay - 100);
indicateLevelUp();
}
}
}