#include <Wire.h>
#include <DS3231.h>
#include <Big_Digits.h>
#include <Arrays.h>
#include <ShipsBell.h>
#include <LiquidCrystal.h>
#define CLOCK_ADDRESS 0x68
const int encSwitchPin = 6;
bool timeFlag;
const int modePin = 6;
const int timeUpPin = 5;
const int timeDownPin = 4;
const int bellPin = 13;
int mode = 0;
bool starTickFlag;
bool setModeFlag;
int counter = 0; //DELETE
//LiquidCrystal (RS, EN, D4, D5, D6, D7);
Big_Digits lcd = Big_Digits(12, 11, 10, 9, 8, 7);//initialize display
DS3231 Clock;
RTClib RTC;
Arrays arr ;
Bell bell;
void setup() {
Wire.begin();
Serial.begin(115200);
pinMode(modePin, INPUT_PULLUP);
pinMode(timeUpPin, INPUT_PULLUP);
pinMode(timeDownPin, INPUT_PULLUP);
pinMode(bellPin, OUTPUT);
digitalWrite(bellPin, LOW);
lcd.begin(20, 4);//start display
for (int i = 0; i < 5; i++) {
lcd.createChar(i, lcd.glyphs[i]);
}
lcd.writeComma(13);
}
void loop() {
long unsigned curTime = millis();
static long unsigned startStarTickTime;
const long unsigned starStartTickPeriod = 250;
static long unsigned startSetTime;
const long unsigned setTimePeriod = 500;
DateTime now = RTC.now(); //Get time
//if pause time elapsed
if (curTime > (startStarTickTime + starStartTickPeriod)) {
startStarTickTime = curTime; //reset pause timer
lcd.writeLocStar(mode, starTickFlag);
starTickFlag = !starTickFlag; // set place for star
if (mode == 0) {
// avance time if normal mode
advanceTime(mode, timeFlag, now.second());
}
else { // set seconds to 0 while setting clock
Clock.setSecond(0);
lcd.PrintNumberString(0, 18, 0);
}
setMode(mode); // look for encoder switch and advance mode
}
if (curTime > (startSetTime + setTimePeriod)) {
modeAction(mode);
startSetTime = curTime; // reset sample time
getTemp();
}
}
String retDoW(int curDoW) {
curDoW --; // index Correction
String strDoW = arr.dayOfWeek[curDoW];
return strDoW;
}
String nowMonth(int newMonth) {
newMonth = newMonth - 1; //index correction
String strMonth = arr.monthList[newMonth];
return strMonth;
}
void advanceTime (int mode, bool& timeFlag, int seconds) {
static int oldSeconds;
if (mode == 0) {
if (oldSeconds != seconds) {
oldSeconds = seconds;
if (mode == 0) {
timeFlag = !timeFlag;
lcd.writeColon(timeFlag);
writeTime();
} else {
lcd.writeColon(true);
}
//tone(13, 800, 50) ;
bellStrike();
}
}
}
void bellStrike() {
DateTime now = RTC.now();
int nowHour = now.hour();
int nowMinute = now.minute();
int nowSecond = now.second();
bell.Ring(nowHour, nowMinute, nowSecond);
}
void setMode(int & mode) {
bool pinRead = digitalRead(encSwitchPin);
if (!pinRead && setModeFlag) {
mode ++;
if (mode > 5) {
mode = 0;
}
setModeFlag = false;
}
if (pinRead) {
setModeFlag = true;
}
}
void modeAction( int mode) {
// Serial.print("Mode Action ");
// Serial.println(mode);
DateTime now = RTC.now();
int newMinute = now.minute();
int newHour = now.hour(); ;
int newMonth = now.month();
int newDay = now.day();
int newYear = (now.year() - 2000);
switch (mode) {
case 0: //Do Nothing
break;
case 1: //set minutes
newMinute = setTime(newMinute);
if (newMinute > 59) {
newMinute = 0;
}
if (newMinute < 0) {
newMinute = 59;
}
Clock.setMinute(newMinute);
lcd.showBigNumber(newMinute, 10);
break;
case 2: //set hours
newHour = setTime(newHour);
if (newHour > 23) {
newHour = 0;
}
if (newHour < 0) {
newHour = 23;
}
Clock.setHour(newHour);
lcd.showBigNumber(newHour, 0);
break;
case 3: //set month
newMonth = setTime(newMonth);
if(newMonth >12){
newMonth = 1;
}
if(newMonth < 1){
newMonth = 12;
}
Clock.setMonth(newMonth);
writeTime();
break;
case 4: // set date
newDay = setTime(newDay);
Clock.setDate(newDay);
writeTime();
break;
case 5: // set year
newYear = setTime(newYear);
Clock.setYear(newYear);
writeTime();
break;
}
}
int setTime(int time) {
bool timeUpPressed = digitalRead(timeUpPin);
bool timeDownPressed = digitalRead(timeDownPin);
int newTime = time;
if (!timeUpPressed) {
newTime ++;
}
if (!timeDownPressed) {
newTime --;
}
return newTime;
}
int getTemp() {
if (mode == 0) {
unsigned long curTime = millis();
unsigned long tempPeriod = 2000;
static unsigned long startTime;
int temp = round(Clock.getTemperature());
int f_temp = ((temp * 1.8) + 32);
static bool tempFlag;
if (curTime > startTime + tempPeriod) {
startTime = curTime;
tempFlag = !tempFlag;
if (tempFlag) { //Cent
lcd.PrintNumberString(temp, 16, 2);
lcd.write(char(223));
lcd.print("C");
} else {
lcd.PrintNumberString(f_temp, 16, 2);
lcd.write(char(223));
lcd.print("F");
}
}
}
}
void writeTime() {
DateTime now = RTC.now();
lcd. PrintNumberString(now.second(), 18, 0);
int nowMonth = now.month() - 1;
String strNowMonth = arr.monthList[nowMonth];
lcd.writeString(strNowMonth, 10, 3);
lcd.PrintNumberString(now.day(), 11, 3);
lcd.PrintNumberString(now.year(), 15, 3);
lcd.writeString(retDoW(Clock.getDoW()), 13, 2);
lcd.showBigNumber(now.hour(), 0);
lcd.showBigNumber(now.minute(), 10);
lcd.setCursor(18, 2);
}