// Define segment pins (a, b, c, d, e, f, g)
const int segmentPins[] = {23, 22, 21, 18, 5, 4, 2};
// Define button pins
const int incButtonPin = 26;
const int decButtonPin = 27;
int value = 0; // Initialize the value to be displayed on the 7-segment display
void setup() {
// Set segment pins as OUTPUT
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
// Set button pins as INPUT_PULLUP
pinMode(incButtonPin, INPUT);
pinMode(decButtonPin, INPUT);
}
void loop() {
// Check the increment button
if (digitalRead(incButtonPin) == HIGH) {
value++;
delay(250); // Button debounce delay
}
// Check the decrement button
if (digitalRead(decButtonPin) == HIGH) {
value--;
delay(250); // Button debounce delay
}
// Ensure the value stays within a specified range (e.g., 0-9)
if (value < 0) {
value = 0;
}
if (value > 9) {
value = 9;
}
// Display the value on the 7-segment display
displayNumber(value);
}
void displayNumber(int num) {
// Define 7-segment patterns for numbers 0-9
byte patterns[] = {
B1000000, //0
B1111001, // 1
B0100100, // 2
B0110000, //3
B0011001, //4
B0010010, //5
B0000010, //6
B1111000, //7
B0000000, //8
B0010000};
// Display the number on the 7-segment display
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], bitRead(patterns[num], i));
}
}