Operation Game - How to reset if mistake is made?

Hey forgive me if the answer is obvious, I’ve tried searching for help but I’m new to programming.
So I’m setting up an “Operation” style game where the player has to reach a metal rod (Rod) through four holes to touch a Contact Point (CP) without hitting the edge of the hole. The sequence in which you have to touch the CP’s is indicated by an LED. So at startup, LED over hole 1 is on - touch CP1 - LED1 turns off and LED2 turns on, indicating that is the next one to press. Its basically a push button sequence code, but there’s a metal hole attached to the reset pin that players have to avoid. Each CP has to be touched in the correct order to trigger a relay (in my test code, winning = flashing LED), and I want to make it so that if the player touches the incorrect CP, the game resets and they have to start from the beginning (which is the part I don’t know how to do).

Since I need contact between the Rod and Hole to trigger reset, the Rod is attached by a wire to Arduino Ground. Therefore the CP Inputs must be reading for LOW to count a correct input.

Here’s my code. I ripped a lot of it from another project I found online, so basically all the comments are not mine, but probably still valid.

const int btn1 = 2;
const int btn2 = 3;
const int btn3 = 4;
const int btn4 = 5;
const int LED1 = 6;
const int LED2 = 7;
const int LED3 = 8;
const int LED4 = 9;
const int LED5 = 10;

int code [] = {1, 2, 3, 4};
int Entered[4];

void setup() {
  Serial.begin(9600);
  pinMode(btn1, INPUT);
  pinMode(btn2, INPUT);
  pinMode(btn3, INPUT);
  pinMode(btn4, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(btn1, HIGH);
  digitalWrite(btn2, HIGH);
  digitalWrite(btn3, HIGH);
  digitalWrite(btn4, HIGH);
  for (int i = 0; i < 4;i++){
    Serial.println(code[i]);
    Serial.println(Entered[i]);
  }
}

void(* resetFunc) (void) = 0;

void checkEntered1(int button /* define the 1,2,3 or 4 as an integer called button */){ //check the first element of the entered[] array
  if (Entered[0] != 0){
    checkEntered2(button); //move on to checkEntered2, passing it "button"
  }
  
  else if(Entered[0] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
    Entered[0] = button; //set the first element as the button that has been pressed
    Serial.print("1: ");Serial.println(Entered[0]); //for debugging
  }
  
}

void checkEntered2(int button){ //check the second element of the entered[] array
  if (Entered[1] != 0){ //if it is not a zero, i.e. it has already been inputted
    checkEntered3(button); //move on to checkEntered3, passing it "button"
  }
  
  else if(Entered[1] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
    Entered[1] = button; //set the second element as the button that has been pressed
    Serial.print("2: ");Serial.println(Entered[1]); //for debugging
  }
  
}

void checkEntered3(int button){  //check the third element of the entered[] array
  if (Entered[2] != 0){ //if it is not a zero, i.e. it has already been inputted
    checkEntered4(button); //move on to checkEntered4, passing it "button"
  }
  
  else if (Entered[2] == 0){ //if it is zero, i.e. if it hasn't been defined with a button yet
    Entered[2] = button; //set the third element as the button that has been pressed
    Serial.print("3: ");Serial.println(Entered[2]); //for debugging
  }  
}

void checkEntered4(int button){  //check the third element of the entered[] array
  if (Entered[3] == 0){ //if it is not a zero, i.e. it has already been inputted
    Entered[3] = button;
    delay(100);
    compareCode();//move on to checkEntered4, passing it "button"
  }
  
}


void loop() {
 if (digitalRead(btn1) == LOW){ //if button1 is pressed
    checkEntered1(1); //call checkEntered and pass it a 1
    delay(250);//wait, needed for correct functioning, otherwise
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
               //buttons are deemed to be pressed more than once
  }
  else if (digitalRead(btn2) == LOW){ //if button2 is pressed
    checkEntered1(2); //call checkEntered1 and pass it a 2
    delay(250); //wait
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, HIGH);
  }
  else if (digitalRead(btn3) == LOW){ //if button3 is pressed
    checkEntered1(3); //call checkEntered1 and pass it a 3
    delay(250); //wait
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, HIGH);
  }
  else if (digitalRead(btn4) == LOW){ //if button4 is pressed
    checkEntered1(4); //call checkEntered1 and pass it a 4
    delay(250); //wait
  }
  }

void compareCode(){ //checks if the code entered is correct by comparing the code[] array with the entered[] array
  for (int i = 0; i<4;i++){ //these three lines are for debugging
   
  }
  if ((Entered[0]==code[0]) && (Entered[1]==code[1]) && (Entered[2]==code[2]) && (Entered[3]==code[3])){   
    digitalWrite (LED4, LOW);
    digitalWrite (LED5, HIGH);
    delay(200);
    digitalWrite (LED5, LOW);
    delay(200);
    digitalWrite (LED5, HIGH);
    delay(200);
    digitalWrite (LED5, LOW);
    delay(200);
    digitalWrite (LED5, HIGH);
    delay(200);
    digitalWrite (LED5, LOW);
    delay(200);
  }
  else {
    resetFunc();
  }
}

Any tips or advice is greatly appreciated!

Then it’s time to learn.

Will you teach me? :smiley:

large_boi:
Its basically a push button sequence code, but there's a metal hole attached to the reset pin that players have to avoid.

Right there's your starting point. Do a site search on 'combination lock', which is basically what you're doing.

large_boi:
Will you teach me? :smiley:

Heaps of C++ tutorials online, including this very site. Also lots of Arduino specific tutorials.

large_boi:
I want to make it so that if the player touches the incorrect CP, the game resets and they have to start from the beginning (which is the part I don't know how to do).

Read up about "state machine"

If your user begins with the STARTING state and progresses through the different positions at any time that s/he touches the wrong probe the program can set the state back to STARTING.

...R