1 clicked button acts as 3 clicked buttons

Hi, I want to make a small piano with the arduino that works with a piezo buzzer.
I made 3 buttons (that are connected through a resistor to pins 2,3 and 4) and after the buttons the pins are connected to the ground, when they are pushed down they are connected to the 5v of the arduino (pulldown resistor buttons).
the piezo buzzer is connected to pin 9
i made the console print the input of the pins (0 or 1) in the void loop.
when i click the buttons that are connected to pin 2 and 3 it says that i clicked them all (shows 111 for pin2,pin3,pin4)
but when i click the button that is connected to pin 4 it says that i clicked only that button (shows 001) which is what i want it to do.
the code is:

int speakerOut = 9;               
int C;
int D;
int E;

void setup() {
 pinMode(speakerOut, OUTPUT); 
 pinMode(2,INPUT);
 pinMode(3,INPUT);
 pinMode(4,INPUT);
 beginSerial(9600);
}

void loop() {
 C = digitalRead(2);
 D = digitalRead(3);
 E = digitalRead(4);
  if (digitalRead(2) == 1) {
      Hertz(1915);
  }
if (digitalRead(3)==1) {
      Hertz(1700);
}
if (digitalRead(4)==1) {
      Hertz(1519);
}
Serial.print(digitalRead(2));
Serial.print(digitalRead(3));
Serial.println(digitalRead(4));
}

void Hertz(int hz) {
        digitalWrite(speakerOut, HIGH);
      delayMicroseconds(hz);
      digitalWrite(speakerOut, LOW);
      delayMicroseconds(hz);
}

Anyone knows what is wrong?
Thanks :slight_smile:

Please post a schematic or something that shows how the buttons are hooked up.

I dont really know how to draw a schematic so i made it like it is on the breadboard, i hope you understand it.

You can see that all three of your input pins are basically connected to each other, right? Remove all of the resistors, enable the internal pull-ups on your three pins, connect one side of each button to ground and the other side to the pin. When the button is pushed the input value on the pin will be LOW, when the button is not pushed its input value will be HIGH.

  • Ben

i dont really understand... can you draw it or something?

I don't have a convenient way of drawing this, but it's pretty much the simplest circuit possible: each pin goes through its own button to ground. If you can't envision what this would look like on a breadboard, I think you should first take some time to look through some basic eletronics tutorials to get a better feel for electronics before jumping in.

In your code, set the pin mode to INPUT (this is its mode by default) and do a digitalWrite(pin, HIGH) for each of the three button pins to enable their internal pull-ups (i.e. this weakly pulls them to 5 V so they will never be floating). Note that you could destroy your pin if you do this with your pin set as an OUTPUT as you'll be shorting power to ground every time you push a button.

  • Ben

cut the black leads from ground to the buttons, OR better yet, do like Ben is saying, connect each pin to ground THROUGH a button, ditch the resistors, and turn on the internal pullup resistors after you set the pins to "INPUT". Then invert the results (1=0, 0=1).