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;
}