#include <LedControl.h>
#include <TimeLib.h>
// Pin connections to the MAX7219
#define DIN_PIN 13
#define CS_PIN 11
#define CLK_PIN 10
// Create a LedControl object
LedControl lc = LedControl(DIN_PIN, CLK_PIN, CS_PIN, 1);
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
Serial.println("Starting up...");
// Initialize the MAX7219
lc.shutdown(0, false); // Wake up displays
lc.setIntensity(0, 8); // Set brightness level (0 is min, 15 is max)
lc.clearDisplay(0); // Clear display register
// Test the LED matrix by lighting up all LEDs briefly
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
lc.setLed(0, row, col, true);
}
}
delay(1000);
lc.clearDisplay(0);
// Set the initial time (e.g., 12:00:00)
setTime(12, 0, 0, 1, 1, 2023);
Serial.println("Setup complete.");
}
void loop() {
lc.clearDisplay(0);
// Get the current time
int s = second();
int m = minute();
int h = hour() % 12;
// Calculate the angles for the hands
float s_angle = s * 6; // 360 degrees / 60 seconds
float m_angle = m * 6 + s_angle / 60; // 360 degrees / 60 minutes
float h_angle = h * 30 + m_angle / 12; // 360 degrees / 12 hours
// Draw the clock
drawClockFace();
drawHand(h_angle, 5); // hour hand
drawHand(m_angle, 7); // minute hand
drawHand(s_angle, 9); // second hand
delay(1000); // Update every second
}
void drawClockFace() {
for (int i = 0; i < 12; i++) {
float angle = i * 30; // 30 degrees for each hour mark
int x = 7 + (cos(degToRad(angle)) * 6);
int y = 7 + (sin(degToRad(angle)) * 6);
lc.setLed(0, y, x, true);
}
}
void drawHand(float angle, int length) {
for (int i = 1; i <= length; i++) {
int x = 7 + (cos(degToRad(angle)) * i);
int y = 7 + (sin(degToRad(angle)) * i);
lc.setLed(0, y, x, true);
}
}
float degToRad(float degrees) {
return degrees * 3.14159265358979 / 180;
}