/******************************************************************************
USBasp Connection to ATmega328P-PU:
* USBasp GND to ground
* USBasp MOSI to PB3 or MOSI or ATmega328P-PU pin 17
* USBasp +5V to +5V supply
* USBasp SS to PC6 or /RESET or ATmega328P-PU pin 1
* USBasp SCK to PB5 or SCK or ATmega328P-PU pin 19
* USBasp MISO to PB4 or MISO or ATmega328P-PU pin 18
1MHz Output[*] // e-Gizmo USBasp Pinout
GND[*][*]SS
MOSI[*][*]SCK
+5V[*][*]MISO
******************************************************************************/
/******************************************************************************
CH340 USB to Serial Connection to ATmega328P-PU:
* CH340 GND to ground
* CH340 TXD to RXD or PD0 or ATmega328P-PU pin 2
* CH340 RXD to TXD or PD1 or ATmega328P-PU pin 3
* CH340 +5V to +5V supply
******************************************************************************/
/*******************************************************************************************************
DS3231 I2C Real-Time Clock Library
----------------------------------
DS3231.cpp - Arduino/chipKit library support for the DS3231 I2C Real-Time Clock
Copyright (C)2015 Rinky-Dink Electronics, Henning Karlsen. All right reserved
This library has been made to easily interface and use the DS3231 RTC with
an Arduino or chipKit.
You can find the latest version of the library at
http://www.RinkyDinkElectronics.com/
This library is free software; you can redistribute it and/or
modify it under the terms of the CC BY-NC-SA 3.0 license.
Please see the included documents for further information.
Commercial use of this library requires you to buy a license that
will allow commercial use. This includes using the library,
modified or not, as a tool to sell products.
The license applies to all part of the library including the
examples and tools supplied with the library.
*******************************************************************************************************/
/*******************************************************************************************************
MAX7219/7221 LED Matrix Controller Library
------------------------------------------
MD_MAX72xx - Library for using a MAX7219/7221 LED matrix controller
See header file for comments
This file contains class and hardware related methods.
Copyright (C) 2012-14 Marco Colli. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*******************************************************************************************************/
/*******************************************************************************************************
MD_Parola - Library for modular scrolling text and Effects
----------------------------------------------------------
See header file for comments
Copyright (C) 2013 Marco Colli. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*******************************************************************************************************/
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <DS3231.h>
#include "JB_Font_Data.h" // Place the header file in this directory Arduino\libraries\MD_MAX72XX\src
// Define the number of devices we have in the chain and the hardware interface
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 8
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
// HARDWARE SPI
MD_Parola digitalClock = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// Init the DS3231 using the hardware interface
DS3231 rtc(SDA, SCL);
// Init a Time-data structure
Time t;
int setButton = 2;
int downButton = 3;
int upButton = 5;
int setSelectButton = 6;
int resetPin = 4;
int DOW = 7;
int dd = 1;
int d = 0;
int mm = 1;
int m = 0;
int yyyy = 2021;
int yy = 0;
int hr = 0;
int h = 0;
int mins = 0;
int mi = 0;
int secs = 0;
int sc = 0;
int timeHr_1stDigit;
int timeHr_2ndDigit;
int timeMin_1stDigit;
int timeMin_2ndDigit;
int timeSec_1stDigit;
int timeSec_2ndDigit;
String meridiem;
void setup()
{
//Serial.begin(9600);
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, LOW);
digitalClock.begin(2);
digitalClock.setZone(0, 0, 3);
digitalClock.setZone(1, 4, 7);
digitalClock.setFont(0, JBSmallFont);
digitalClock.setFont(1, JBSmallFont);
// Set the intensity (brightness) of the display (0-15)
digitalClock.setIntensity(1);
// Initialize the rtc object
rtc.begin();
pinMode(setButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(upButton, INPUT_PULLUP);
pinMode(setSelectButton, INPUT_PULLUP);
}
void loop()
{
// Get data from the DS3231
t = rtc.getTime(); // Read current time and date.
Convert_to_12Hour_Clock_Format();
String timeDisplay = String (timeHr_1stDigit) + String (timeHr_2ndDigit) + ":" + String (timeMin_1stDigit) + String (timeMin_2ndDigit) + ":" + String (timeSec_1stDigit) + String (timeSec_2ndDigit) + String (meridiem);
char char_timeDisplay[timeDisplay.length()+1];
timeDisplay.toCharArray(char_timeDisplay,timeDisplay.length()+1);
String dateDisplay = String (rtc.getDOWStr()) + " " + "-" + " " + String (rtc.getMonthStr()) + " " + String (t.date, DEC) + ", " + String (t.year, DEC);
char char_dateDisplay[dateDisplay.length()+1];
dateDisplay.toCharArray(char_dateDisplay,dateDisplay.length()+1);
digitalClock.displayZoneText(0, char_timeDisplay, PA_CENTER, 100, 0, PA_NO_EFFECT, PA_NO_EFFECT);
if (digitalClock.displayAnimate())
{
digitalClock.displayZoneText(1, char_dateDisplay, PA_LEFT, 120, 100, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
for (uint8_t i=0; i<2; i++)
{
if (digitalClock.getZoneStatus(i))
{
// do something with the parameters for the animation then reset it
digitalClock.displayReset(i);
}
}
}
int setButtonState = digitalRead(2);
if (setButtonState == LOW)
{
set_select();
}
resetClock();
}
void Convert_to_12Hour_Clock_Format()
{
String timeHr = String (t.hour, DEC);
int int_timeHr = timeHr.toInt();
//Serial.println(timeHr);
String timeMin = String (t.min, DEC);
int int_timeMin = timeMin.toInt();
String timeSec = String (t.sec, DEC);
int int_timeSec = timeSec.toInt();
if (int_timeHr == 0)
{
timeHr_1stDigit = 1;
timeHr_2ndDigit = 2;
meridiem = "<";
}
else if ((int_timeHr > 0) && (int_timeHr < 10))
{
timeHr_1stDigit = 0;
timeHr_2ndDigit = int_timeHr%10;
meridiem = "<";
}
else if ((int_timeHr > 9) && (int_timeHr < 12))
{
timeHr_1stDigit = int_timeHr/10;
timeHr_2ndDigit = int_timeHr%10;
meridiem = "<";
}
else if (int_timeHr == 12)
{
timeHr_1stDigit = int_timeHr/10;
timeHr_2ndDigit = int_timeHr%10;
meridiem = ">";
}
else if ((int_timeHr > 12) && (int_timeHr < 22))
{
timeHr_1stDigit = 0;
timeHr_2ndDigit = int_timeHr - 12;
meridiem = ">";
}
else
{
timeHr_1stDigit = int_timeHr/20;
timeHr_2ndDigit = (int_timeHr - 12) - 10;
meridiem = ">";
}
if (int_timeMin < 10)
{
timeMin_1stDigit = 0;
timeMin_2ndDigit = int_timeMin%10;
}
else
{
timeMin_1stDigit = int_timeMin/10;
timeMin_2ndDigit = int_timeMin%10;
}
if (int_timeSec < 10)
{
timeSec_1stDigit = 0;
timeSec_2ndDigit = int_timeSec%10;
}
else
{
timeSec_1stDigit = int_timeSec/10;
timeSec_2ndDigit = int_timeSec%10;
}
}
void set_select()
{
digitalClock.displayReset();
delay(200);
digitalClock.displayClear();
delay(200);
digitalClock.displayZoneText(0, "Set", PA_CENTER, 100, 0, PA_NO_EFFECT, PA_NO_EFFECT);
/**************************************************************************************************/
int set_sel = 0;
int setButtonState = digitalRead(2);
while (setButtonState == HIGH)
{
setButtonState = digitalRead(2);
int downButtonState = digitalRead(3);
if (downButtonState == LOW)
{
set_sel--;
delay(150);
}
int upButtonState = digitalRead(5);
if (upButtonState == LOW)
{
set_sel++;
delay(150);
}
if (set_sel > 2)
{
set_sel = 0;
}
else if (set_sel < 0)
{
set_sel = 2;
}
String set_select_String = " ";
switch (set_sel)
{
case 0: // Set Day of the Week
set_select_String = "DOW";
set_DOW();
break;
case 1: // Set Date
set_select_String = "Date";
set_Date();
break;
case 2: // Set Time
set_select_String = "Time";
set_Time();
break;
}
String set_sel_Str = String (set_select_String);
char char_set_sel_Str[set_sel_Str.length()+1];
set_sel_Str.toCharArray(char_set_sel_Str,set_sel_Str.length()+1);
digitalClock.displayZoneText(1, char_set_sel_Str, PA_CENTER, 100, 0, PA_NO_EFFECT, PA_NO_EFFECT);
if (digitalClock.displayAnimate())
{
digitalClock.displayReset(1);
}
}
delay(100);
digitalClock.displayReset();
delay(200);
digitalClock.displayClear();
delay(200);
}
void set_DOW()
{
int setSelectButtonState = digitalRead(6);
if (setSelectButtonState == LOW)
{
digitalClock.displayClear();
delay(200);
digitalClock.displayReset();
delay(200);
digitalClock.displayZoneText(0, "Set", PA_CENTER, 100, 0, PA_NO_EFFECT, PA_NO_EFFECT);
/**************************************************************************************************/
int setSelectButtonState = digitalRead(6);
while (setSelectButtonState == HIGH)
{
setSelectButtonState = digitalRead(6);
int downButtonState = digitalRead(3);
if (downButtonState == LOW)
{
DOW--;
delay(150);
}
int upButtonState = digitalRead(5);
if (upButtonState == LOW)
{
DOW++;
delay(150);
}
if (DOW > 7)
{
DOW = 1;
}
else if (DOW < 1)
{
DOW = 7;
}
rtc.setDOW(DOW);
digitalClock.displayZoneText(1, rtc.getDOWStr(FORMAT_SHORT), PA_CENTER, 100, 0, PA_NO_EFFECT, PA_NO_EFFECT);
if (digitalClock.displayAnimate())
{
digitalClock.displayReset(1);
}
}
delay(100);
/**************************************************************************************************/
digitalClock.displayReset();
delay(200);
digitalClock.displayClear();
delay(200);
}
}
void set_Date()
{
int setSelectButtonState = digitalRead(6);
if (setSelectButtonState == LOW)
{
digitalClock.displayClear();
delay(200);
digitalClock.displayReset();
delay(200);
digitalClock.displayZoneText(0, "Set", PA_CENTER, 100, 0, PA_NO_EFFECT, PA_NO_EFFECT);
/**************************************************************************************************/
int setSelectButtonState = digitalRead(6);
while (setSelectButtonState == HIGH)
{
setSelectButtonState = digitalRead(6);
int days[31] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
int upButtonState = digitalRead(5);
if (upButtonState == LOW)
{
d++;
delay(150);
}
int downButtonState = digitalRead(3);
if (downButtonState == LOW)
{
d--;
delay(150);
}
if (d > 30)
{
d = 0;
}
else if (d < 0)
{
d = 30;
}
dd = days[d];
String dateStr = "D: " + String (dd);
char char_dateStr[dateStr.length()+1];
dateStr.toCharArray(char_dateStr,dateStr.length()+1);
digitalClock.displayZoneText(1, char_dateStr, PA_CENTER, 100, 0, PA_NO_EFFECT, PA_NO_EFFECT);
if (digitalClock.displayAnimate())
{
digitalClock.displayReset(1);
}
}
delay(500);
digitalClock.displayReset(1);
delay(5);
digitalClock.displayClear(1);
delay(5);
/**************************************************************************************************/
/**************************************************************************************************/
setSelectButtonState = digitalRead(6);
while (setSelectButtonState == HIGH)
{
setSelectButtonState = digitalRead(6);
int months[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int upButtonState = digitalRead(5);
if (upButtonState == LOW)
{
m++;
delay(150);
}
int downButtonState = digitalRead(3);
if (downButtonState == LOW)
{
m--;
delay(150);
}
if (m > 11)
{
m = 0;
}
else if (m < 0)
{
m = 11;
}
mm = months[m];
String dateStr = "M: " + String (mm);
char char_dateStr[dateStr.length()+1];
dateStr.toCharArray(char_dateStr,dateStr.length()+1);
digitalClock.displayZoneText(1, char_dateStr, PA_CENTER, 100, 0, PA_NO_EFFECT, PA_NO_EFFECT);
if (digitalClock.displayAnimate())
{
digitalClock.displayReset(1);
}
}
delay(500);
digitalClock.displayReset(1);
delay(5);
digitalClock.displayClear(1);
delay(5);
/**************************************************************************************************/
/**************************************************************************************************/
setSelectButtonState = digitalRead(6);
while (setSelectButtonState == HIGH)
{
setSelectButtonState = digitalRead(6);
int years[10] = {2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030};
int upButtonState = digitalRead(5);
if (upButtonState == LOW)
{
yy++;
delay(150);
}
int downButtonState = digitalRead(3);
if (downButtonState == LOW)
{
yy--;
delay(150);
}
if (yy > 9)
{
yy = 0;
}
else if (yy < 0)
{
yy = 9;
}
yyyy = years[yy];
String dateStr = "Y: " + String (yyyy);
char char_dateStr[dateStr.length()+1];
dateStr.toCharArray(char_dateStr,dateStr.length()+1);
digitalClock.displayZoneText(1, char_dateStr, PA_CENTER, 100, 0, PA_NO_EFFECT, PA_NO_EFFECT);
if (digitalClock.displayAnimate())
{
digitalClock.displayReset(1);
}
}
delay(100);
rtc.setDate(dd, mm, yyyy);
/**************************************************************************************************/
digitalClock.displayReset();
delay(200);
digitalClock.displayClear();
delay(200);
}
}
void set_Time()
{
int setSelectButtonState = digitalRead(6);
if (setSelectButtonState == LOW)
{
digitalClock.displayClear();
delay(200);
digitalClock.displayReset();
delay(200);
digitalClock.displayZoneText(0, "Set", PA_CENTER, 100, 0, PA_NO_EFFECT, PA_NO_EFFECT);
/**************************************************************************************************/
int setSelectButtonState = digitalRead(6);
while (setSelectButtonState == HIGH)
{
setSelectButtonState = digitalRead(6);
int hours[24] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
int upButtonState = digitalRead(5);
if (upButtonState == LOW)
{
h++;
delay(150);
}
int downButtonState = digitalRead(3);
if (downButtonState == LOW)
{
h--;
delay(150);
}
if (h > 23)
{
h = 0;
}
else if (h < 0)
{
h = 23;
}
hr = hours[h];
String timeStr = "HR: " + String (hr);
char char_timeStr[timeStr.length()+1];
timeStr.toCharArray(char_timeStr,timeStr.length()+1);
digitalClock.displayZoneText(1, char_timeStr, PA_CENTER, 100, 0, PA_NO_EFFECT, PA_NO_EFFECT);
if (digitalClock.displayAnimate())
{
digitalClock.displayReset(1);
}
}
delay(500);
digitalClock.displayReset(1);
delay(5);
digitalClock.displayClear(1);
delay(5);
/**************************************************************************************************/
/**************************************************************************************************/
setSelectButtonState = digitalRead(6);
while (setSelectButtonState == HIGH)
{
setSelectButtonState = digitalRead(6);
int minutes[60] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59};
int upButtonState = digitalRead(5);
if (upButtonState == LOW)
{
mi++;
delay(150);
}
int downButtonState = digitalRead(3);
if (downButtonState == LOW)
{
mi--;
delay(150);
}
if (mi > 59)
{
mi = 0;
}
else if (mi < 0)
{
mi = 59;
}
mins = minutes[mi];
String timeStr = "MIN: " + String (mins);
char char_timeStr[timeStr.length()+1];
timeStr.toCharArray(char_timeStr,timeStr.length()+1);
digitalClock.displayZoneText(1, char_timeStr, PA_CENTER, 100, 0, PA_NO_EFFECT, PA_NO_EFFECT);
if (digitalClock.displayAnimate())
{
digitalClock.displayReset(1);
}
}
delay(500);
digitalClock.displayReset(1);
delay(5);
digitalClock.displayClear(1);
delay(5);
/**************************************************************************************************/
/**************************************************************************************************/
setSelectButtonState = digitalRead(6);
while (setSelectButtonState == HIGH)
{
setSelectButtonState = digitalRead(6);
int seconds[60] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59};
int upButtonState = digitalRead(5);
if (upButtonState == LOW)
{
sc++;
delay(150);
}
int downButtonState = digitalRead(3);
if (downButtonState == LOW)
{
sc--;
delay(150);
}
if (sc > 59)
{
sc = 0;
}
else if (sc < 0)
{
sc = 59;
}
secs = seconds[sc];
String timeStr = "SEC: " + String (secs);
char char_timeStr[timeStr.length()+1];
timeStr.toCharArray(char_timeStr,timeStr.length()+1);
digitalClock.displayZoneText(1, char_timeStr, PA_CENTER, 100, 0, PA_NO_EFFECT, PA_NO_EFFECT);
if (digitalClock.displayAnimate())
{
digitalClock.displayReset(1);
}
}
delay(100);
rtc.setTime(hr, mins, secs);
/**************************************************************************************************/
digitalClock.displayClear();
delay(200);
digitalClock.displayReset();
delay(200);
}
}
void resetClock()
{
String timeMin = String (t.min, DEC);
int int_timeMin = timeMin.toInt();
String timeSec = String (t.sec, DEC);
int int_timeSec = timeSec.toInt();
if ((int_timeMin == 59) && (int_timeSec == 59)) // Reset every hour.
{
digitalWrite(resetPin, HIGH);
}
else {}
}