0

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.

enter image description here

per1234
4,2782 gold badges23 silver badges43 bronze badges
asked Dec 12, 2016 at 14:08
8
  • 2
    Chances are there is something wrong with how you built your circuit. Using the same 5V pin for both buttons is perfectly acceptable. Commented Dec 12, 2016 at 14:10
  • Do I need different ground though? I'll edit with a circuit drawing in minute. Commented Dec 12, 2016 at 14:11
  • 2
    All the ground connections are identical and connected together. It makes no difference which ground pin your use. Commented Dec 12, 2016 at 14:12
  • 2
    There is only one ground. It may have multiple pins on it, but there is only one. Use whatever ground pin(s) you fancy, they are all the same. Commented Dec 12, 2016 at 14:12
  • 1
    I hope you don't expect characters '0' .. '3' in the Serial Monitor as you are sending characters: NUL, SOH, STX and ETX (ASCII values from 0 to 3) and they might not be visible. Commented Dec 12, 2016 at 14:53

1 Answer 1

2

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/

answered Dec 12, 2016 at 15:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.