user29051: I am trying modify this setup for two push buttons https://www.arduino.cc/en/Tutorial/Button.
The code seems rather trivial
int yellowButtonPin = 2;
int blueButtonPin = 4;

int yellowButtonState = 0;
int blueButtonState = 0;


void setup(){
  Serial.begin(9600);
  pinMode(yellowButtonPin,INPUT);
  pinMode(blueButtonPin,INPUT);
}

void loop(){
  yellowButtonState = digitalRead(yellowButtonPin);
  blueButtonState = digitalRead(blueButtonPin);
  if (yellowButtonState == HIGH && blueButtonState == HIGH){
    Serial.write(3);
  }
  else if (yellowButtonState == HIGH && blueButtonState == LOW){
    Serial.write(2);
  }
   else if (yellowButtonState == LOW && blueButtonState == HIGH){
    Serial.write(1);
  }
  else {
    Serial.write(0);
  }
}

My problem is that there are several ground pins on the Arduino, but only one 5V pin, so I can't just simple double the circuits. Connecting the two circuits to different grounds but the same 5V didn't work out, although it seemed to be a good idea. How should I do this properly? To be honest, I actually would want to have three buttons, but I guess going from two to three will be trivial after going from one to two.


user29053: Learn about:
Built-in pullup resistors in Arduino
Debouncing
and checks this code: https://blog.adafruit.com/2009/10/20/example-code-for-multi-button-checker-with-debouncing/

29051: <p>I am trying modify this setup for two push buttons <a href="https://www.arduino.cc/en/Tutorial/Button" rel="nofollow noreferrer">https://www.arduino.cc/en/Tutorial/Button</a>.</p>

<p>The code seems rather trivial</p>

<pre class="lang-cpp prettyprint-override"><code>int yellowButtonPin = 2;
int blueButtonPin = 4;

int yellowButtonState = 0;
int blueButtonState = 0;


void setup(){
  Serial.begin(9600);
  pinMode(yellowButtonPin,INPUT);
  pinMode(blueButtonPin,INPUT);
}

void loop(){
  yellowButtonState = digitalRead(yellowButtonPin);
  blueButtonState = digitalRead(blueButtonPin);
  if (yellowButtonState == HIGH &amp;&amp; blueButtonState == HIGH){
    Serial.write(3);
  }
  else if (yellowButtonState == HIGH &amp;&amp; blueButtonState == LOW){
    Serial.write(2);
  }
   else if (yellowButtonState == LOW &amp;&amp; blueButtonState == HIGH){
    Serial.write(1);
  }
  else {
    Serial.write(0);
  }
}
</code></pre>

<p>My problem is that there are several ground pins on the Arduino, but only one 5V pin, so I can't just simple double the circuits. Connecting the two circuits to different grounds but the same 5V didn't work out, although it seemed to be a good idea. How should I do this properly? To be honest, I actually would want to have three buttons, but I guess going from two to three will be trivial after going from one to two.</p>

<p><a href="https://i.stack.imgur.com/OjbNP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OjbNP.png" alt="enter image description here"></a></p>
 29053: <p>Learn about:</p>

<p>Built-in pullup resistors in Arduino
Debouncing</p>

<p>and checks this code: <a href="https://blog.adafruit.com/2009/10/20/example-code-for-multi-button-checker-with-debouncing/" rel="nofollow noreferrer">https://blog.adafruit.com/2009/10/20/example-code-for-multi-button-checker-with-debouncing/</a></p>
