// Pin definitions for 7-segment display
const int segmentA = 2;
const int segmentB = 3;
const int segmentC = 4;
const int segmentD = 5;
const int segmentE = 6;
const int segmentF = 7;
const int segmentG = 8;
// Pin definitions for buttons
const int buttonIncrease = 9;
const int buttonDecrease = 10;
int currentNumber = 0;
void setup() {
// Set the segment pins as outputs
pinMode(segmentA, OUTPUT);
pinMode(segmentB, OUTPUT);
pinMode(segmentC, OUTPUT);
pinMode(segmentD, OUTPUT);
pinMode(segmentE, OUTPUT);
pinMode(segmentF, OUTPUT);
pinMode(segmentG, OUTPUT);
// Set the button pins as inputs with pull-up resistors
pinMode(buttonIncrease, INPUT_PULLUP);
pinMode(buttonDecrease, INPUT_PULLUP);
displayNumber(currentNumber); // Display initial number
}
void loop() {
// Check if the increase button is pressed
if(digitalRead(buttonIncrease) == LOW) {
currentNumber++; // Increase the number
if(currentNumber > 9) currentNumber = 0; // Wrap around if we exceed 9
displayNumber(currentNumber); // Display the new number
delay(200); // Debounce the button
}
// Check if the decrease button is pressed
if(digitalRead(buttonDecrease) == LOW) {
currentNumber--; // Decrease the number
if(currentNumber < 0) currentNumber = 9; // Wrap around if we go below 0
displayNumber(currentNumber); // Display the new number
delay(200); // Debounce the button
}
}
// Function to display a number on the 7-segment display
void displayNumber(int number) {
switch(number) {
case 0:
digitalWrite(segmentA, HIGH);
digitalWrite(segmentB, HIGH);
digitalWrite(segmentC, HIGH);
digitalWrite(segmentD, HIGH);
digitalWrite(segmentE, HIGH);
digitalWrite(segmentF, HIGH);
digitalWrite(segmentG, LOW);
break;
case 1:
// Define segment pins for number 1
// (you can fill in the rest of the cases for other numbers)
digitalWrite(segmentA, LOW);
digitalWrite(segmentB, HIGH);
digitalWrite(segmentC, HIGH);
digitalWrite(segmentD, LOW);
digitalWrite(segmentE, LOW);
digitalWrite(segmentF, LOW);
digitalWrite(segmentG, LOW);
break;
// Fill in cases for numbers 2-9
}
}
https://www.youtube.com/watch?v=MV57rbvLSO8