#include <Arduino.h>
// Define the connections pins
#define CLK 18 // CLK pin
#define DIO 19 // DIO pin
void startTransmission();
void endTransmission();
void writeByte(uint8_t data);
void displayNumber(int num);
void displayCountdown(int minutes, int seconds);
void setBrightness(uint8_t brightness);
const uint8_t digitToSegment[] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111 // 9
};
void setup() {
pinMode(CLK, OUTPUT);
pinMode(DIO, OUTPUT);
setBrightness(0x0f); // Set brightness to maximum
}
void loop() {
int countdown = 60; // 60 seconds countdown
while (countdown >= 0) {
int seconds = countdown % 60;
int minutes = (countdown / 60) % 60;
// Display the countdown in mm:ss format
displayCountdown(minutes, seconds);
delay(500); // Wait for 1 second
countdown--; // Decrease the countdown by 1
}
// After the countdown is over, display "0000"
displayNumber(0);
// Optional: Reset the countdown or perform another action
// Uncomment the next line to loop the countdown indefinitely
// countdown = 60;
}
void startTransmission() {
digitalWrite(CLK, LOW);
digitalWrite(DIO, LOW);
}
void endTransmission() {
digitalWrite(CLK, LOW);
digitalWrite(DIO, LOW);
digitalWrite(CLK, HIGH);
digitalWrite(DIO, HIGH);
}
void writeByte(uint8_t data) {
for (int i = 0; i < 8; i++) {
digitalWrite(CLK, LOW);
digitalWrite(DIO, (data & 0x01) ? HIGH : LOW);
data >>= 1;
digitalWrite(CLK, HIGH);
}
// Acknowledge bit
digitalWrite(CLK, LOW);
pinMode(DIO, INPUT);
digitalWrite(CLK, HIGH);
pinMode(DIO, OUTPUT);
}
void displayNumber(int num) {
startTransmission();
writeByte(0x40); // Command: Set data
endTransmission();
startTransmission();
writeByte(0xC0); // Command: Set address
for (int i = 0; i < 4; i++) {
int digit = (num / (int)pow(10, 3 - i)) % 10;
writeByte(digitToSegment[digit]);
}
endTransmission();
}
void displayCountdown(int minutes, int seconds) {
startTransmission();
writeByte(0x40); // Command: Set data
endTransmission();
startTransmission();
writeByte(0xC0); // Command: Set address
writeByte(digitToSegment[minutes / 10]);
writeByte(digitToSegment[minutes % 10] | 0b10000000); // Add colon
writeByte(digitToSegment[seconds / 10]);
writeByte(digitToSegment[seconds % 10]);
endTransmission();
}
void setBrightness(uint8_t brightness) {
startTransmission();
writeByte(0x88 | (brightness & 0x07)); // Command: Display control, set brightness level
endTransmission();
}