!!! Push Button Counter !!! EASY ANSWER PLEASE HELP

Hi All, I want to design a scoreboard, where every time you get a point, you push a button and it counts. (0-21) or (0-99) I would like this to display on 2 seven segment displays, or at least 1 seven segment displays, and have LEDs light up for multiples. so if score is 10, then seven segment would say 0 but 1 LED would be high. I know how to wire the buttons, and even have leds turn on and could prob get the 7 segment wired, but coding isnt my thing.

So this is what I would like to do if possible.

have 3 buttons (Plus, Minus, Reset) for now i will just use 21 LEDS unless someone knows how to use 7 segments. so..

int score would == 0 for a new game. if button (plus button) == high then ++1 to score. if button (minus button) == high then --1 to score. reset would put score to 0.

I have read to make sure the button is debounced so it is accurate. But i could use some help. I have found some similar examples from google, but not that intuitive.

THANKS

Start with the state change detection example. It lets you tell when a switch BECOMES pressed, not IT pressed. You don't want someone holding a switch down, and running up the score.

Thanks for the reply,

This is a great example because it counts the number of times the button is 'pressed'. Do I need to debounce the button, when I pushed the button it counted 0,3,6, instead of 0,1,2.

How do I change the code below so when buttonstate ==1 turn led1, high. then buttonstate == 2 turn led2, high...etc. Or can I use this code to display the button state on 2 seven segment displays?

  lastButtonState = buttonState;  
  // turns on the LED every four button pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
}

How do I change the code below

if(buttonPushCounter == 0)
{
}
else if(buttonPushCounter == 1)
{
}
else if(buttonPushCounter == 2)
{
}
else if(buttonPushCounter == 3)
{
}
else
   buttonPushCounter = 0;

Do I need to debounce the button, when I pushed the button it counted 0,3,6, instead of 0,1,2.

Yes. No. Maybe. Who knows? Only YOU can see your code.

Here is a very simple example of a button used to increase a counter, and change the brightness of a LED. I’m sure you can figure out how to set up two more buttons and have one decrease the counter and the other clear the counter.

const byte LED = 6;
const byte Button = A0;

byte ButtonState;
byte lastState = LOW;
byte count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(Button, INPUT);
}

void loop() {
  ButtonState = digitalRead(Button);

  if(ButtonState && ButtonState != lastState)  // button latch, no debounce needed.
  {
    if(count < 255) // This will check to see if the count is within a range of 0 - 255, and anything over that, it will reset count back to 0. Of course, this will happen anyways because count is a BYTE, and not an int or any other type.
      count += 5; // same as count = count + 5;
    else
      count = 0;
      
    analogWrite(LED, count);
    Serial.println(count);
  } 
  lastState = ButtonState;
}

Do I need to debounce the button, when I pushed the button it counted 0,3,6, instead of 0,1,2.

Yes. No. Maybe. Who knows? Only YOU can see your code.

Why so much snarkiness on these forums?

Short answer: in general, it is wise to debounce buttons. The "lazy" way to debounce a button is to wait a few milliseconds after the button changes state (on to off, or vice versa) before the next time you read it. For many purposes (such as that of a sports scoreboard), using a statement like delay(10); will suffice.

By the way, displaying your scores in binary (if you don't know what binary is, look it up) will be easier to build, but harder to read. I suggest putting together a binary version of the scoreboard, and then once you've got that right, looking up how to use a 7-segment display.

I'm not sure 10ms is long enough as your finger will still be pressed...

100-300ms is more likely to give better results, but delays make things appear laggy.

The basic counting example worked, I modified so it would add by 1 instead of 5. and go to 21 and reset to 0.
I have this displaying through a serial monitor so I know the count works.

How could I display the count on the seven segment displays? If binary is the answer I know binary, but wouldnt know the syntax for the code.

Trying to combine these two codes:

const byte Button = A0;

byte ButtonState;
byte lastState = LOW;
byte count = 0;

void setup() {
  Serial.begin(9600);
  
  pinMode(Button, INPUT);
}

void loop() {
  ButtonState = digitalRead(Button);

  if(ButtonState && ButtonState != lastState)  // button latch, no debounce needed!!!!!
  {
    if(count < 21) // This will check to see if the count is within a range of 0 - 21, and anything over that, it will reset count back to 0. Of course, this will happen anyways because count is a BYTE, and not an int or any other type.
      count += 1; // same as count = count + 1;
    else
      count = 0;
    Serial.println("The Score is: ");
    Serial.println(count);
  } 
  lastState = ButtonState;
}
#define Button 9

int count = 0; // current display count
int val = 0;   // digital input from button


void setup() {               
  pinMode(2, OUTPUT);  
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(Button, OUTPUT);
  Serial.begin(9600);
}
   
    void loop() {
  val = digitalRead(Button);
 if (val == HIGH) {
    count++;
 // write '0'
 Serial.println(count);
 
 if    (count == 0)
{ 
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 digitalWrite(7, 1);
 digitalWrite(8, 0);
}
else if(count == 1)
{ 
 digitalWrite(2, 0);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 0);
 digitalWrite(8, 0);
}
else if(count == 2)
{
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 0);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 digitalWrite(7, 0);
 digitalWrite(8, 1);
}
else if(count == 3)
{
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 0);
 digitalWrite(7, 0);
 digitalWrite(8, 1);
}
else if(count == 4)
{
 digitalWrite(2, 0);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 1);
 digitalWrite(8, 1);
}
else if(count == 5)
{
 digitalWrite(2, 1);
 digitalWrite(3, 0);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 0);
 digitalWrite(7, 1);
 digitalWrite(8, 1);
}
else if(count == 6)
{
 digitalWrite(2, 1);
 digitalWrite(3, 0);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 digitalWrite(7, 1);
 digitalWrite(8, 1);
}
else if(count == 7)
{
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 0);
 digitalWrite(8, 0);
}
else if(count == 8)
{
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 digitalWrite(7, 1);
 digitalWrite(8, 1);
}
else if(count == 9)
{
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 1);
 digitalWrite(8, 1);
}
else
count = 0;
    }
    }

