Hey, I am pretty new to programming but i want to write a program that will have two buttons, so if i press button A first and then button B, increment the value. IF i press button B first and then button A, decrement the value. i am using digital pins. can anyone help..
Hi,
connect button 1 to pin3 and GND and button 2 to pin4 and GND.
When a button is pressed, the Arduino reads a 0, otherwise a 1.
#define button1 3 // Button 1 at pin3
#define button2 4 // button 2 at pin4
int b1 = 1;
int b2 = 1;
int b1old = 1; // old state of buttom 1
in b2old = 1; // old state of button 2
int cout = 0; // this value will be increased/decreased
bool b1pressed = false;
bool b2pressed = false;
void setup()
{
pinMode(button1,INPUT);
digitalWRite(button1,HIGH); //pull up on
pinMode(button2,INPUT);
digitalWrite(button2,HIGH); // pull up on
}
void loop()
{
b1 = digitalRead(button1);
b2 = digitalRead(button2);
if (b1old == 1 && b1 == 0) b1pressed = true; // just pressed
else b1pressed = false;
if (b2old == 1 && b2 == 0) b2pressed = true; // just pressed
else b2pressed = false;
if (b1==0 && !b1pressed && b2pressed) count++;
if (b2==0 && !b2pressed && b1pressed) count--;
b1old = b1; // remember the last state
b2old = b2; // remember the last state
}
May be you have to dbounce the buttons, depends on the type of button you use.
Mike
Hi,
Thanks Mike_pa, I will give this a shot.
Hi Mike.. the code works great.. I modified it a little bit but works great. Thank you once again