# Two button counter

Hello everyone.. This is my first post on the Arduino forums so thanks in advance for all the help!!

I want to develop a simple two button counter using the Arduino Uno. I have a really cool idea to put this to use and will certainly share it with everyone when I'm finished, but first I want to test it and see how everything would work so I can build the rest of the enclosure for it.

Here is what I'm trying to achieve...

I need there to be 2 buttons and a little LCD screen to output some data. The user can push 'button 1' or 'button 2' as their data inputs. I want the Arduino to count up the amount of times 'button 1' was pushed along with 'button 2' and then output the ratio of 'button 1' to 'button 2' on the LCD screen.

So here is an example... If I push 'buttonOne' 10 times and 'buttonTwo' 10 times for a total of 20 pushes - the LCD should show me that I have a 50% 'buttonOne' ratio compared to 'buttonTwo'. Obviously this will change if I push any one button more times than the other. So if I push 'buttonOne' 15 times and 'buttonTwo' only 5 times to equal 20 pushes - then the LCD would say I have a 75% 'buttonOne' ratio and a 25% 'buttonTwo' ratio.

I know a little programming and I'm pretty good with circuitry and electronics, so I think this is something I can put together on my own, I just don't know where to start yet and what the equation would look like to program.

Can any of you help me with this?

I need there to be 2 buttons and a little LCD screen to output some data.

This is a simple matter of buying the right hardware.

The user can push 'button 1' or 'button 2' as their data inputs. I want the Arduino to count up the amount of times 'button 1' was pushed along with 'button 2' and then output the ratio of 'button 1' to 'button 2' on the LCD screen.

Two analogRead statements, one for each switch, will tell you whether the switch is pressed, or not. Some debouncing and saving of the previous state, will allow you to determine if the switch transitioned from released to pressed. If so, increment a counter.

and what the equation would look like to program.

Yes, you do. You showed that in your examples.

http://arduino.cc/en/Tutorial/HomePage Plenty of useful material here.

This is a simple matter of buying the right hardware.

I already have most of the hardware. I just need to pick up the LCD screen from Radio Shack.

Two analogRead statements, one for each switch, will tell you whether the switch is pressed, or not. Some debouncing and saving of the previous state, will allow you to determine if the switch transitioned from released to pressed. If so, increment a counter.

I understand most of what you said here.. But my issue is I don't even know where to begin lol. I haven't programmed much in C - I'm more proficient in web programming (php, html, css, js).

Yes, you do. You showed that in your examples.

This is something else I need some help with. I'm also thinking I might need a third button to clear the count and start over.

Start at the beginning. Like PaulS said there are plenty of examples here to help you out.

Get the basic 'blink' etc. programs working until you understand what they're doing. Then learn how to handle buttons, then learn the LCD library, etc. Basically keep breaking the project down into smaller pieces until you can handle them. Don't bite off more than you can chew -- for example until you've got your buttons working perfectly don't worry about the LCD, just use Serial.print()s and the IDE monitor.

Good luck!

