I'm trying to make a Simon says style Arduino game, but I am having a lot of trouble switching between the different states. The code definitely hits these statements, but nothing happens when it does. Specifically, this is happening at state = error in the user input section of the switch statement. Here is the code:
typedef enum { output, userInput, idleReset, finish, error} SimonState;
SimonState state = output;
bool boot = true;
int greenButton = 5;
int greenLed = 6;
int yellowButton = 7;
int yellowLed = 8;
int redButton = 9;
int redLed = 10;
int blueButton = 11;
int blueLed = 12;
int counter = 0;
int memory[10];
int userMem[10] = {4};
int buzzer = 3;
int roundsNumber = 1;
int blinkDelay = 500;
int totalRounds = 3;
int buttonValueg;
int buttonStateg = LOW;
int lastKnowng = LOW;
int lastTimeg = 0;
void debounceGreen(int inputCheck) {
buttonValueg = digitalRead(greenButton);
if (buttonValueg != lastKnowng) {
lastTimeg = millis();
}
if ((millis() - lastTimeg) > 50) {
if (buttonValueg != buttonStateg) {
buttonStateg = buttonValueg;
if (buttonStateg == HIGH) {
userMem[inputCheck] = 0;
green();
}
}
}
lastKnowng = buttonValueg;
}
int buttonValuer;
int buttonStater = LOW;
int lastKnownr = LOW;
int lastTimer = 0;
void debounceRed(int inputCheck) {
buttonValuer = digitalRead(redButton);
if (buttonValuer != lastKnownr) {
lastTimer = millis();
}
if ((millis() - lastTimer) > 50) {
if (buttonValuer != buttonStater) {
buttonStater = buttonValuer;
if (buttonStater == HIGH) {
userMem[inputCheck] = 2;
red();
}
}
}
lastKnownr = buttonValuer;
}
int buttonValueb;
int buttonStateb = LOW;
int lastKnownb = LOW;
int lastTimeb = 0;
void debounceBlue(int inputCheck) {
buttonValueb = digitalRead(blueButton);
if (buttonValueb != lastKnownb) {
lastTimeb = millis();
}
if ((millis() - lastTimeb) > 50) {
if (buttonValueb != buttonStateb) {
buttonStateb = buttonValueb;
if (buttonStateb == HIGH) {
userMem[inputCheck] = 3;
blue();
}
}
}
lastKnownb = buttonValueb;
}
int buttonValuey;
int buttonStatey = LOW;
int lastKnowny = LOW;
int lastTimey = 0;
void debounceYellow(int inputCheck) {
buttonValuey = digitalRead(yellowButton);
if (buttonValuey != lastKnowny) {
lastTimey = millis();
}
if ((millis() - lastTimey) > 50) {
if (buttonValuey != buttonStatey) {
buttonStatey = buttonValuey;
if (buttonStatey == HIGH) {
userMem[inputCheck] = 1;
yellow();
}
}
}
lastKnowny = buttonValuey;
}
void errorTone() {
for (int i = 0; i < 3; i++) {
tone(buzzer, 250);
delay(250);
noTone(buzzer);
delay(250);
}
boot = false;
}
void winner() {
for (int i = 0; i < 3; i++) {
digitalWrite(greenLed, HIGH);
delay(50);
digitalWrite(yellowLed, HIGH);
delay(50);
digitalWrite(redLed, HIGH);
delay(50);
digitalWrite(blueLed, HIGH);
delay (250);
digitalWrite(greenLed, LOW);
delay(50);
digitalWrite(yellowLed, LOW);
delay(50);
digitalWrite(redLed, LOW);
delay(50);
digitalWrite(blueLed, LOW);
delay(250);
}
noTone(buzzer);
tone(buzzer, 440);
delay(100);
tone(buzzer, 587);
delay(100);
tone(buzzer, 784);
delay(100);
tone(buzzer, 880);
delay(100);
noTone(buzzer);
}
void green() {
digitalWrite(greenLed, HIGH);
tone(buzzer, 880);
delay(500);
noTone(buzzer);
delay(blinkDelay);
digitalWrite(greenLed, LOW);
}
void yellow() {
digitalWrite(yellowLed, HIGH);
tone(buzzer, 784);
delay(500);
noTone(buzzer);
delay(blinkDelay);
digitalWrite(yellowLed, LOW);
}
void red() {
digitalWrite(redLed, HIGH);
tone(buzzer, 440 );
delay(500);
noTone(buzzer);
delay(blinkDelay);
digitalWrite(redLed, LOW);
}
void blue() {
digitalWrite(blueLed, HIGH);
tone(buzzer, 587);
delay(500);
noTone(buzzer);
delay(blinkDelay);
digitalWrite(blueLed, LOW);
}
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
for (int i = 0; i < 10; i++) {
memory[i] = random(4);
}
pinMode(greenLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(redLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(greenButton, INPUT);
pinMode(yellowButton, INPUT);
pinMode(redButton, INPUT);
pinMode(blueButton, INPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
switch (state) {
case output:
if (roundsNumber > totalRounds) {
Serial.println("Big memory!");
state = finish;
}
Serial.println("output");
if (counter >= roundsNumber)
state = userInput;
else if (memory[counter] == 0)
green();
else if (memory[counter] == 1)
yellow();
else if (memory[counter] == 2)
red();
else if (memory[counter] == 3)
blue();
delay(500);
counter++;
break;
case userInput:
Serial.println("input time baaaaaaaabyyyyyyyyy");
for (int inputCheck = 0; inputCheck < roundsNumber;) {
Serial.println("debouncing...");
debounceGreen(inputCheck);
debounceBlue(inputCheck);
debounceYellow(inputCheck);
debounceRed(inputCheck);
if (userMem[inputCheck] != memory[inputCheck] && userMem[inputCheck] != 4) {
Serial.println("failed");
Serial.println(inputCheck);
state = error;
}
else if (userMem[inputCheck] == memory[inputCheck]) {
Serial.println("passed");
Serial.println(inputCheck);
inputCheck++;
}
}
delay(250);
state = idleReset;
break;
case idleReset:
for (int i = 0; i < totalRounds; i++) {
userMem[i] = 4;
}
roundsNumber++;
counter = 0;
state = output;
break;
case finish:
Serial.println("Winner!");
winner();
break;
case error:
Serial.println("error");
errorTone();
break;
}
}```