// SEVEN SEGMENT
const int latchDisplay = 2; // Connects to the ST_CP pin of 74HC595 for display
const int clockDisplay = 18; // Connects to the SH_CP pin of 74HC595 for display
const int dataDisplay = 13; // Connects to the DS pin of 74HC595 for display
// Push Buttons
const int buttonUpPin = 32; // Pin for counter up button
const int buttonDownPin = 33; // Pin for counter down button
int count = 0; // Counter variable, initial value set to 0
void setup() {
// SEVENSEGMENT
pinMode(latchDisplay, OUTPUT);
pinMode(clockDisplay, OUTPUT);
pinMode(dataDisplay, OUTPUT);
// Push Buttons with pulldown resistors
pinMode(buttonUpPin, INPUT);
pinMode(buttonDownPin, INPUT);
}
void loop() {
// Read the state of the push buttons
bool buttonUpState = digitalRead(buttonUpPin);
bool buttonDownState = digitalRead(buttonDownPin);
// Print button states for debugging
Serial.print("Button Up State: ");
Serial.println(buttonUpState);
Serial.print("Button Down State: ");
Serial.println(buttonDownState);
// Counter up logic
if (buttonUpState) {
delay(50); // Debounce delay
if (digitalRead(buttonUpPin)) {
count += buttonUpState; // Increase count by 1 when button is pressed
if (count > 99) {
count = 0;
}
}
}
// Counter down logic
if (buttonDownState) {
delay(50); // Debounce delay
if (digitalRead(buttonDownPin)) {
count -= buttonDownState; // Decrease count by 1 when button is pressed
if (count < 0) {
count = 99;
}
}
}
// Display the number on the seven-segment display
displayNumber(count);
delay(100); // Adjust delay as needed
}
void displayNumber(int number) {
const byte segments[] PROGMEM = {
B01000000, //0
B01111001, //1
B00100100, //2
B00110000, //3
B00011001, //4
B00010010, //5
B00000010, //6
B01111000, //7
B00000000, //8
B00010000 //9
};
byte timAsatuan = number% 10; // Updated calculation for timAsatuan
byte timApuluhan = number / 10; // Updated calculation for timApuluhan
digitalWrite(latchDisplay, LOW);
shiftOut(dataDisplay, clockDisplay, MSBFIRST, segments[timApuluhan]);
shiftOut(dataDisplay, clockDisplay, MSBFIRST, segments[timAsatuan]);
digitalWrite(latchDisplay, HIGH);
}