// Program to demonstrate the MD_Parola library
//
// Simple Clock Program using DS1307 library and Parola LED Matrix Library
//
// MD_MAX72XX library can be found at https://github.com/MajicDesigns/MD_MAX72XX
/*
* RTClib.h is not compatible with Parola or DS1307RTC - need to set the RTC with a separate program
* This version removes Timezone.h for ease of understanding the code
*/
//RTC Wiring for Nano = SCL/A5, SDA/A4
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include "Sample_Font.h"
//#include"Font_Data.h"
#include <DS1307RTC.h> // https://github.com/PaulStoffregen/DS1307RTC
#include <Timezone.h> // https://github.com/JChristensen/Timezone
// To set the time of the RTC to the local time when compiled, set setRTC to true. Once the RTC time is set, set it to false and reflash.
#define setRTC false
#if setRTC
#include "RTClib.h"
RTC_DS3231 rtc;
#endif
#include <Wire.h>
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
//#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
// Radar Pin - goes high on detect
#define RADAR_PIN 12
unsigned long currentTime = 0;
unsigned long previousTime = 0;
int interval = 30 * 1000; // 30 sec
int firstboot = true;
int wait = 120; // In milliseconds
bool radarDetected = false;
bool triggered = true;
unsigned long resetTrigger = 0;
// Hardware SPI connection
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// Arbitrary output pins
// MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
unsigned long targetTime = 0;
int ss_interval = 2;
int ss;
time_t t;
time_t tz;
int hour12;
//TimeChangeRule is set with reference to the time the RTC is set to.
//Note that Wokwi will set the RTC to current local time so the rule change will need to
//be adjusted from current DST or STD.
// US Eastern Time Zone (New York, Detroit) if RTC is set to UTC Standard Time
//TimeChangeRule myDST = {"EDT", Second, Sun, Mar, 2, -240}; //Daylight time = UTC - 4 hours
//TimeChangeRule mySTD = {"EST", First, Sun, Nov, 2, -300}; //Standard time = UTC - 5 hours
//Timezone myTZ(myDST, mySTD);
// US Pacific Time - for case RTC was set at PDT as baseline
// TimeChangeRule myDST = {"PDT", Second, Sun, Mar, 2, +0}; //Daylight time = Already set at DT
// TimeChangeRule mySTD = {"PST", First, Sun, Nov, 2, 60}; //Standard time = RTC + 60 minutes to "Fall Forward"
// Timezone myTZ(myDST, mySTD);
// US Central Time - for case RTC is set to PDT as baseline
// TimeChangeRule myDST = {"CDT", Second, Sun, Mar, 2, +120}; //Daylight time = RTC +120 for CST
// TimeChangeRule mySTD = {"CST", First, Sun, Nov, 2, +60}; //Standard time = RTC +60 for CDT with "Fall Forward"
// Timezone myTZ(myDST, mySTD);
// US Central Time - for case RTC is set to CST as baseline
TimeChangeRule myDST = {"CDT", Second, Sun, Mar, 2, -60}; //Daylight time = CST - 60 minutes (Spring Back)
TimeChangeRule mySTD = {"CST", First, Sun, Nov, 2, 0}; //Standard time = CST + 0 minutes (Fall Fwd)
Timezone myTZ(myDST, mySTD);
// If TimeChangeRules are already stored in EEPROM, comment out the three
// lines above and uncomment the line below.
//Timezone myTZ(100); //assumes rules stored at EEPROM address 100
TimeChangeRule *tcr; //pointer to the time change rule, use to get TZ abbrev
void setup(void) {
Serial.begin(115200);
pinMode(RADAR_PIN, INPUT);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
P.begin();
// Set the intensity (brightness) of the display (0-15)
P.setIntensity(2);
// Clear the display
P.displayClear();
// Set Font - commented out setFont for the default
//P.setFont(0, numeric7Seg);
P.setFont(0, SAMPLE_FONT);
//following line sets the RTC to the date & time this sketch was compiled. It only runs when setRTC is defineds as true.
#if setRTC
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //Uncomment to set RTC time. Comment and reflash after time is set.
Serial.println("RTC time set to compiler time");
#endif
char buf[32];
t = now();
time_t tz = myTZ.toLocal(t, &tcr);
hour12 = (hour(tz) + 11) % 12 + 1; // take care of noon and midnight-- converts 24h time to 12h time
// uncomment for scrolling
sprintf(buf, "%02d:%02d%s", hour12, minute(tz), hour(tz) >= 12 ? "pm" : "am");
Serial.println(buf);
}
void loop(void) {
char buf[32];
currentTime = millis();
if (currentTime - previousTime > interval || firstboot == true) {
previousTime = currentTime;
firstboot = false;
t = now();
tz = myTZ.toLocal(t, &tcr);
hour12 = (hour(tz) + 11) % 12 + 1; // take care of noon and midnight-- converts 24h time to 12h time
// uncomment for scrolling
sprintf(buf, "%02d:%02d%s", hour12, minute(tz), hour(tz) >= 12 ? "pm" : "am");
Serial.println(buf);
}
// Comment out if scrolling - flashing colon won't work with scrolling animation
//**************************************************************************************************
if (targetTime < millis()) {
targetTime = millis() + 500; //flash colon every 1/2 second
ss++; // Advance second
if (ss == 60) {
ss = 0;
}
}
if (ss % ss_interval) { // Flash colon when ss is an odd number (remainder is one)
sprintf(buf, "%02d:%02d%s", hour12, minute(tz), hour(tz) >= 12 ? "p" : "a");
} else {
sprintf(buf, "%02d %02d%s", hour12, minute(tz), hour(tz) >= 12 ? "p" : "a");
}
//**************************************************************************************************
// End comment out if scrolling
// Display time scrolling
// if (P.displayAnimate())
// P.displayScroll(buf, PA_CENTER, PA_SCROLL_LEFT, 100);
// Display time statically
radarDetected = digitalRead(RADAR_PIN);
if (radarDetected) {
triggered = true;
resetTrigger = millis();
Serial.print("Radar Pin: ");
Serial.println(radarDetected);
Serial.println("==========");
delay(wait);
}
if (triggered) {
P.setTextAlignment(PA_CENTER);
P.print(buf);
delay(wait);
}
if (millis() - resetTrigger > 10000) {
resetTrigger = millis();
triggered = false;
P.displayClear();
Serial.print("trigger: ");
Serial.println(triggered);
}
}