Using Multiple Pushbuttons

Hello,

I am working with Arduino Uno R3

I am trying to use 4 buttons for different Game modes within a project I am working on.

I have all the buttons working with debounce that will allow each button to have a HIGH or LOW state.

However what I am going for is to only allow 1 button to go HIGH at a time, so only one button can be pressed at a time.

What would be ideal would be you can click through the buttons and that button goes HIGH and all other buttons go to LOW state. But what would also work, is you have to make all buttons LOW before another button can be HIGH.

This would allow two buttons to never be HIGH at the same time.

Any help would be great thanks.

Here is the code i'm using

 long timertime = 0;   
 long debounce = 150;

 int GM1 = 9; 
 int switchState = LOW; 
 int prevSwitchState = HIGH;
 int ReadGM1;
 int light = 5; 

 int GM2 = 10; 
 int switchState1 = LOW;
 int prevSwitchState1 = HIGH;
 int ReadGM2;
 int light2 = 6; 

 int GM3 = 11; 
 int switchState2 = LOW;
 int prevSwitchState2 = HIGH;
 int ReadGM3;
 int light3 = 7; 


 int GM4 = 12; 
 int switchState3 = LOW;
 int prevSwitchState3 = HIGH;
 int ReadGM4;
 int light4 = 8; 


void setup() {

 pinMode (GM1,INPUT);
 pinMode (light,OUTPUT); 

 pinMode (GM2,INPUT);
 pinMode (light2,OUTPUT);

 pinMode (GM3,INPUT);
 pinMode (light3,OUTPUT);

 pinMode (GM4,INPUT);
 pinMode (light4,OUTPUT);

}


void loop (){

ReadGM1 = digitalRead(GM1); 
 if (ReadGM1 == HIGH && prevSwitchState == LOW && millis() - timertime > debounce)
 {
  if (switchState == HIGH)
      switchState = LOW;
  else
    switchState = HIGH;
    timertime = millis();    
  }
 digitalWrite(light, switchState);
 prevSwitchState = ReadGM1;



ReadGM2 = digitalRead(GM2); 
 if (ReadGM2 == HIGH && prevSwitchState1 == LOW && millis() - timertime > debounce)
 {
  if (switchState1 == HIGH)
      switchState1 = LOW;
  else
    switchState1 = HIGH;
    timertime = millis();    
  }
 digitalWrite(light2, switchState1);
 prevSwitchState1 = ReadGM2;



ReadGM3 = digitalRead(GM3); 
 if (ReadGM3 == HIGH && prevSwitchState2 == LOW && millis() - timertime > debounce)
 {
  if (switchState2 == HIGH)
      switchState2 = LOW;
  else
    switchState2 = HIGH;
    timertime = millis();    
  }
 digitalWrite(light3, switchState2);
 prevSwitchState2 = ReadGM3;



ReadGM4 = digitalRead(GM4); 
 if (ReadGM4 == HIGH && prevSwitchState3 == LOW && millis() - timertime > debounce)
 {
  if (switchState3 == HIGH)
      switchState3 = LOW;
  else
    switchState3 = HIGH;
    timertime = millis();    
  }
 digitalWrite(light4, switchState3);
 prevSwitchState3 = ReadGM4;

}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks… Tom… :slight_smile:

You have buttons and you have lights in your code. Your text doesn't mention lights. It seems like perhaps you have used the word "button" a few times when you meant "light".

Each time you detect a successful transition for a button being pushed, digitalWrite() all the other lights off.

In fact, it may be better to write a function allLightsOff() and then turn on the one you want on.

I am only using the lights as indicators to see when game mode is selected. When each is button is HIGH it will run code after that.

This consists of 4 game modes. Currently I have at the start of each game mode a statement such as

if (switchState == HIGH && switchState1 == LOW && switchState2 == LOW && switchState3 == LOW) // Game mode1 code here.

Which allows game mode 1 to only run when GM2. GM3, GM4 buttons are LOW and GM1 button is high.

But this is causing bugs in the program, Id like to add a feature that only allows one game mode button to be HIGH at a time.

So create a variable called GameMode. When a button is pushed, change the GameMode to that value (1, 2, 3 or 4). The game mode can't hold 2 values at once - it will be just the one mode that you're in.

MorganS: So create a variable called GameMode. When a button is pushed, change the GameMode to that value (1, 2, 3 or 4). The game mode can't hold 2 values at once - it will be just the one mode that you're in.

Sorry, I'm new to programming with c++. With research I have been doing I have seen what you are suggesting be done, but they were using 1 push button. Would this work the same using 4 pushbuttons all on separate inputs?

Why not? The button pushes can't happen simultaneously. Even if you try to push two at once, the Arduino will detect one first, then the other.