pushbuttons

Hello everybody.

I'm new in arduino.

I've make a simple program with three pushbuttons that should command 3 outputs independently, the problem is, when i push 2 or the 3 buttons simultaneously more than one output is activated.
I only would one.

int State1 = 0;
int State2 = 0;
int State3 = 0;

void setup() {

 Serial.begin(9600);

 pinMode(2,INPUT);
 pinMode(3,INPUT);
 pinMode(4,INPUT);

}

void loop() {

  if (digitalRead(2)== HIGH) State1 = 1;
  else if (digitalRead(3)== HIGH) State2 = 1;
  else if (digitalRead(4)== HIGH) State3 = 1;
  else {
    State1 = 0;
    State2 = 0;
    State3 = 0;
  }

  
  Serial.print(State1);
  Serial.print("   ");
  Serial.print(State2);
  Serial.print("   ");
  Serial.println(State3);
  

}

How are the inputs wired ?
Do you have pulldown resistors in place to keep them LOW when the pushbuttons are not pressed or are they floating at an unknown voltage ?

digitalRead will return either 0 (LOW) or 1 (HIGH), so you can simply set button state like this:

StateX = digitalRead(PinNumber);

Before you check the state of a button, you must figure out if either states are HIGH - the easiest way of doing this is to add all states together and check if the sum is zero:

if (State1+State2 == 0) { read buttons until a HIGH is found  }

the problem is, when i push 2 or the 3 buttons simultaneously more than one output is activated.
I only would one.

No, that is not what happens, unless your switches are not wired properly.

What IS happening is that you do NOT set the value of State1, State2 and State3 on every pass through loop(). You only set some of them. So, sometimes you print new data and sometimes you print old data.

You need to separate the setting of State1, State2, and State3, so that they are independent.

You could also just convert the button states to which button is pressed:

int currentButton = 0;
...
if (digitalRead(pin1)) currentButton = 1;
else if (digitalRead(pin2)) currentButton = 2;
else if (digitalRead(pin3)) currentButton = 3;
else currentButton = 0;
...
Serial.print("Current button = ");
Serial.println(currentButton);

This will prevent more than one button to be registred.

You could put the pin numbers in an array and iterate through it to output the current state.

Sorry i'm not very good in English but as i have understand, it seems that my buttons are not wired correctly.

Can you tell me what is needed to be modified in the schematic (attached file).

Danois90's code makes no change at all.

Your wiring is OK as it is. The inputs are held LOW by the 10K resistors until the buttons are pressed to take them HIGH. There are ways of using inputs without external resistors but leave them alone for now.

The real problem is explained in reply #3

So, what is the simplest way to make it work.

How to set the value of each state in every pass through loop

Is it better to make a function?

So, what is the simplest way to make it work.

How to set the value of each state in every pass through loop

Is it better to make a function?

How to set the value of each state in every pass through loop

Did you read reply #2?

  byte State1 = digitalRead(2);
   byte State2 = digitalRead(3);
   byte State3 = digitalRead(4);

Now, the value of State1, of State2, and of State3 are completely independent of each other.

Yes but my buttons are not isolated.

I don't want that two buttons work simultaneously, i just want the first button work and that's all, even when pushing on another button.

Ok thank you.

I found a solution with the following code.

int State1 = 0;
int State2 = 0;
int State3 = 0;

int Out1 = 0;
int Out2 = 0;
int Out3 = 0;


void setup() {

 Serial.begin(9600);

 pinMode(2,INPUT);
 pinMode(3,INPUT);
 pinMode(4,INPUT);

}

void loop() {

  State1 = digitalRead(2);
  State2 = digitalRead(3);
  State3 = digitalRead(4);

  if (State1+State2+State3 == 0) { 
    Out1=0;
    Out2=0;
    Out3=0;
  }
    else{
      if (State1 == 1 && (State1+State2+State3 < 2)) Out1=1;
      if (State2 == 1 && (State1+State2+State3 < 2)) Out2=1;
      if (State3 == 1 && (State1+State2+State3 < 2)) Out3=1;
    }
  
  Serial.print(Out1);
  Serial.print("   ");
  Serial.print(Out2);
  Serial.print("   ");
  Serial.println(Out3);
 

}

If you have a simple way to do this, let me know!!

      if (State1 == 1 && (State1+State2+State3 < 2)) Out1=1;
      if (State2 == 1 && (State1+State2+State3 < 2)) Out2=1;
      if (State3 == 1 && (State1+State2+State3 < 2)) Out3=1;

Why do you need to add the three variables, and compare to 2 three times?

if(State1+State2+State3 <2)
{
   if(State1 == 1) Out1 = 1;
   if(State2 == 1) Out2 = 1;
   if(State3 == 1) Out3 = 1;
}

You're going to have to detect state changes if you want to ignore additional button presses until the first one activated is released. So a button gets pressed, action happens, and only when that button is released will the function break to read buttons again.

There is another issue lurking under the surface - the order of priority of the buttons (rollover).

I don't want that two buttons work simultaneously, i just want the first button work and that's all, even when pushing on another button.

The first button, or button #1 ?
There are simple methods - which have been suggested above, but you may need to consider whether button X has precedence over button Y or vice versa.