// Define the segment pins connected to Arduino
const int segA = 0;
const int segB = 2;
const int segC = 4;
const int segD = 5;
const int segE = 16;
const int segF = 17;
const int segG = 18;
// Define the push button pins
const int buttonUp = 12;
const int buttonDown = 13;
int counter = 0; // Start at 0
int lastButtonUpState = LOW;
int lastButtonDownState = LOW;
// Array to hold the binary values for each segment for digits 0-9
const int digits[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
};
void setup() {
// Set all segment pins as output
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
// Set button pins as input
pinMode(buttonUp, INPUT);
pinMode(buttonDown, INPUT);
}
void loop() {
int buttonUpState = digitalRead(buttonUp);
int buttonDownState = digitalRead(buttonDown);
// Check if Up button is pressed
if (buttonUpState == HIGH && lastButtonUpState == LOW) {
counter++;
if (counter > 9) counter = 0; // Wrap around to 0 if the count exceeds 9
displayDigit(counter);
delay(200); // Debounce delay
}
lastButtonUpState = buttonUpState;
// Check if Down button is pressed
if (buttonDownState == HIGH && lastButtonDownState == LOW) {
counter--;
if (counter < 0) counter = 9; // Wrap around to 9 if the count goes below 0
displayDigit(counter);
delay(200); // Debounce delay
}
lastButtonDownState = buttonDownState;
}
void displayDigit(int digit) {
// Set the segments according to the digit value
digitalWrite(segA, digits[digit][0]);
digitalWrite(segB, digits[digit][1]);
digitalWrite(segC, digits[digit][2]);
digitalWrite(segD, digits[digit][3]);
digitalWrite(segE, digits[digit][4]);
digitalWrite(segF, digits[digit][5]);
digitalWrite(segG, digits[digit][6]);
}