#include "RTClib.h"
#include <DHT.h>
#include <SPI.h>
#include <Wire.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include "Font7Seg.h"
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4 // Define the number of displays connected
#define CLK_PIN 13 // CLK or SCK
#define DATA_PIN 11 // DATA or MOSI
#define CS_PIN 10 // CS or SS
#define SPEED_TIME 10 // Speed of the transition
#define PAUSE_TIME 100
#define MAX_MESG 20
// These are for the clock
#define DS1307_ADDRESS 0x68
// Global variables
uint8_t hours, minutes, seconds;
char szTime[9]; // mm:ss\0
char szMesg[MAX_MESG + 1] = "";
uint8_t clear = 0x00;
// Hardware SPI connection
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
RTC_DS1307 rtc; //create an instant of RTC
DateTime previous;
DateTime current;
void beginDS1307()
{
// Read the values (date and time) of the DS1307 module
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(clear);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 0x07);
seconds = bcdToDec(Wire.read());
minutes = bcdToDec(Wire.read());
hours = bcdToDec(Wire.read() & 0xff);
}
uint8_t decToBcd(uint8_t value)
{
return ((value / 10 * 16) + (value % 10));
}
uint8_t bcdToDec(uint8_t value)
{
return ((value / 16 * 10) + (value % 16));
}
// Code for reading clock time
void getTime(char *psz, bool f = true)
{
sprintf(psz, "%02d%c%02d", hours, (f ? ':' : ' '), minutes);
}
void setup () {
Wire.begin();
P.begin(2);
P.setInvert(false);
P.setZone(0, MAX_DEVICES - 4, MAX_DEVICES - 1);
P.setZone(1, MAX_DEVICES - 4, MAX_DEVICES - 1);
P.displayZoneText(1, szTime, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
P.displayZoneText(0, szMesg, PA_CENTER, SPEED_TIME, 0, PA_PRINT , PA_NO_EFFECT);
Serial.begin(115200);
//check rtc connection
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
previous = rtc.now();
}
void loop () {
static bool flasher = false;
current = rtc.now(); //get the current time
Serial.print("Current time: ");
display(current);
previous = rtc.now();
delay(1000);
getTime(szMesg, flasher);
P.print(szMesg);
}
void display(DateTime now){
Serial.print(now.hour(), DEC);//get current hour
Serial.print(':');
Serial.print(now.minute(), DEC); //get current minute
Serial.print(':');
Serial.print(now.second(), DEC); //get current second
Serial.println();
beginDS1307();
hours = (now.hour(), DEC);
minutes = (now.minute(), DEC);
seconds = (now.second(), DEC);
/*
P.displayAnimate();
if (P.getZoneStatus(0))
{
switch (display)
{
case 0: // Clock
P.setFont(0, numeric7Seg);
P.setTextEffect(0, PA_PRINT, PA_NO_EFFECT);
P.setPause(0, 0);
if ((millis() - lastTime) >= 1000)
{
lastTime = millis();
getTime(szMesg, flasher);
flasher = !flasher;
}
if ((seconds == 00) && (seconds <= 30)) {
display++;
}
break;
}
P.displayReset(0); // Rest zone zero
}
*/
}