Author Options:

How to make Arduino "remember" button presses? Answered

Ok so I have a rocker switch. Every time it connects, I wan't Arduino to remember how many times I pressed the button. Then I wan't to be able to multiply a number (say 15) by the amount of button presses.


The rocker switch is no different then a button. So you can use it with the button sketch. You connect it the same way.

Yeah, that's what I tried, but it isn't working. I'll just have to disconnect the circuitry from the switch.

So, every time the switch is closed, I want Arduino to be able to count how many times it was closed. So say I push the button five times, then I want Arduino to print that data to serial monitor, then eventually to an LCD. ??

Um.. this is my problem. I have no code! Right now all I have is the button sketch from Arduino

Put a resistor from + supply, say 4.7K to one side of the switch, and then to your arduino, put the other side of the switch to ground. That's your basic input unit.

In your code.
something LIKE

ctr=0; //your counter
while (HIGH)
while (digitalRead(switch)==HIGH) ; //wait while switch is off
ctr++ //increment your counter //switch must now be ON
while (digitalRead(switch)==LOW); //wait for switch to be released
serial.print (myAnswer);

Ok, this is starting to make sense. What is ctr?? What do you mean by your counter?? Also you you said, // wait while switch is off, then you said // wait for switch to be released. released is off, right? Please explain.

ctr, the variable that is your counter, the thing you are putting numbers into. off is released, on is depressed.

Hmm, ok. Using my horrible Arduino code logic, can you change ctr to something else? At the top, it says, ctr=0; so, my using horrible logic, I could change it to btr=0; the same thing???

I've read the Arduino page on = and == but didn't really get it. Could you explain?


Yep. It can be anything.
Somewhere though you have to DECLARE the variable

int btr;


int btr=0; //which initialises it as well.

Difference between = and == is tricky to explain.

= means "is assigned to", so read something like this

should be read as "btr is assigned the value 5"

THIS though, if (btr==5) {} is READ as you would expect, and asks the question IF the contents of btr is equal to 5 then do something.


Oh Ok I understand!

Thanks Steve!
What would I do without you guys??

Start with just the switch. If there is any additional circuitry on it remove it. Then you can connect it just like you would with the button sketch. The code is essentially the same with only a couple of additions.

In the sketch you will need to add a vaiable to track the number of button presses. So right under where the sketch establishes the buttonstate variable ad a new one. In this case we'll call it buttoncount. No in the void loop section of the sketch we'll remove the lines that control the LED or keep them if you like. In the 'if' statement checking the buttonstate add the line buttoncount ++; . This will ad 1 to the buttoncount each time the button is pressed or if it detects the button being held down over multiple loops through the sketch.

Are you sure that the switch is not outputing negative voltage? because an Arduino cannot read negative voltage. Try this as your circuit:
Arduino Pin 2 --------/ ----------- +5V (negative or positive will not make a difference unless it is in a different circuit) If you gave more information on the circuit it would help alot! Thanks