Help with programming memory game

I am doing memory game with UNO where I have 4 leds and 4 pushbuttons connected to 8 different digital pins and goal is to make a memory game where every turn that you successfully do it would add one extra step in the sequence you need to do and it should continue until you make a mistake and then it should come back to level 1. Problem occurs when user makes a first mistake, the game doesent start from step 1 but sequence blinks again and it is just stuck like that. It is probably a simple fix but I just cant wrap my head around it at the moment.
This is code:

int switch1 = 7; //The four button input pins
int switch2 = 6;
int switch3 = 5;
int switch4 = 4;
int led1 = 13; //LED pins
int led2 = 12;
int led3 = 11;
int led4 = 10;
int turn = 0;
int input1 = LOW;
int input2 = LOW;
int input3 = LOW;
int input4 = LOW;

int randomArray[50];
int inputArray[50];

void setup() {

Serial.begin(9600);

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(switch1, INPUT);
pinMode(switch2, INPUT);
pinMode(switch3, INPUT);
pinMode(switch4, INPUT);
randomSeed(analogRead(0)); //Added to generate “more randomness” with the randomArray for the output function

}

void output() { //function for generating the array

for (int y=turn; y <= turn; y++){ 

randomArray[y] = random(4, 8);

int size = sizeof(randomArray)/sizeof(int);

for(int k=0; k<size; k++)
{
  if(randomArray[k]== 4)
  {digitalWrite(led1, HIGH);
  delay(500);
  digitalWrite(led1, LOW);
  delay(100);}
  else if(randomArray[k]== 5)
  {digitalWrite(led2, HIGH);
  delay(500);
  digitalWrite(led2, LOW);
  delay(100);}
  else if(randomArray[k]== 6)
  {digitalWrite(led3, HIGH);
  delay(500);
  digitalWrite(led3, LOW);
  delay(100);}
  else if(randomArray[k]== 7)
  {digitalWrite(led4, HIGH);
  delay(500);
  digitalWrite(led4, LOW);
  delay(100);}
}

}
}

void input() { //Function for allowing user input and checking input against the generated array

for (int x=0; x <= turn;){ //Statement controlled by turn count
input1 = digitalRead(switch1);
input2 = digitalRead(switch2);
input3 = digitalRead(switch3);
input4 = digitalRead(switch4);

if (input1 == HIGH){ //Checking for button push
digitalWrite(led1, HIGH);
delay(200);
digitalWrite(led1, LOW);
inputArray[x] = 4;
delay(50);
if (inputArray[x] != randomArray[x]) { //Checks value input by user and checks it against
fail(); //the value in the same spot on the generated array
} //The fail function is called if it does not match
x++;
}

if (input2 == HIGH){
digitalWrite(led2, HIGH);
delay(200);
digitalWrite(led2, LOW);
inputArray[x] = 5;
delay(50);
if (inputArray[x] != randomArray[x]) {
fail();
}
x++;

}

if (input3 == HIGH){
digitalWrite(led3, HIGH);
delay(200);
digitalWrite(led3, LOW);
inputArray[x] = 6;
delay(50);
if (inputArray[x] != randomArray[x]) {
fail();
}
x++;

}

if (input4 == HIGH){

digitalWrite(led4, HIGH);
delay(200);
digitalWrite(led4, LOW);
inputArray[x] = 7;
delay(50);
Serial.print(" ");
Serial.print(4);
if (inputArray[x] != randomArray[x]) {
fail();
}
x++;

}
}
delay(500);
turn++; 
}

void fail() { //Function used if the player fails to match the sequence

for (int y=0; y<=5; y++){ 
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
delay(200);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
delay(200);
}
delay(500);
turn = -1; //Resets turn value so the game starts over
}

void loop() {
delay(1000);
  output();
  input();
}

Hi
You need to understand the for loop syntax

This loop will run only once:

and this will be run infinitely

As I understand, the error in that last loop is origin of your problem

Hello
Take a view here to get some ideas.
https://create.arduino.cc/projecthub/Metonymy/arduino-simple-simon-says-351b9e

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.