#include <DS3232RTC.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
// RTC
#define CLK 2
#define DIO 3
#define GMT 9
//32x8LED matrix
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
// HARDWARE SPI
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
const uint16_t WAIT_TIME = 2000;
unsigned long lastTime = 0;
int displayState;
String pTime, pDate;
void setup()
{
P.begin();
P.setIntensity(0);
P.setTextAlignment(PA_CENTER);
Serial.begin(9600);
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");
}
void loop()
{
int sensorValue = analogRead(A2);
sensorValue = map(sensorValue, 0, 1023, 0, 15);
P.setIntensity(sensorValue);
if ((millis() - lastTime) > WAIT_TIME) {
pTime=String(hour())+":"+printDigits(minute());
pDate=String(month())+"-"+String(day());
displayState = !displayState;
lastTime = millis();
}
displayState == 0 ? P.print(pTime) : P.print(pDate);
delay(100);
if (Serial.available()) {
time_t t = processSyncMessage();
if (t != 0) {
RTC.set(t); // set the RTC and the system time to the received value
setTime(t);
Serial.println(t);
}
}
}
String printDigits(int digits)
{
return digits < 10 ? "0"+ String(digits) : String(digits);
}
/* code to process time sync messages from the serial port */
#define TIME_HEADER "T" // Header tag for serial time sync message
unsigned long processSyncMessage() {
unsigned long pctime = 0L;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if (Serial.find(TIME_HEADER)) {
pctime = Serial.parseInt();
return pctime + GMT * 3600;
if ( pctime < DEFAULT_TIME) { // check the value is a valid time (greater than Jan 1 2013)
pctime = 0L; // return 0 to indicate that the time is not valid
}
}
return pctime;
}