#include <SPI.h>
// Define pins for 7-segment display
const int latchDisplay = 10; // Connects to the ST_CP pin of 74HC595 for display
const int clockDisplay = 13; // Connects to the SH_CP pin of 74HC595 for display
const int dataDisplay = 11; // Connects to the DS pin of 74HC595 for display
// Define pins for LEDs
const int latchLEDs = 10; // Connects to the ST_CP pin of 74HC595 for LEDs
const int clockLEDs = 7; // Connects to the SH_CP pin of 74HC595 for LEDs
const int dataLEDs = 11; // Connects to the DS pin of 74HC595 for LEDs
// Define pins for buttons
const int buttonUpPin = A0; // Up button connected to analog pin A0
const int buttonDownPin = A1; // Down button connected to analog pin A1
int count = 0; // Counter variable
void setup() {
Serial.begin(115200);
// Set pins as outputs
pinMode(latchDisplay, OUTPUT);
pinMode(clockDisplay, OUTPUT);
pinMode(dataDisplay, OUTPUT);
pinMode(latchLEDs, OUTPUT);
pinMode(clockLEDs, OUTPUT);
pinMode(dataLEDs, OUTPUT);
// Set button pins as inputs
pinMode(buttonUpPin, INPUT_PULLUP);
pinMode(buttonDownPin, INPUT_PULLUP);
// Initialize displays and LEDs
displayNumber(count);
updateLEDs(count);
Serial.println(count);
}
void loop() {
// Increment count if the up button is pressed
if (digitalRead(buttonUpPin) == LOW) {
delay(50); // Debouncing delay
if (digitalRead(buttonUpPin) == LOW) {
count++;
if (count > 32) {
count = 0; // Reset count after reaching 32
}
displayNumber(count);
updateLEDs(count);
Serial.println(count);
delay(250); // Delay for button press sensitivity
}
}
// Decrement count if the down button is pressed
if (digitalRead(buttonDownPin) == LOW) {
delay(50); // Debouncing delay
if (digitalRead(buttonDownPin) == LOW) {
count--;
if (count < 0) {
count = 32; // Wrap to 32 after reaching 0
}
displayNumber(count);
updateLEDs(count);
Serial.println(count);
delay(250); // Delay for button press sensitivity
}
}
}
void displayNumber(int number) {
const byte segments[] PROGMEM = {
B00111111, // 0
B00000110, // 1
B01011011, // 2
B01001111, // 3
B01100110, // 4
B01101101, // 5
B01111101, // 6
B00000111, // 7
B01111111, // 8
B01101111, // 9
B00000000 // Blank (all segments off)
};
byte units = number % 10;
byte tens = number / 10;
digitalWrite(latchDisplay, LOW);
shiftOut(dataDisplay, clockDisplay, MSBFIRST, segments[tens]);
shiftOut(dataDisplay, clockDisplay, MSBFIRST, segments[units]);
digitalWrite(latchDisplay, HIGH);
}
void updateLEDs(int number) {
byte ledValues = 0;
if (number > 0 && number <= 32) {
ledValues = (1 << (number - 1)); // Set the corresponding LED bit
}
digitalWrite(latchLEDs, LOW);
shiftOut(dataLEDs, clockLEDs, MSBFIRST, ledValues);
digitalWrite(latchLEDs, HIGH);
delayMicroseconds(10); // Small delay for stability
}