6 buttons and analog pin
hello everybody. i'm using a5 read presses of 6 buttons , printing pressed. problem program registrates 1 press of button couple of presses. press first button twice, program print number 1 (first button) 3 or 4 times. suppose should debounced, don't know how on analog pin. on picture, buttons go 1 6 right left.
code: [select]
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);
}
you have no delay in sketch, loop() delayed serial.println().
you add timing , debounce that, using analog input makes harder.
what if delay() after button pressed. quick , dirty, might little.
you add timing , debounce that, using analog input makes harder.
what if delay() after button pressed. quick , dirty, might little.
code: [select]
....
else button = 0;
if (button != 0)
delay( 20);
if (old_button == button) {
....
Arduino Forum > Using Arduino > Programming Questions > 6 buttons and analog pin
arduino
Comments
Post a Comment