Stopping bomb defusal loop to avoid repeatedly pressing button

I'm writing a code for my Leonardo, so that if the right wire from a bomb prop is pulled, it transitions a slide on the computer its connected to (done by pressing 6 then ENTER) and conversely if wrong wire pulled / goes HIGH it sends the command 4 then ENTER...
I tried using the BOOLEAN condition of game_complete to update the condition once one of this statements was performed but it just continually repeats the key command, rather than just entering it once...
Is there a fault in my code or can Arduino simply not perform this function in this way?

code below:

`#include <Keyboard.h>

void setup() {
  // put your setup code here, to run once:
boolean game_complete = false;

}

void loop() {
  // put your main code here, to run repeatedly:


  pinMode (9,INPUT_PULLUP);
  pinMode (8,INPUT_PULLUP);
  pinMode (7,INPUT_PULLUP);
  pinMode (6,OUTPUT);
int right_wire = 9;
int wrong_wire = 8;
int big_red_button = 7;
boolean game_complete;

if(game_complete == false){

//here it will transition slide by pressing "enter" when the red button is pressed which will reveal the wire to pull.
if (digitalRead(big_red_button)==LOW){
      Keyboard.press(KEY_RETURN);
    delay(100);
    Keyboard.releaseAll();
    delay(5000);
  }
//if the correct wire is pulled, then the following will occur 

  else if(digitalRead(right_wire)==HIGH){

    // press number then enter here "54" is the code for the number 5
    //this will press "6" then the "enter" key
    Keyboard.press(54);
    delay(100);
    Keyboard.press(KEY_RETURN);
    delay(100);
    Keyboard.releaseAll();
    delay(1000);
    game_complete = true;
    
  }
  //if the wrong wire is removed, then the following will occur
  
    else if (digitalRead(wrong_wire)==HIGH){

    // press number then enter here "52" is the code for the number 4
    //this will press "4" then the "enter" key
    Keyboard.press(52);
    delay(100);
    Keyboard.press(KEY_RETURN);
    delay(100);
    Keyboard.releaseAll();
    delay(1000);
    game_complete = true;
    

  }
  
}

}`

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

boolean game_complete;

You do this each time through loop(). As it is a local variable and you do not specify a particular value it will be set to whatever was previously in the location the compiler gives it

Then you test its value, which is a waste of time.

You also have a global variable named game_complete and it would make more sense to use that to hold the state of the game. Remove or comment out boolean game_complete; in the loop() function to force the sketch to use the global variable of the same name

My apologies. i have fixed this, thank you for calling me out.

I'm a little lost with your response but are you saying that if I just remove the boolean from the loop entirely or just the original statement ?
would the game_complete= true still take effect in the loop after each condition is met?

Looking at this again I see that you do not have a global named game_complete, rather you have one in setup() and another in loop(), each with their own scope and you also have other code in loop() that should not be there, such as the pinMode() calls

Try this, but no guarantees that it will do what you want, but it compiles

      game_complete = true;

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