# 6 Buttons 2 pins problem

Hello some where online a while back i found online a way to do 6 push buttons and only need 2 pins but i didn’t get around to it into now. i saved the sketch and how it was wired up but when i go to try it doens’t work i get all zeros from it. just going and going i lost the link to the page so i can’t find it no where can someone please help me with this sketch i have no clue why it is not responding

``````int old_button = 0;
int button;
int pressed_button;
int z;

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

void loop () {
if (z > 1021) button = 0;
else if (z > 511 && z < 514) button = 1;
else if (z > 680 && z < 684) button = 2;
else if (z > 766 && z < 770) button = 3;
else if (z > 817 && z < 822) button = 4;
else if (z > 851 && z < 856) button = 5;
else if (z > 875 && z < 880) button = 6;
else button = 0;

if (old_button == button) {
old_button = button;
pressed_button = 0;
}

else {
old_button = button;
pressed_button = button;
}
Serial.println(pressed_button);
}
``````

You need a ladder of resistors and buttons. That circuit is okay.

Why don’t you use a simple sketch to check the analog value and the button value ?

``````cont int pinAnalogButtons = A5;

void setup () {
Serial.begin(9600);
Serial.println("Started");
}

void loop () {
int button;
Serial.print("value=");
Serial.println(z);

if (z > 1021) button = 0;
else if (z > 511 && z < 514) button = 1;
else if (z > 680 && z < 684) button = 2;
else if (z > 766 && z < 770) button = 3;
else if (z > 817 && z < 822) button = 4;
else if (z > 851 && z < 856) button = 5;
else if (z > 875 && z < 880) button = 6;
else button = 0;

Serial.print("button=");
Serial.println(button);

delay(1500);
}
``````

If it is not working, check all the voltages with a multimeter.

Hello peter I'm not sure i never used push buttons like this not even sure how to do them. i can do a single push buttons with a simple turn on and off led but that is all m first time trying this. i tried your sketch and this is what i got.

Arduino: 1.6.5 (Windows 8.1), Board: "Arduino Uno"

sketch_sep11a.ino:2:1: error: 'cont' does not name a type

sketch_sep11a.ino: In function 'void loop()':

sketch_sep11a.ino:11:22: error: 'pinAnalogButtons' was not declared in this scope

Error compiling.

This report would have more information with "Show verbose output during compilation" enabled in File > Preferences.

Oops, sorry, :-[ It must be "const", not "cont".

Thank you peter for the help it is working. Now just got to figure out with pushing the button it will turn on a led.