The reason I don't like working with creepypasta from the internet is that most of the code I've seen is written with a scope so narrow that to expand or modify it's use requires a lot of rewriting the code. For you, I've done just that. I've kept the pins and HIGH/LOW behavior the same(excusing my own human error), with the exception of the button. If you need it to go HIGH to count, you will NOT want to use the internal pullup resistor, which holds it HIGH. instead, you will need an external pull-down resistor from the pin to ground. 10 KOhms should work. I've written the code in a way I hope you can easily follow and modify for your future need.
/* Display Control for 4 anodes and 8 cathodes (for common cathode, see inline comments in displayRefresh())
signal for segments controlled by Arduino Pin Numbers 6-13 as follows
___
| a |
f |___| b
e | g | c
|___| .
d dp
a = 6, b = 7, c = 8, d = 9, e = 10, f = 11, g = 12, dp = 13
signal for commons controlled by Arduino Pin Numbers 2-5 as follows
_ _ _ _
|_| |_| |_| |_|
|_| |_| |_| |_|
d4 d3 d2 d1
d1 = 5, d2 = 4, d3 = 3, d4 = 2;
*/
const byte NumbersLUT[11] = {0xFC, 0x60, 0xDA, 0xf2, 0x66, 0xB6, 0xBE, 0xE0, 0xFE, 0xF6, 0x00};// segment data for numbers 0-9 and blank
const byte SegmentsLUT[8] = {6, 7, 8, 9, 10, 11, 12, 13};// pin numbers for segments
const byte CommonsLUT[4] = {2, 3, 4, 5};// pin numbers for commons
const unsigned long refreshRate = 1000UL;
unsigned long refreshTimestamp;
byte displayData[4] = {0x00, 0x00, 0x00, 0xFC};// stores data from NumbersLUT for each digit
byte digitIndex = 3;// indexes displayData and CommonsLUT to multiplex display
const byte ButtonPin = 14;
int lastButtonState;
unsigned long buttonDebounceTimestamp;
byte ones;
byte tens;
byte hundreds;
byte thousands;
void setup() {
for (byte i = 2; i <= 13; i++) pinMode(i, OUTPUT);// sets pins 2-13 as outputs
pinMode(ButtonPin, INPUT);
}
void loop() {
if (pollbutton()) incrementCount();
displayData[0] = (thousands == 0) ? 0x00 : NumbersLUT[thousands];
displayData[1] = (thousands == 0 && hundreds == 0) ? 0x00 : NumbersLUT[hundreds];
displayData[2] = (thousands == 0 && hundreds == 0 && tens == 0) ? 0x00 : NumbersLUT[tens];
displayData[3] = NumbersLUT[ones];
displayRefresh();
}
bool pollbutton() {
int buttonState = digitalRead(ButtonPin);
if (buttonState != lastButtonState && buttonState == HIGH) {// transition is LOW to HIGH
if (millis() - buttonDebounceTimestamp >= 50UL) {// 50UL = 50ms = time for debounce
lastButtonState = buttonState;
buttonDebounceTimestamp = millis();
return true;
}
}
else {
lastButtonState = buttonState;
return false;
}
}
void incrementCount() {
if (ones >= 9) {
ones = 0;
if (tens >= 9) {
tens = 0;
if (hundreds >= 9) {
hundreds = 0;
if (thousands >= 9) {
thousands = 0;
}
else thousands++;
}
else hundreds++;
}
else tens++;
}
else ones++;
}
void displayRefresh() {
if (micros() - refreshTimestamp >= refreshRate) {
digitalWrite(CommonsLUT[digitIndex], LOW);// HIGH for common cathode
for (int i = 0; i < 8; i++) digitalWrite(SegmentsLUT[i], ((displayData[digitIndex] & (0x01 << i)) ? LOW : HIGH));// swap HIGH and LOW for common cathode
digitIndex = (digitIndex >= 3) ? 0 : (digitIndex + 1);
digitalWrite(CommonsLUT[digitIndex], HIGH);// LOW for common cathode
refreshTimestamp = micros();
}
}