// Define the segment pins connected to Arduino
#include <Arduino.h>
// Define pin connections for the seven-segment display
int segmentPins[] = {2, 0, 4, 16, 17, 5, 18};
// Define the push button pins
const int buttonUp = 21;
const int buttonDown = 22;
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 clearDisplay() {
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], LOW);
}
}
void displayDigit(int digit) {
clearDisplay();
// Set the segments according to the digit value
for(int i = 0; i< 7; i++){
digitalWrite(segmentPins[i], digits[digit][i]);
}
}
void setup() {
// Set all segment pins as output
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], 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(1000); // 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(1000); // Debounce delay
}
lastButtonDownState = buttonDownState;
}