#include <Arduino.h>
// This works kalo delaynya 500. Dibawah itu cacat. Harusnya normal kah di arduino beneran?
// Define pins for the shift register
#define DATA_PIN 11
#define CLOCK_PIN 12
#define LATCH_PIN 8
// Define pins for the digits
// #define DIGIT1_PIN 9
// #define DIGIT2_PIN 10
// #define DIGIT3_PIN 13
#define DIGIT1_PIN A5
#define DIGIT2_PIN A4
#define DIGIT3_PIN A3
int digit1 = 0;
// Segment values for common cathode 7-segment display
// byte segmentDigits[] = {
// 0b11000000, // 0
// 0b11111001, // 1
// 0b10100100, // 2
// 0b10110000, // 3
// 0b10011001, // 4
// 0b10010010, // 5
// 0b10000010, // 6
// 0b11111000, // 7
// 0b10000000, // 8
// 0b10010000 // 9
// };
int segmentDigits[] = {126,48,109,121,51,91,95,112,127,123};
// Display buffer
byte displayBuffer[3] = {0, 0, 0};
unsigned long previousMillis = 0; // Stores the last time the message was printed
const long interval = 1000; // Interval at which to print (milliseconds)
// Function to update the display
void updateDisplay() {
for (int i = 0; i < 3; i++) {
// Activate the current digit
digitalWrite(DIGIT1_PIN, i == 0 ? LOW : HIGH);
digitalWrite(DIGIT2_PIN, i == 1 ? LOW : HIGH);
digitalWrite(DIGIT3_PIN, i == 2 ? LOW : HIGH);
// Send the segment data
// digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, segmentDigits[displayBuffer[i]]);
digitalWrite(LATCH_PIN, LOW);
digitalWrite(LATCH_PIN, HIGH);
// Force 0 dulu
shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, 0);
digitalWrite(LATCH_PIN, LOW);
digitalWrite(LATCH_PIN, HIGH);
// Small delay to ensure the digit is visible
delay(10);
}
}
void setup() {
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
pinMode(DIGIT1_PIN, OUTPUT);
pinMode(DIGIT2_PIN, OUTPUT);
pinMode(DIGIT3_PIN, OUTPUT);
// Initialize all digit pins to HIGH (off)
digitalWrite(DIGIT1_PIN, HIGH);
digitalWrite(DIGIT2_PIN, HIGH);
digitalWrite(DIGIT3_PIN, HIGH);
}
void loop() {
// Update the display buffer with the digits you want to show
// displayBuffer[0] = 1; // First digit
displayBuffer[1] = 7; // Second digit
displayBuffer[2] = 7; // Third digit
// Update the display
updateDisplay();
unsigned long currentMillis = millis(); // Get the current time
if (currentMillis - previousMillis >= interval) {
// Save the last time the message was printed
previousMillis = currentMillis;
// Print the message to the Serial Monitor
Serial.print("Hello, this message prints every 2 seconds!");
digit1++;
displayBuffer[0] = digit1;
if(digit1 >9){
digit1=0;
}
}
}