const int bcdPins[4][4] = {
{22, 24, 26, 28}, // BCD IC A
{30, 32, 34, 36}, // BCD IC B
{38, 40, 42, 44}, // BCD IC C
{46, 48, 50, 52} // BCD IC D
};
const int start_stop = 10;
const int increment = 11;
const int decrement = 12;
int counterValue = 0; // Stores counter
boolean isCounting = false;
void setup() {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
pinMode(bcdPins[i][j], OUTPUT);
}
}
pinMode(start_stop, INPUT);
pinMode(increment, INPUT);
pinMode(decrement, INPUT);
displayCounter();
}
void loop() {
int startStopState = digitalRead(start_stop);
int incrementState = digitalRead(increment);
int decrementState = digitalRead(decrement);
if (startStopState == HIGH) {
isCounting = !isCounting;
delay(250); // Debouncing delay
}
if (isCounting || incrementState == HIGH || decrementState == HIGH) {
counterValue = (counterValue + (isCounting ? 1 : (incrementState == HIGH ? 1 : -1)) + 10000) % 10000;
displayCounter();
delay(250); // Debouncing delay
}
delay(100);
}
void displayDigit(int digit, int bcdPins[4]) {
for (int i = 0; i < 4; ++i) {
digitalWrite(bcdPins[i], (digit >> i) & 0x01);
}
}
void displayCounter() {
int ones = counterValue % 10;
int tens = (counterValue / 10) % 10;
int hundreds = (counterValue / 100) % 10;
int thousands = counterValue / 1000;
displayDigit(ones, bcdPins[0]);
displayDigit(tens, bcdPins[1]);
displayDigit(hundreds, bcdPins[2]);
displayDigit(thousands, bcdPins[3]);
}