Hello everybody. I'm using A5 to read presses of 6 buttons and then printing what was pressed. The problem is that sometimes program registrates one press of a button as a couple of presses. Say I press first button twice, program sometimes will print number 1 (first button) three or four times. I suppose that it should be debounced, but i don't know how to do it on analog pin. On the picture, buttons go 1 to 6 from right to left.
int old_button = 0;
int button;
int pressed_button;
int z;
void setup () {
pinMode(A5, INPUT);
Serial.begin(9600);
}
void loop () {
z = analogRead(A5);
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) {
pressed_button = 0;
}
else {
old_button = button;
pressed_button = button;
}
Serial.println(pressed_button);
}