const int data = 6; // Data pin for the first shift register
const int stcp = 7; // Latch pin for the first shift register
const int shcp = 8; // Clock pin for the first shift register
const int ds = 21; // Data pin for the second shift register
const int stcpi = 23; // Latch pin for the second shift register
const int shcpi = 22; // Clock pin for the second shift register
int prevState = LOW; // Stores previous button state for polling
volatile bool buttonPressed = false; // Interrupt flag for ISR button
unsigned long lastDebounce = 0; // Debouncing timer
const unsigned long debounceDelay = 200; // Debouncing delay (200ms)
int x = 0;
int y = 0;
const int button = 17;
const int button_2 = 16;
void local() {
unsigned long currentTime = millis();
if (currentTime - lastDebounce > debounceDelay) {
lastDebounce = currentTime;
buttonPressed = true;
}
}
void setup() {
// Setup for Serial and pin modes
Serial.begin(115200);
pinMode(button, INPUT);
pinMode(button_2, INPUT);
pinMode(data, OUTPUT);
pinMode(stcp, OUTPUT);
pinMode(shcp, OUTPUT);
pinMode(ds, OUTPUT);
pinMode(stcpi, OUTPUT);
pinMode(shcpi, OUTPUT);
attachInterrupt(digitalPinToInterrupt(button), local, RISING);
}
void loop() {
if (buttonPressed == true) {
buttonPressed = false;
x++;
if (x > 6) {
x = 0;
}
incrementNum(x);
delay(10);
}
int state = digitalRead(button_2);
unsigned long currentTime = millis();
if (state == HIGH && prevState == LOW && (currentTime - lastDebounce > debounceDelay)) {
lastDebounce = currentTime; // Update debounce time
y++;
if (y > 6) {
y = 0;
}
increment(y);
delay(10);
}
prevState = state; // Update previous state for the next loop
}
void incrementNum(int num) {
digitalWrite(stcp, LOW);
if (num == 0) {
shiftOut(data, shcp, LSBFIRST, 0B11111100);
} else if (num == 1) {
shiftOut(data, shcp, LSBFIRST, 0B01100000);
} else if (num == 2) {
shiftOut(data, shcp, LSBFIRST, 0B11011010);
} else if (num == 3) {
shiftOut(data, shcp, LSBFIRST, 0B11110010);
} else if (num == 4) {
shiftOut(data, shcp, LSBFIRST, 0B01100110);
} else if (num == 5) {
shiftOut(data, shcp, LSBFIRST, 0B10110110);
} else if (num == 6) {
shiftOut(data, shcp, LSBFIRST, 0B10111110);
}
digitalWrite(stcp, HIGH); // Latch the data to display
}
// Function to update the second seven-segment display
void increment(int num) {
digitalWrite(stcpi, LOW);
if (num == 0) {
shiftOut(ds, shcpi, LSBFIRST, 0); // Clear display
} else if (num == 1) {
shiftOut(ds, shcpi, LSBFIRST, 0B11101110); // Display 'A'
} else if (num == 2) {
shiftOut(ds, shcpi, LSBFIRST, 0B00111110); // Display 'B'
} else if (num == 3) {
shiftOut(ds, shcpi, LSBFIRST, 0B10011100); // Display 'C'
} else if (num == 4) {
shiftOut(ds, shcpi, LSBFIRST, 0B01111010); // Display 'D'
} else if (num == 5) {
shiftOut(ds, shcpi, LSBFIRST, 0B10011110); // Display 'E'
} else if (num == 6) {
shiftOut(ds, shcpi, LSBFIRST, 0B10001110); // Display 'F'
}
digitalWrite(stcpi, HIGH); // Latch the data to display
}