Program stops taking input after I press a button

Hi there! I am working on a project in which I have 4 buttons. When I get the state for each button, they return 1 when I am holding down the button and 0 when I am not, as they should. However, as soon as I use the state to control my LEDs, the Serial monitor stops reading the input and the buttons do not respond to any more presses. How do I make my Arduino to continue reading the input after I use the button press? I am so confused as to why this is happening and would really appreciate some guidance. My code so far is pasted below. It all works fine except that the Arduino stops taking input after I start the game.

const int bButton = 2;
const int gButton = 4;
const int yButton = 6;
const int rButton = 8;
const int bLED = 3;
const int gLED = 5;
const int yLED = 7;
const int rLED = 9;

int bbState = 0;
int gbState = 0;
int ybState = 0;
int rbState = 0;
int lastbbState = 0;
int lastgbState = 0;
int lastybState = 0;
int lastrbState = 0;

volatile bool gbPressed = false;
volatile bool bbPressed = false;
volatile bool ybPressed = false;
volatile bool rbPressed = false;

bool gameCondition = false;

int mole = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int i = 0; i < 10; i++)
{
if(i%2 == 0)
{
pinMode(i, INPUT);
digitalWrite(i, 1);

}
else
{
  pinMode(i, OUTPUT);
  digitalWrite(i, HIGH);
  delay(200);
  digitalWrite(i, LOW);
}

}
}

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

bbState = digitalRead(bButton);
gbState = digitalRead(gButton);
ybState = digitalRead(yButton);
rbState = digitalRead(rButton);
Serial.print(bbState);
if((bbState == 1 || gbState ==1 || ybState == 1 || rbState== 1) && gameCondition == false)
{
startGame();
}

}

void startGame()
{
gameCondition = true;
bbState = 0;
while(gameCondition)
{
mole = 2*random(2,6)-1;
delay(1000);
digitalWrite(mole, HIGH);
delay(500); //difficulty
digitalWrite(mole, LOW);
}
}
void endGame()
{
gameCondition = false;
}

Sorry to reply to myself, but I want to clarify that it is the if statement within the loop that is causing the problem. When I comment it out, the serial monitor continues to read the button state as normal.

void startGame()
{
  gameCondition = true;
  bbState = 0;
  while (gameCondition)
  {
    mole = 2 * random(2, 6) - 1;
    delay(1000);
    digitalWrite(mole, HIGH);
    delay(500); //difficulty
    digitalWrite(mole, LOW);
  }
}

Your code will never exit the while loop because the value of gameCondition never changes within it. What is the purpose of the while loop in your code ?

There was another if statement that called the endGame() function which changes the gameCondition to false. But I could never access it because the program stops reading input from the buttons after the first if statement. I need the while loop in order to keep the LEDs turning on and off randomly throughout the duration of the game.

No you don't, and in any case the delay()s will mess with whatever game you are writing because nothing will happen while they occur

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Okay thanks! I am still having the same issue with or without the while loop and delays, though. Even when the if statement simply turn on the LED on the button press, I cannot turn it off afterwards on a second button press, because the serial monitor no longer shows input being taken in from the button.

Where does gameCondition get cancelled within the while() loop ?

It sounds like to need to change the state of a variable when the button becomes pressed. Take a look at the StateChangeDetection example in the IDE

Thought this one would work, but nope. Still immediately stop taking input as soon as I use the state in an if statement. I used the example in the IDS and added a nested if to detect the change, but it's still the same issue. Could it be a problem with my buttons themselves? Each of them are connected with one pin through a 10k ohm pull down resistor to the ground, one pin to the arduino, and one to power.

Describing this issue a little better here:
I am tracking the button state in the Serial monitor. It reads 0 continuously before the button is pressed. As soon as I press the button, it changes to 1 (as it should) and then stops. So it reads "0000000001" and then does not continue. Without the if statement, it works fine though.

You lost me
Please post a simple diagram of how the input is wired

Since you use the term 'pull down resistor' see if your circuit looks like S1 below

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