# 2 7 segment display counter with 1 pushbutton

Hello all,

im trying to make a 00-99 counter that increments by 2 for a basketball score keeper project. The problem is: i get the Arduino uno to read the push button and set the score as 2 then display two on the LEDs, but when i push it again it does not jump from 2 to 4. not sure if it isnt looping or if its just getting stuck on 2? here is the loop and the commands to put it on the LED.

``````void loop()
{
buttonVal = digitalRead(button); //checks button

if (buttonVal == HIGH){ //if button is pressed
score = score + 2; //adds 2 to the score
counter();
}

void counter(){
if(score == 2){
offf();
two();
}
if(score == 4){
offf();
four();
}
}
``````

What does offf() do? What does two() do? What does four() do? Post ALL your code, not just a snippet. And try balancing your curly braces...you haven't closed loop() before defining counter().

Sorry,

the off() two() etc. sets the leds on. example:

``````void off(){ //turns off all lights
digitalWrite(G, HIGH);
digitalWrite(F, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, HIGH);
delay(10);
``````

and the curly brackets are correct on my actual code.. copy and paste error here

I guess you missed the "ALL your code" portion of the response...

You don't want to increment it when the button is HIGH, because it will likely be HIGH for thousands of iterations before the button is released. Look at the StateChangeDetection example.

``````const int A = 0;
const int B = 1;
const int C = 2;
const int D = 3;
const int E = 4;
const int F = 5;
const int G = 6;
const int AA = 7;
const int BB = 8;
const int CC = 9;
const int DD = 10;
const int EE = 11;
const int FF = 12;
const int GG = 13;
const int button = A1; //creates aninstance of button

int score;
int buttonVal = 0; //holds the buttons current value
int buttonOldVal = 0; //holds the buttons previous value

void setup() {
digitalWrite(G, HIGH);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
digitalWrite(GG, HIGH);
digitalWrite(FF, LOW);
digitalWrite(AA, LOW);
digitalWrite(BB, LOW);
digitalWrite(EE, LOW);
digitalWrite(DD, LOW);
digitalWrite(CC, LOW);
pinMode(0,OUTPUT);
pinMode(1,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(A1,INPUT);
}

void loop(){
buttonVal = digitalRead(button); //checks button

if (buttonVal == HIGH){ //if button is pressed
score = score + 2;
}
}

void counter(){
if(score == 2){
offf();
two();
delay(1000);
}
if(score == 4){
offf();
four();
delay(1000);
}
if(score == 6){
zero();
six();
delay(1000);
}
}

void off(){ //turns off all lights
digitalWrite(G, HIGH);
digitalWrite(F, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, HIGH);
delay(10);
}

void offf(){ //turns off all lights
digitalWrite(GG, HIGH);
digitalWrite(FF, HIGH);
digitalWrite(AA, HIGH);
digitalWrite(BB, HIGH);
digitalWrite(EE, HIGH);
digitalWrite(DD, HIGH);
digitalWrite(CC, HIGH);
delay(10);
}

void zero(){ //shows the number zero
digitalWrite(G, HIGH);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
delay(10);
}
void zeroo(){ //shows the number zero
digitalWrite(GG, HIGH);
digitalWrite(FF, LOW);
digitalWrite(AA, LOW);
digitalWrite(BB, LOW);
digitalWrite(EE, LOW);
digitalWrite(DD, LOW);
digitalWrite(CC, LOW);
delay(10);
}

void one(){ //shows the number one
digitalWrite(G, HIGH);
digitalWrite(F, HIGH);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, LOW);
delay(10);
}

void onee(){ //shows the number one
digitalWrite(GG, HIGH);
digitalWrite(FF, HIGH);
digitalWrite(AA, HIGH);
digitalWrite(BB, LOW);
digitalWrite(EE, HIGH);
digitalWrite(DD, HIGH);
digitalWrite(CC, LOW);
delay(10);
}

void two(){ //shows the number two
digitalWrite(G, LOW);
digitalWrite(F, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
digitalWrite(C, HIGH);
delay(10);
}

void twoo(){ //shows the number two
digitalWrite(GG, LOW);
digitalWrite(FF, HIGH);
digitalWrite(AA, LOW);
digitalWrite(BB, LOW);
digitalWrite(EE, LOW);
digitalWrite(DD, LOW);
digitalWrite(CC, HIGH);
delay(10);
}

void three(){ //shows the number three
digitalWrite(G, LOW);
digitalWrite(F, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
delay(10);
}

void threee(){ //shows the number three
digitalWrite(GG, LOW);
digitalWrite(FF, HIGH);
digitalWrite(AA, LOW);
digitalWrite(BB, LOW);
digitalWrite(EE, HIGH);
digitalWrite(DD, LOW);
digitalWrite(CC, LOW);
delay(10);
}

void fourr(){ //shows the number four
digitalWrite(GG, LOW);
digitalWrite(FF, LOW);
digitalWrite(AA, HIGH);
digitalWrite(BB, LOW);
digitalWrite(EE, HIGH);
digitalWrite(DD, HIGH);
digitalWrite(CC, LOW);
delay(10);
}

void four(){ //shows the number four
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, LOW);
delay(10);
}

void five(){ //shows the number five
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(E, HIGH);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
delay(10);
}

void fivee(){ //shows the number five
digitalWrite(GG, LOW);
digitalWrite(FF, LOW);
digitalWrite(AA, LOW);
digitalWrite(BB, HIGH);
digitalWrite(EE, HIGH);
digitalWrite(DD, LOW);
digitalWrite(CC, LOW);
delay(10);
}

void six(){ //shows the number six
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
delay(10);
}

void sixx(){ //shows the number six
digitalWrite(GG, LOW);
digitalWrite(FF, LOW);
digitalWrite(AA, LOW);
digitalWrite(BB, HIGH);
digitalWrite(EE, LOW);
digitalWrite(DD, LOW);
digitalWrite(CC, LOW);
delay(10);
}

void seven(){ //shows the number seven
digitalWrite(G, HIGH);
digitalWrite(F, HIGH);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, LOW);
delay(10);
}

void sevenn(){ //shows the number seven
digitalWrite(GG, HIGH);
digitalWrite(FF, HIGH);
digitalWrite(AA, LOW);
digitalWrite(BB, LOW);
digitalWrite(EE, HIGH);
digitalWrite(DD, HIGH);
digitalWrite(CC, LOW);
delay(10);
}

void eight(){ //shows the number eight
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, LOW);
digitalWrite(D, LOW);
digitalWrite(C, LOW);
delay(10);
}

void eightt(){ //shows the number eight
digitalWrite(GG, LOW);
digitalWrite(FF, LOW);
digitalWrite(AA, LOW);
digitalWrite(BB, LOW);
digitalWrite(EE, LOW);
digitalWrite(DD, LOW);
digitalWrite(CC, LOW);
delay(10);
}

void nine(){ //shows the number nine
digitalWrite(G, LOW);
digitalWrite(F, LOW);
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(E, HIGH);
digitalWrite(D, HIGH);
digitalWrite(C, LOW);
delay(10);
}

void ninee(){ //shows the number nine
digitalWrite(GG, LOW);
digitalWrite(FF, LOW);
digitalWrite(AA, LOW);
digitalWrite(BB, LOW);
digitalWrite(EE, HIGH);
digitalWrite(DD, HIGH);
digitalWrite(CC, LOW);
delay(10);
}
``````

all of my code

``````void loop(){
buttonVal = digitalRead(button); //checks button

if (buttonVal == HIGH){ //if button is pressed
score = score + 2;
}
}
``````

counter() is never called, so I don't see how you can expect your display to update...

it was in there.. i been changing stuff alot and forgot to retype it in there..

if i dont want it to be high what would u recomend it?

Silveira89: if i dont want it to be high what would u recomend it?

Take the joint away?

What don't you want to be high? Try not to use pronouns like "it".

You don't want to increment it when the button is HIGH, because it will likely be HIGH for thousands of iterations before the button is released. Look at the StateChangeDetection example.

how would i get it to increment without reading when the button is high? sorry at work, multitasking isnt my thing appearently

Silveira89: how would i get it to increment without reading when the button is high? sorry at work, multitasking isnt my thing appearently

Look at the last sentence in the post you just quoted.