Adding and minus ?

Hi everyone, could anyone show me or write me a code in arduino which uses 2 buttons, when button 1 is pressed the value increase by 1 every press and when button 2 pressed the value will decrease by 1 every press? I am not very good at using arduino and I'm also new to it. Thanks in advance.
emerson 

sort by: active | newest | oldest
int ctr;

ctr=0;

if (btn1==true) and (btn2==false) {ctr++}
if (btn1==false) and (btn2==true) {ctr--}
while (btn1==true) or (btn2==true) do{};

///missing code to set limits on the ctr

huydoan240491 (author)  steveastrouk4 years ago
thanks
huydoan240491 (author)  steveastrouk4 years ago
hi steve, thanks for your reply but it doesnt seem like what i wish for. i want something like this, but it didn't work. could you help me on this?
photo.JPG
That's not the code I showed you.
Try mine. It needs to be put in a loop statement. Make my btn1 and 2 into your swit1 and 2
maewert4 years ago
In addition to what Steve said... how did you wire up your buttons? You need good 5v - ground inputs when the buttons are pressed/released. One way to accomplish this is to provide resistors that pull up or pull down the input line when the button is not being pressed. The Arduinos have internal pull up resistors that can be activated by writing HIGH to the input pins within your setup. Looking at your code though it looks like you give the pins 5v when the button is pressed, therefore you can't use the internal pull up resistors but have to add external ones (or change your wiring so you set the pin to ground when the button is pressed).

See the figure showing the top way and the bottom two alternatives.
Best Wishes
buttons.JPG
huydoan240491 (author)  maewert4 years ago
i got it thanks