#include <Wire.h>
#include "RTClib.h"
#include <LedControl.h>
RTC_DS1307 rtc;
LedControl lc(PB5, PB6, PB7, 1);
// Pins
#define BTN1 PA0
#define BTN2 PA1
#define LED1 PA2
#define LED2 PA3
int currentSymbol = 0; // 0 = none, 1 = smile, 2 = heart
byte smile[8] = {
B00111100,
B01000010,
B10100101,
B10000001,
B10100101,
B10011001,
B01000010,
B00111100
};
byte heart[8] = {
B00000000,
B01100110,
B11111111,
B11111111,
B11111111,
B01111110,
B00111100,
B00011000
};
void setup() {
pinMode(BTN1, INPUT_PULLUP);
pinMode(BTN2, INPUT_PULLUP);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
Wire.begin();
if (!rtc.begin()) {
while (1); // halt if RTC not found
}
// Uncomment to set the RTC time
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
void loop() {
DateTime now = rtc.now();
// Check schedule only if no manual symbol active
if (currentSymbol == 0) {
if (now.hour() == 10 && now.minute() < 30) {
activateSymbol(1);
} else if (now.hour() == 15 && now.minute() < 30) {
activateSymbol(2);
}
}
// Button 1 pressed
if (digitalRead(BTN1) == LOW) {
if (currentSymbol == 1) {
clearSymbol();
} else {
activateSymbol(1);
}
delay(200); // debounce
}
// Button 2 pressed
if (digitalRead(BTN2) == LOW) {
if (currentSymbol == 2) {
clearSymbol();
} else {
activateSymbol(2);
}
delay(200); // debounce
}
}
void activateSymbol(int symbol) {
lc.clearDisplay(0);
if (symbol == 1) {
showSymbol(smile);
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
} else if (symbol == 2) {
showSymbol(heart);
digitalWrite(LED2, HIGH);
digitalWrite(LED1, LOW);
}
currentSymbol = symbol;
}
void clearSymbol() {
lc.clearDisplay(0);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
currentSymbol = 0;
}
void showSymbol(byte pattern[8]) {
for (int i = 0; i < 8; i++) {
lc.setRow(0, i, pattern[i]);
}
}
// #include <Wire.h>
// #include "RTClib.h"
// #include <LedControl.h>
// RTC_DS1307 rtc;
// // 4 devices chained: DataIn=PB5, CLK=PB6, LOAD=PB7
// LedControl lc(PB5, PB6, PB7, 4);
// // Pins
// #define BTN1 PA0
// #define BTN2 PA1
// #define LED1 PA2
// #define LED2 PA3
// bool leftActive = false;
// bool rightActive = false;
// unsigned long lastLeftToggle = 0;
// unsigned long lastRightToggle = 0;
// byte smile[8] = {
// B00111100,
// B01000010,
// B10100101,
// B10000001,
// B10100101,
// B10011001,
// B01000010,
// B00111100
// };
// byte heart[8] = {
// B00000000,
// B01100110,
// B11111111,
// B11111111,
// B11111111,
// B01111110,
// B00111100,
// B00011000
// };
// void setup() {
// pinMode(BTN1, INPUT_PULLUP);
// pinMode(BTN2, INPUT_PULLUP);
// pinMode(LED1, OUTPUT);
// pinMode(LED2, OUTPUT);
// for (int i = 0; i < 4; i++) {
// lc.shutdown(i, false);
// lc.setIntensity(i, 8);
// lc.clearDisplay(i);
// }
// Wire.begin();
// rtc.begin();
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Set RTC if needed
// // Initially display both
// showLeft(smile);
// leftActive = true;
// showRight(heart);
// rightActive = true;
// lastLeftToggle = millis();
// lastRightToggle = millis();
// }
// void loop() {
// unsigned long now = millis();
// // -------- Automatic toggle --------
// if (now - lastLeftToggle >= 30000) { // every 30 seconds
// if (leftActive) clearLeft();
// else showLeft(smile);
// leftActive = !leftActive;
// lastLeftToggle = now;
// }
// if (now - lastRightToggle >= 15000) { // every 15 seconds
// if (rightActive) clearRight();
// else showRight(heart);
// rightActive = !rightActive;
// lastRightToggle = now;
// }
// // -------- Manual control --------
// if (digitalRead(BTN1) == LOW) {
// if (leftActive) clearLeft();
// else showLeft(smile);
// leftActive = !leftActive;
// lastLeftToggle = now; // reset timer after manual change
// delay(200);
// }
// if (digitalRead(BTN2) == LOW) {
// if (rightActive) clearRight();
// else showRight(heart);
// rightActive = !rightActive;
// lastRightToggle = now; // reset timer after manual change
// delay(200);
// }
// }
// // -------- Display functions --------
// void showLeft(byte pattern[8]) {
// for (int row = 0; row < 8; row++) {
// lc.setRow(1, row, pattern[row]);
// // lc.setRow(0, row, pattern[row]);
// }
// digitalWrite(LED1, HIGH);
// }
// void showRight(byte pattern[8]) {
// for (int row = 0; row < 8; row++) {
// // lc.setRow(2, row, pattern[row]);
// lc.setRow(3, row, pattern[row]);
// }
// digitalWrite(LED2, HIGH);
// }
// void clearLeft() {
// // lc.clearDisplay(0);
// lc.clearDisplay(1);
// digitalWrite(LED1, LOW);
// }
// void clearRight() {
// // lc.clearDisplay(2);
// lc.clearDisplay(3);
// digitalWrite(LED2, LOW);
// }
Loading
st-nucleo-c031c6
st-nucleo-c031c6