pushbutton counter on 4 digit 7 segment display

Hello,

New to the forum and brand new to everything Arduino. I have some really basic programming knowledge but not enough to be considered dangerous.

Anyway, I have a 4 digit display and I would like to us 2 push buttons for each digit, one button to count up and one button to count down.

I found the code below in a tutorial online and I tweaked it to make it work for any single digit but I can't figure out how to copy the code to the other 3 digits.

Any help appreciated.

//Initialize the 7 segment pins
int A = 2;
int B = 3;
int C = 4;
int D = 5;
int E = 6;
int F = 7;
int G = 8;
int DP = 11;
int D1 = 9;
int D2 = 10;

//Initialize the push buttons pins, push buttons states, and the counter
int switchUpPin1 = A0;
int switchDownPin1 = A1;
int counter = 0;
int buttonUpState1 = 0;
int lastbuttonUpState1 = 0;
int buttonDownState11 = 0;
int lastbuttonDownState11 = 0;

void setup()
{
Serial.begin(9600);

pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);

pinMode(D1, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
digitalWrite(A0, LOW);
digitalWrite(A1, LOW);
digitalWrite(D1, HIGH);

}

void loop()
{
//Getting the reads from the buttons
buttonUpState1 = digitalRead(switchUpPin1);
buttonDownState11 = digitalRead(switchDownPin1);

//Detecting button press and getting the button status
//Do this for the button up
if (buttonUpState1 != lastbuttonUpState1)
{
if (buttonUpState1 == HIGH)
{
//Reset the counter to -1
if(counter == 9)
{
counter = -1;
}
//Increase the counter by 1
counter++;
//Print the counter to the console and calling the function
Serial.println(counter);
changeNumber(counter);
//Delaying by 250 ms
delay(250);
}
else
{
Serial.println("OFF");
}
//Delay to avoid button bouncing
delay(50);
}

//Do this for the button down
if (buttonDownState11 != lastbuttonDownState11)
{
if (buttonDownState11 == HIGH)
{
//Set the counter to 10
if(counter == 0)
{
counter = 10;
}
//Decreases the counter by 1
counter--;
////Print the counter to the console and calling the function
Serial.println(counter);
changeNumber(counter);
//Delaying by 250 ms
delay(250);
}
else
{
Serial.println("OFF");
}
//Delay to avoid button bouncing
delay(50);
}
//Calling the function changeNumber with the arg counter
changeNumber(counter);
}

//The function to display the numbers
void changeNumber(int buttonPress)
{
switch (buttonPress)
{

//number 0
case 0:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
//number 1
case 1:
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
//number 2
case 2:
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
break;
//number 3
case 3:
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
break;
//number 4
case 4:
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
break;
//number 5
case 5:
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
break;
//number 6
case 6:
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
break;
//number 7
case 7:
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
//number 8
case 8:
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
break;
//number 9
case 9:
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
break;
}
}

7 segment displays are typically multiplexed, set the outputs for one digit, ground the common lead to that digit, then do same to next digit. when you do this faster than 60 times a second (TV frame rate), you won't see any flicker

with only 2 buttons, why not use one button to increment the value, wrapping from 9 back to zero and use the 2nd button to switch between digits

Thanks for the response. I would like to increment and decrement the displayed value so I need 2 buttons for that. I didn't think about using a button to change from digit to digit, but I have no idea how to do it.

csracing:
I didn't think about using a button to change from digit to digit, but I have no idea how to do it.

how many digits do you display? how do you keep track of the value of each digit?

are you going to need 2 more buttons top move right as well as left? do you really need a button to decrement a digit? is that the only way to reach a digit value?

csracing:
Thanks for the response. I would like to increment and decrement the displayed value so I need 2 buttons for that. I didn't think about using a button to change from digit to digit, but I have no idea how to do it.

1. I hope that the structure of your '4-digit cc-type multiplexed display unit' and the associated 'INC/DEC button's would be like Fig-1
4digit7segment.png
Figure-1:

2. You may now operate/drive the display unit if Fig-1 using SevSeg.h Library in a convenient way. SevSeg.h Library embeds all the operations (and more) that are being done by the discrete codes of the sketch that you have posted.

3. Include the Library of Step-2 in your IDE; open an example from the Library; follow the codes of the example; try to prepare a sketch of your own to show 1234 on the display unit of Fig-1. If it works, it is fine; if not working, post the sketch for possible correction by the Forum members.

4. Once Step-3 is done, you may add codes with the sketch of Step-3 to increment the displayed value when INC (increment) button is pressed.

5. Once Step-4 is done, you may add codes with the sketch of Step-4 to decrement the displayed value when DEC (decrement) button is pressed.

SevSeg-master.zip (20.7 KB)

4digit7segment.png

csracing:
I would like to increment and decrement the displayed value so I need 2 buttons for that.

you don't need 2 buttons to both increment and decrement a digit to change it to any value