// Define the GPIO pins for the segments of the 7-segment display
int a = 2; // Segment A
int b = 3; // Segment B
int c = 4; // Segment C
int d = 5; // Segment D
int e = 6; // Segment E
int f = 7; // Segment F
int g = 8; // Segment G
// Define button pin
int buttonPin = 9; // Push button
// Variables
int count = 0; // Attendance count
int buttonState = 0; // Current button state
int lastButtonState = 0; // Previous button state
// 7-segment number patterns
const byte numbers[10][7] = {
{1, 1, 1, 1, 1, 1, 0}, // 0
{0, 1, 1, 0, 0, 0, 0}, // 1
{1, 1, 0, 1, 1, 0, 1}, // 2
{1, 1, 1, 1, 0, 0, 1}, // 3
{0, 1, 1, 0, 0, 1, 1}, // 4
{1, 0, 1, 1, 0, 1, 1}, // 5
{1, 0, 1, 1, 1, 1, 1}, // 6
{1, 1, 1, 0, 0, 0, 0}, // 7
{1, 1, 1, 1, 1, 1, 1}, // 8
{1, 1, 1, 1, 0, 1, 1} // 9
};
// Function to display the number on the 7-segment display
void displayNumber(int num) {
digitalWrite(a, numbers[num][0]);
digitalWrite(b, numbers[num][1]);
digitalWrite(c, numbers[num][2]);
digitalWrite(d, numbers[num][3]);
digitalWrite(e, numbers[num][4]);
digitalWrite(f, numbers[num][5]);
digitalWrite(g, numbers[num][6]);
}
void setup() {
// Initialize segment pins
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
// Initialize button pin
pinMode(buttonPin, INPUT);
// Display the initial number (0)
displayNumber(count);
}
void loop() {
// Read button state
buttonState = digitalRead(buttonPin);
// Check if the button is pressed (and it's a new press)
if (buttonState == HIGH && lastButtonState == LOW) {
count++;
if (count > 9) count = 0; // Reset count to 0 after 9
displayNumber(count); // Update the display
}
// Save the current state as the last state
lastButtonState = buttonState;
// Small delay to avoid bouncing issues
delay(50);
}