#include <LedControl.h>
#include <Wire.h>
#include <RTClib.h>
// MAX7219 pins (DIN, CLK, CS)
#define DIN_PIN PB5
#define CLK_PIN PB3
#define CS_PIN PB4
// Pushbutton and LED pins
#define BUTTON_PIN PA0
#define LED_PIN PB1
LedControl lc = LedControl(DIN_PIN, CLK_PIN, CS_PIN, 1); // One MAX7219 device
RTC_DS1307 rtc;
bool displayOn = true;
bool lastButtonState = HIGH;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
Wire.begin();
// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running, setting time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set time to compile time
}
// Initialize MAX7219
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
}
void displayPattern() {
// Example pattern: diagonal line
for (int i = 0; i < 8; i++) {
lc.setRow(0, i, 1 << i);
}
}
void clearDisplay() {
lc.clearDisplay(0);
}
void loop() {
// Read pushbutton, simple toggle logic
bool buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW && lastButtonState == HIGH) {
displayOn = !displayOn; // Toggle display on/off
delay(200); // Debounce delay
}
lastButtonState = buttonState;
DateTime now = rtc.now();
// Example: Only show pattern between 8AM and 8PM
if (displayOn && now.hour() >= 8 && now.hour() < 20) {
displayPattern();
digitalWrite(LED_PIN, HIGH); // LED ON while display active
} else {
clearDisplay();
digitalWrite(LED_PIN, LOW);
}
// Optional: print current time to Serial Monitor
Serial.print("Time: ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.println(now.second());
delay(500);
}