4 push buttons

I am using 4 push buttons, say, button1, button2, button3 and button4. When I press button1 I send the number 1, when I press button2 the number 2 is sent. Similarly for button3 and button4 the numbers 3 and 4 are sent respectively. Now when I press button1 and then button2, the message sent is "1" itself, until button1 is released, only then "2" is sent. But when I do the opposite, i.e. first press button2 and then button1, when only button2 is pressed, "2" is sent but when I press button1 as well, the serial monitor shows "1" and not "2". I need to keep sending "2" even if button1 is pressed. This happens for all the buttons. if I press 4 initially, "4" is sent. keeping 4 pressed, if I then press 3, not "4" but 3 is sent. does not happen when I do the reverse, i.e. pressing button3 and then keeping button3 pressed pressing button4.

Can anyone suggest a code for this?

the code I am using is:

int buttonPin1 = 30;
int buttonPin2 = 26;
int buttonPin3 = 28;
int buttonPin4 = 32;

int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
void setup()
{
    pinMode(buttonPin1,INPUT);
    pinMode(buttonPin2,INPUT);
    pinMode(buttonPin3,INPUT);
    pinMode(buttonPin4,INPUT);
    Serial.begin(9600);
}
 
void loop()
{
    buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);
    buttonState3 = digitalRead(buttonPin3);
    buttonState4 = digitalRead(buttonPin4);

    if(buttonState1 == HIGH){
      Serial.println("1");
    }

    else if(buttonState2 == HIGH){
     Serial.println("2");
    }

    else if(buttonState3 == HIGH){
      Serial.println("3");
    }

    else if(buttonState4 == HIGH){
     Serial.println("4");
    }

    else{
     Serial.println("0");
    }
}

Is this an electronics question?

The Programming section (ie here) would seem to be a more appropriate place for this thread so it has been moved

pranava2107:
I am using 4 push buttons, say, button1, button2, button3 and button4.......

Can anyone suggest a code for this?

Trying to follow your logic made me dizzy. I can't figure out what you expect to happen.

We can only guess what you want to happen.

Slow down and tell us exactly what you expect ‘should’ happen.

Do you know what ‘else if’ does ?

Have you studied how the state change example works ?

FYI
2017-08-05_20-33-24.jpg

In the posted code, remove every "else" and see what happens.

byte button[] = {30, 26, 28, 32};
byte numberOfButtons = 4;
byte buttonState[numberOfButtons];  
byte activeButton[numberOfButtons];
boolean hit;

void setup() 
{
  for (byte i = 0; i < numberOfButtons; i++) 
  {
    pinMode(button[i], INPUT_PULLUP);  //Check for LOW for a button press
  }   
  Serial.begin(115200); 
}

void loop() 
{
  for (byte i = 0; i < numberOfButtons; i++) //read all buttons at beginning of loop()
  {  
    buttonState[i] = !digitalRead(button[i]);
  }
  
  for (byte i = 0; i < numberOfButtons; i++)
  {
    if (buttonState[i] && (!otherButtonPressed(i) || activeButton[i]))  //if no other buttons are active or this one is already active
    {
      activeButton[i] = true;
      Serial.print(i+1);
    }
    else
    {
      activeButton[i] = false;
    }
  }
}

boolean otherButtonPressed(byte currentButton)
{
  hit = 0;
  for (byte i = 0; i < numberOfButtons; i++)
  {
    if (i == currentButton)
    {
      continue; // skip test for button passed to function
    }
    if (activeButton[i] == HIGH)  // if any other button is currently the active one
    {
      hit = true;
    }
  }
  return hit;  //Were any other buttons already pressed?
}

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How have you got your buttons wired to the Arduino?
What model Arduino are you using?

Thanks.. Tom.. :slight_smile:

You could also just use a library like TDuino which makes it quite simple:

#include <TDuino.h>

#define BUTTON_COUNT 4
const byte BUTTON_PINS[BUTTON_COUNT] = {30, 26, 28, 32};

void buttonPress(byte buttonPin, int state)
{
  Serial.print("Pressed: ");
  Serial.println(buttonPin);
}

void buttonRelease(byte buttonPin, int state)
{
  Serial.print("Released: ");
  Serial.println(buttonPin);
}

TButton buttons[BUTTON_COUNT];

void setup()
{
  Serial.begin(9600);
  for (byte i = 0; i < BUTTON_COUNT; i++)
  {
    buttons[i].attach(BUTTON_PINS[i]);
    buttons[i].onPress(buttonPress);
    buttons[i].onRelease(buttonRelease);
  }
}

void loop()
{
  for (byte i = 0; i < BUTTON_COUNT; i++) buttons[i].loop();
}