#include <Wire.h>
#include <RTClib.h>
#include <MD_MAX72XX.h>
#include <SPI.h>
// * Cấu hình RTC DS1307 *
RTC_DS1307 rtc;
// * Cấu hình LED Matrix MAX7219 *
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW // Loại module LED
#define MAX_DEVICES 5 // Số lượng module LED (5 ma trận 8x8)
#define CLK_PIN 13 // Chân đồng hồ
#define DATA_PIN 11 // Chân dữ liệu
#define CS_PIN 10 // Chân chọn chip
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// Hàm hiển thị số cuộn từ trên xuống dưới
void scrollVertical(int oldNum, int newNum, int delayTime) {
mx.clear(); // Xóa dữ liệu cũ trên LED
uint8_t oldBitmap[8];
uint8_t newBitmap[8];
mx.getChar(oldNum, sizeof(oldBitmap), oldBitmap); // Bitmap của số cũ
mx.getChar(newNum, sizeof(newBitmap), newBitmap); // Bitmap của số mới
// Hiệu ứng cuộn dọc từ trên xuống dưới
for (int shift = 0; shift <= 8; shift++) {
mx.clear(); // Xóa màn hình
// Vẽ phần trên của số cũ
for (int i = 0; i < 8 - shift; i++) {
mx.setRow(0, i, oldBitmap[i + shift]);
}
// Vẽ phần dưới của số mới
for (int i = 0; i < shift; i++) {
mx.setRow(0, 8 - shift + i, newBitmap[i]);
}
delay(delayTime); // Điều chỉnh tốc độ cuộn
}
}
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Không tìm thấy module RTC!");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("Đặt thời gian mặc định...");
rtc.adjust(DateTime(F(_DATE_), F(_TIME_))); // Lấy thời gian từ máy tính
}
// Khởi động LED Matrix
mx.begin();
mx.control(MD_MAX72XX::INTENSITY, 8); // Độ sáng (0-15)
mx.clear();
}
void loop() {
DateTime now = rtc.now();
static int lastHour = -1;
static int lastMinute = -1;
if (now.hour() != lastHour) {
scrollVertical(lastHour, now.hour(), 50); // Hiệu ứng trượt từ trên xuống
lastHour = now.hour();
}
if (now.minute() != lastMinute) {
scrollVertical(lastMinute, now.minute(), 50); // Hiệu ứng trượt từ trên xuống
lastMinute = now.minute();
}
delay(1000);
}