[quote author=Chris Magagna link=topic=77270.msg583695#msg583695 date=1319995225] Start at the beginning. Like PaulS said there are plenty of examples here to help you out.

Get the basic 'blink' etc. programs working until you understand what they're doing. Then learn how to handle buttons, then learn the LCD library, etc. Basically keep breaking the project down into smaller pieces until you can handle them. Don't bite off more than you can chew -- for example until you've got your buttons working perfectly don't worry about the LCD, just use Serial.print()s and the IDE monitor.

Good luck! [/quote]

Yea I agree.. I've already done the blink programs and a few others to get the feel of Arduino. I've also been playing around with whatever programs I can find that are similar (which is not much), I'm just excited to get this project going so I'm being a little impatient right now lol. If anyone has any links to any other programs that are similar to this type of idea - it would greatly help.

The problem is I don't have much spare time (due to client projects) to work on Arduino projects to learn, so I was hoping to speed things up a bit with some help from the community.

I'd go for digitalRead to get the switch state, not analogRead

You could start with one of the button libraries, that way you won’t have to deal with debouncing etc. yourself. Here’s one:

http://www.arduino.cc/playground/Code/Button

After that your code is going to be pretty simple…

``````if (button1.uniquePress()) {
button1Counter++;
}
if (button2.uniquePress()) {
button2Counter++;
}
ratio1 = 100 * button1 / button2;
ratio2 = 100 * button2 / button1;
lcd.clear();
lcd.print(ratio1);
lcd.print(ratio2)
``````

Obviously this isn’t real code…for example you’ll need to deal with cases where the buttons have been pressed zero times, so you don’t get divide by zero errors…

Good luck!

[quote author=Chris Magagna link=topic=77270.msg583733#msg583733 date=1319997797] You could start with one of the button libraries, that way you won't have to deal with debouncing etc. yourself. Here's one:

http://www.arduino.cc/playground/Code/Button

After that your code is going to be pretty simple...

``````if (button1.uniquePress()) {
button1Counter++;
}
if (button2.uniquePress()) {
button2Counter++;
}
ratio1 = 100 * button1 / button2;
ratio2 = 100 * button2 / button1;
lcd.clear();
lcd.print(ratio1);
lcd.print(ratio2)
``````

Obviously this isn't real code...for example you'll need to deal with cases where the buttons have been pressed zero times, so you don't get divide by zero errors....

Good luck! [/quote]

Yea.. I was just looking at these libraries - I'm going to play around with this today and get a feel of how to adapt the button libraries to fit my project. I was going to go to Radio Shack to look at LCD screens but I'm not sure if those will work or not. Does anyone have any good recommendations for LCD's from Radio Shack? Or should I just order from like Sparkfun or one of the other Arduino distributors? I want to make sure I get an LCD screen that works with Arduino, so I'm a bit reluctant to try the Radio Shack ones because of compatibility issues.

The other part I'm having issues with is figuring out how to get the ratio of button pushes compared to the other. I've got so much to think about that I feel like I'm over-thinking this equation.

how to get the ratio of button pushes compared to the other.

ratio = float(buttonACount) /float( ButtonBCount); where ratio is a floating point variable, and the button counts are integers.

I would forget the button library, using a library for something that simple is just stupid.

I got the basics of the two button counter going. Here is the code...

``````// constants - wont change
const int button1Pin = 2;
const int button2Pin = 3;

// variables - that will change
int button1PushCounter = 0;
int button1State = 0;
int lastButton1State = 0;
int button2PushCounter = 0;
int button2State = 0;
int lastButton2State = 0;

void setup () {
// initiliazes button1 as input
pinMode (button1Pin, INPUT);

// initiliazes button2 as input
pinMode (button2Pin, INPUT);

// initiliazes serial com
Serial.begin(9600);
}

void loop () {
// read the push button input pins
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);

// compare the button1State to its previous state
if (button1State != lastButton1State) {

// if button1 state has changed, increment counter
if (button1State == HIGH) {
// if button1 state is HIGH then the button1 went from off to on
button1PushCounter++;
Serial.println("on");
Serial.print("Number of button1 pushes: ");
Serial.println(button1PushCounter, DEC);
}
else {
// if the current state is low then the button went from on to off
Serial.print("off");
}
}

// compare the button2State to its previous state
if (button2State != lastButton2State) {

// if button2 state has changed, increment counter
if (button2State == HIGH) {
// if button2 state is HIGH then the button1 went from off to on
button2PushCounter++;
Serial.println("on");
Serial.print("Number of button2 pushes: ");
Serial.println(button2PushCounter, DEC);
}
else {
// if the current state is low then the button went from on to off
Serial.print("off");
}
}

// save the current state as the last state for next time through loop
lastButton1State = button1State;
lastButton2State = button2State;

}
``````

I have the breadboard setup with 2 buttons on it and I'm using the serial monitor to see what the count is after I push each button. At this point, the Arduino keeps a running count of the total amount of button pushes for each 'button1' and 'button2' and displays it on the serial monitor after each push. I just ordered an LCD from Sparkfun so I'll work on getting it to display on the LCD when that comes in.

My next challenge is to add a third button, so when 'button3' is pushed - the serial monitor will display the ratio of 'button1' pushes to 'button2' pushes and displays that on the serial monitor. Any help on this would be great, I haven't found much on ratios yet and I'm still trying to figure out how to get that working and to display it.

Also, how would I get the Arduino to store these values until I push a fourth button ('button4') to clear the values and reset the counter? Right now, if I power it off - it resets and the count starts over again.

Is there anyway to make it auto-power-off, like a calculator? And then when any button is pushed, it automatically comes back on?

Right now, if I power it off - it resets and the count starts over again.

Yes that is how it works. You can use EEPROM to store values that are persistent over power cycles.

Is there anyway to make it auto-power-off, like a calculator? And then when any button is pushed, it automatically comes back on?

Look up sleep mode but you will also need to power it by batteries.

I haven't found much on ratios yet and I'm still trying to figure out how to get that working and to display it.

What did you not understand about my last post?

What did you not understand about my last post?

Where would I put that line of code? How do I implement that into what I've already done? Also, how do I get ratio to display on the serial monitor when a third button is pushed?

I tried to insert that code you mentioned in the code in a few different places and I kept getting syntax errors.

So that's why I asked if anyone can shed some more light on the topic.

I tried to insert that code you mentioned in the code in a few different places and I kept getting syntax errors.

So, show us your code.

So, show us your code.

4 posts up.. I posted it there.

That code compiles - what's the problem?

Yea.. It compiles and works to count button1 and button2 pushes.

My issue is that I want to add in a third button (button3) and when it's pushed, it will output the ratio of button1 pushes to button2 and display it on the serial monitor. I'm a bit confused as to how to get this next step done.

Break the problem down into smaller steps:

1. Calculate the ratio
2. Display the ratio

Break the problem down into smaller steps: 1) Calculate the ratio 2) Display the ratio

I already know this.. My issue is I don't know the lines of code to add to get the Arduino to calculate the ratios for me and where to put them.

I think I know how to get it to display on the serial monitor, I would guess it's similar to how the button counter is displayed on the serial monitor, but I wouldn't be able to start this step without know how to apply the ratio function first.

Did you read reply #5?

Yea.. I played around with that a bit too and I couldn't get it working.

The poster mentioned that this wasn't usable code without doing some other things to it - I feel like I'm missing something with this so that's why I'm asking for the help.

I'm pretty good at figuring things out like this, I am good with logic, but I have been playing around with this for a while now and I feel like I'm not getting anywhere because something is missing.