const int digitPins[4] = {4, 19, 21, 22};
const int clockPin = 18;
const int latchPin = 5;
const int dataPin = 23;
const int secondIndicatorPin = 15;
const int buttonPin1 = 12; //2
const int buttonPin2 = 16; //1
const int resetButtonPin = 14; //3
const int reverseCountPin = 2; //l
const int ledPin = 17; //r
const byte digit[10] = {
B00111111, // 0
B00000110, // 1
B01011011, // 2
B01001111, // 3
B01100110, // 4
B01101101, // 5
B01111101, // 6
B00000111, // 7
B01111111, // 8
B01101111 // 9
};
int digitBuffer[4] = {0};
int digitScan = 0;
int buttonState1 = HIGH;
int lastButtonState1 = HIGH;
unsigned long lastDebounceTime1 = 0;
const int debounceDelay = 1;
int clickCount = 0;
int buttonState2 = HIGH;
int lastButtonState2 = HIGH;
unsigned long lastDebounceTime2 = 0;
int resetButtonState = HIGH;
int lastResetButtonState = HIGH;
unsigned long lastDebounceTimeReset = 0;
int reverseCountState = HIGH;
int lastReverseCountState = HIGH;
void setup()
{
Serial.begin(115200);
delay(5);
for (int i = 0; i < 4; i++)
{
pinMode(digitPins[i], OUTPUT);
}
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(secondIndicatorPin, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(resetButtonPin, INPUT);
pinMode(reverseCountPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.println("Press the buttons to increment or reset the counter.");
}
void updateDisp()
{
for (byte j = 0; j < 4; j++)
digitalWrite(digitPins[j], LOW);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, B11111111);
digitalWrite(latchPin, HIGH);
delayMicroseconds(5);
digitalWrite(digitPins[digitScan], HIGH);
digitalWrite(latchPin, LOW);
if (digitScan == 0)
shiftOut(dataPin, clockPin, MSBFIRST, ~(digit[digitBuffer[digitScan]] | B10000000));
else
shiftOut(dataPin, clockPin, MSBFIRST, ~digit[digitBuffer[digitScan]]);
digitalWrite(latchPin, HIGH);
digitScan++;
if (digitScan > 3)
digitScan = 0;
}
void handleButtonPress1()
{
clickCount += 100;
}
void handleButtonPress2()
{
if (clickCount < 9999)
{
clickCount++;
}
else
{
clickCount = 0;
}
}
void handleResetButtonPress()
{
clickCount = 0;
}
void loop()
{
int reading1 = digitalRead(buttonPin1);
if (reading1 != lastButtonState1)
{
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay)
{
if (reading1 != buttonState1)
{
buttonState1 = reading1;
if (buttonState1 == LOW)
{
handleButtonPress1();
}
}
}
lastButtonState1 = reading1;
int reading2 = digitalRead(buttonPin2);
if (reading2 != lastButtonState2)
{
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay)
{
if (reading2 != buttonState2)
{
buttonState2 = reading2;
if (buttonState2 == LOW)
{
if (reverseCountState == HIGH)
{
handleButtonPress2();
}
else
{
clickCount--;
if (clickCount < 0)
{
clickCount = 9999;
}
}
}
}
}
lastButtonState2 = reading2;
int resetButtonReading = digitalRead(resetButtonPin);
if (resetButtonReading != lastResetButtonState)
{
lastDebounceTimeReset = millis();
if (resetButtonReading == LOW)
{
handleResetButtonPress();
}
}
lastResetButtonState = resetButtonReading;
int reverseCountReading = digitalRead(reverseCountPin);
if (reverseCountReading != lastReverseCountState)
{
lastDebounceTime2 = millis();
if (reverseCountReading == LOW)
{
reverseCountState = LOW;
}
else
{
reverseCountState = HIGH;
}
}
lastReverseCountState = reverseCountReading;
digitBuffer[3] = clickCount / 1000;
digitBuffer[2] = (clickCount % 1000) / 100;
digitBuffer[1] = (clickCount % 100) / 10;
digitBuffer[0] = clickCount % 10;
if (clickCount % 5 != 0)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
updateDisp();
delay(1);
}