Thanks for all your replies. The first code proves i do not need to debounce the button and works fine, now I need to just print the count to the 7 segment display.

Here is my current code. This works counting 0-9 AND displays on the seven segment display.
I do not know how to add a second button to subtract 1 from the current count. any info on that?
A reset may not be necessary if score is only going to go to 9.

I have a 2 piece seven segment display with 10 pins, do i wire this up as if it was a single 7 segment display.

Here is my current code:

const byte Button = A0;

byte ButtonState;
byte lastState = LOW;
byte count = 0;

void setup() {
  
  Serial.begin(9600);
  
  pinMode(Button, INPUT);
}

void loop() {
  ButtonState = digitalRead(Button);

  if(ButtonState && ButtonState != lastState)  // button latch, no debounce needed!!!!!
  {
    if(count < 9) // This will check to see if the count is within a range of 0 - 9, and anything over that, it will reset count back to 0. Of course, this will happen anyways because count is a BYTE, and not an int or any other type.
      count += 1; // same as count = count + 1;
    else
      count = 0;
    Serial.print("The Score is: ");
    Serial.println(count);
  } 
  lastState = ButtonState;
 
  if    (count == 0)
{ 
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 digitalWrite(7, 1);
 digitalWrite(8, 0);
}
else if(count == 1)
{ 
 digitalWrite(2, 0);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 0);
 digitalWrite(8, 0);
}
else if(count == 2)
{
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 0);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 digitalWrite(7, 0);
 digitalWrite(8, 1);
}
else if(count == 3)
{
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 0);
 digitalWrite(7, 0);
 digitalWrite(8, 1);
}
else if(count == 4)
{
 digitalWrite(2, 0);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 1);
 digitalWrite(8, 1);
}
else if(count == 5)
{
 digitalWrite(2, 1);
 digitalWrite(3, 0);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 0);
 digitalWrite(7, 1);
 digitalWrite(8, 1);
}
else if(count == 6)
{
 digitalWrite(2, 1);
 digitalWrite(3, 0);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 digitalWrite(7, 1);
 digitalWrite(8, 1);
}
else if(count == 7)
{
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 0);
 digitalWrite(8, 0);
}
else if(count == 8)
{
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 digitalWrite(7, 1);
 digitalWrite(8, 1);
}
else if(count == 9)
{
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 1);
 digitalWrite(8, 1);
}
// 
}

You should really use an array for your 7 seg display. 1 byte per number should work with the aid of the bitRead function and a simple FOR loop.

The second button should be self explanatory, I already gave you one example, now do the same for the second button BUT make sure you change the variables to distinguish both buttons. The variables are ButtonState and lastState, make sure you can tell the difference between the buttons.

If using an Array is more efficient then I would like to learn how to do so. I dont know how to code the seven segment using an array unfortunately. Im going to try and add the second button and see if I can have it subtract.

Thanks for all your help.

can you please tell me why you have used count++ ?

count is just the name of the variable used to keep track of the number being displayed. You can use any name you want as long as it is consistent and you know what it does.

Hi All,

First off, I’m more of a Network kind of guy and I am still struggling to swim in the waters of coding. Any help is going to be greatly appreciated.

So I’m playing around with an Arduino, and LCD Shield with six buttons on it.

I’m able to display stuff on it but i’m trying to learn how the button works.

They are on a signal analog port.

int read_LCD_buttons() // read the buttons
{
adc_key_in = analogRead(0);
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 650) return btnUP;
if (adc_key_in < 750) return btnDOWN;
if (adc_key_in < 800) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;

}

So I got the value right for each buttons. Now i’m trying to display default value (tps) such as “0” and use the buttons up and down to increase the displayed value.

int tps; (Default Value)

Also, when I press Select, I would like to save the actual value in another one.

int tps2 (Selected Value)

I don’t know if my logic is correct of if Im way off the rail but could anyone give me some insight ?

Thanks

First off, why did you attach your query to an old post ?

Have you got any code other than the function you posted ?

hi @palumbob413,

can you provide me push button counter+reset codes for display HT1621.

HT 1621 is 6 digit LCD module.

Thanking you,

https://images.app.goo.gl/2sSTJVGERoq4ijwE7

palumbob413 contributed as a guest in 2014.

Do you seriously think that he/she is still around ?