#include <DS1307RTC.h>
#include <Wire.h>
#include <Time.h>
#include <EEPROM.h>
#define OFFSET 3
#define READINGS 10
#define ADDRESS 0
#define EVENINGADDRESS 10
#define NIGHTADDRESS 20
int upPin = 4;
int downPin = 5;
int menuPin = 2;
int exitPin = 3;
int tempPin = 0;
int heaterPin = 8;
int ledPin = 13;
time_t timeNow = 0;
time_t lastCheck = 0;
time_t timeset = 0;
time_t switchTime = 0;
time_t screenTimer = 0;
int total = 0;
int average = 0;
int reading = 0;
float tempC = 0.00F;
float tempF = 0.00F;
float tempReading = 0.00F;
int tempSetting = 65;
int state = 0;
int check = 0;
int firstTime = 0;
int menuLevel = 0;
int initial = 0;
int manualSet = 0;
int manualRun = 0;
int manualOff = 0;
int runTime = 1;
int menuMode = 0;
int stepMenu = 0;
int eveningTemp = 0;
int nightTemp = 0;
TimeElements timeSetting;
void setup()
{
Serial.begin(9600);
while (!Serial) {
;
}
setSyncProvider(RTC.get); // the function to get the time from the RTC
setSyncInterval(10);
if (timeStatus() != timeSet) {
Serial.print("Unable to sync with the RTC");
}
delay(100);
Serial.write(12);
Serial.write(17);
delay(5);
Serial.print("Initalizing...");
delay(1000);
EEPROM.get(ADDRESS, tempSetting);
EEPROM.get(EVENINGADDRESS, eveningTemp);
EEPROM.get(NIGHTADDRESS, nightTemp);
printScreen("Retrieving Temp", "From EEPROM");
delay(1000);
printTempInfo();
timeNow = now();
screenTimer = now();
printClock();
pinMode(upPin, INPUT);
pinMode(downPin, INPUT);
pinMode(heaterPin, OUTPUT);
pinMode(exitPin, INPUT);
pinMode(menuPin, INPUT);
}
void loop()
{
timeNow = now();
if (digitalRead(menuPin) == HIGH) {
Serial.write(17);
if (menuMode == 0) {
menuMode = 1;
menuLevel = 0;
stepMenu = 0;
delay(250);
} else {
stepMenu = 1;
delay(250);
}
}
if (digitalRead(exitPin) == HIGH) {
Serial.write(17);
if (menuMode == 0) {
manualSet = 1;
initial = 0;
} else {
exitMenu();
}
delay(250);
}
if (manualSet == 1) {
if (state == 1) {
if (initial == 0) {
printScreen("Turn Off For", String(runTime) + " minutes");
initial = 1;
} else {
Serial.write(148);
if (runTime < 10) {
Serial.print("0");
}
Serial.print(String(runTime));
delay(20);
}
runTime = varChange(runTime, 60, 1);
if (digitalRead(exitPin) == HIGH) {
manualSet = 0;
initial = 0;
runTime = 1;
manualOff = 0;
}
if (digitalRead(menuPin) == HIGH) {
printScreen("Turning Off...", "");
delay(500);
switchTime = now() + (runTime * 60);
state = 0;
manualOff = 1;
manualSet = 0;
runTime = 1;
initial = 0;
delay(1000);
lastCheck = now() - 20;
}
} else {
if (initial == 0) {
printScreen("Turn On For", String(runTime) + " minutes");
initial = 1;
} else {
Serial.write(148);
if (runTime < 10) {
Serial.print("0");
}
Serial.print(String(runTime));
delay(20);
}
runTime = varChange(runTime, 60, 1);
if (digitalRead(exitPin) == HIGH) {
manualSet = 0;
initial = 0;
runTime = 1;
manualRun = 0;
}
if (digitalRead(menuPin) == HIGH) {
printScreen("Turning On...", "");
delay(500);
switchTime = now() + (runTime * 60);
state = 1;
manualRun = 1;
manualSet = 0;
runTime = 1;
initial = 0;
delay(1000);
lastCheck = now() - 20;
}
}
} else if (menuMode != 0) {
if (stepMenu == 1) {
menuLevel = menuLevel + 1;
stepMenu = 0;
initial = 0;
delay(250);
}
if (firstTime == 0) {
printScreen("Entering", "Menu Mode...");
timeSetting.Year = year() - 1970;
timeSetting.Month = month();
timeSetting.Day = day();
timeSetting.Hour = hour();
timeSetting.Minute = minute();
EEPROM.get(EVENINGADDRESS, eveningTemp);
EEPROM.get(NIGHTADDRESS, nightTemp);
delay(2000);
firstTime = 1;
}
if (menuLevel == 0) {
printSettingPage("Temp", tempSetting);
tempSetting = varChange(tempSetting, 80, 40);
} else if (menuLevel == 1) {
printSettingPage("Year", timeSetting.Year);
timeSetting.Year = varChange(timeSetting.Year, 99, 0);
} else if (menuLevel == 2) {
printSettingPage("Month", timeSetting.Month);
timeSetting.Month = varChange(timeSetting.Month, 12, 1);
} else if (menuLevel == 3) {
printSettingPage("Day", timeSetting.Day);
timeSetting.Day = varChange(timeSetting.Day, 31, 1);
} else if (menuLevel == 4) {
printSettingPage("Hour", timeSetting.Hour);
timeSetting.Hour = varChange(timeSetting.Hour, 23, 0);
} else if (menuLevel == 5) {
printSettingPage("Minute", timeSetting.Minute);
timeSetting.Minute = varChange(timeSetting.Minute, 60, 0);
} else if (menuLevel == 6) {
printSettingPage("Evening", eveningTemp);
eveningTemp = varChange(eveningTemp, 80, 40);
} else if (menuLevel == 7) {
printSettingPage("Night", nightTemp);
nightTemp = varChange(nightTemp, 80, 40);
}
} else {
timeNow = now();
if (timeNow > screenTimer + 30) {
Serial.write(18);
}
if (digitalRead(upPin) == HIGH || digitalRead(downPin) == HIGH) {
Serial.write(17);
screenTimer = now();
}
printClock();
if (switchTime <= timeNow) {
manualRun = 0;
}
if (timeNow >= lastCheck + 10) {
if (hour() == 18 && minute() == 00) {
EEPROM.get(EVENINGADDRESS, tempSetting);
} else if (hour() == 21 && minute() == 00) {
EEPROM.get(NIGHTADDRESS, tempSetting);
} else if (hour() == 05 && minute() == 00) {
EEPROM.get(ADDRESS, tempSetting);
}
tempReading = readTemp();
printTempInfo();
if (manualRun == 0) {
if (tempReading <= tempSetting - OFFSET) {
state = 1;
}
if (tempReading >= tempSetting + OFFSET) {
state = 0;
}
}
lastCheck = now();
}
runHeater();
}
}
float readTemp()
{
for (int thisReading = 0; thisReading < READINGS; thisReading++) {
reading = analogRead(tempPin);
total += reading;
}
average = total / READINGS;
total = 0;
tempC = ((float(average) * (5000.0 / 1024.0)) - 500.0) / 10.0;
tempF = (tempC * 9.0 / 5.0) + 32.0;
return tempF;
}
void printClock()
{
if (manualRun == 0) {
String clockHour = "";
String clockMinute = "";
Serial.write(158);
if (hour() < 10) {
clockHour = "0" + String(hour());
} else {
clockHour = String(hour());
}
if (minute() < 10) {
clockMinute = "0" + String(minute());
} else {
clockMinute = String(minute());
}
Serial.print(clockHour + ":" + clockMinute);
} else {
Serial.write(141);
Serial.print("man");
Serial.write(158);
if (switchTime - timeNow > 60) {
if ((((switchTime - timeNow) / 60) + 1) < 10) {
Serial.print(0);
}
Serial.print(String(((switchTime - timeNow) / 60) + 1));
Serial.write(161);
Serial.print("min");
} else if (switchTime - timeNow == 60) {
Serial.print(String(01));
Serial.write(161);
Serial.print("min");
} else {
if ((switchTime - timeNow) < 10) {
Serial.print(0);
}
Serial.print(String(switchTime - timeNow));
Serial.write(161);
Serial.print("sec");
}
}
}
void exitMenu()
{
menuMode = 0;
menuLevel = 0;
firstTime = 0;
initial = 0;
Serial.write(12);
Serial.print("Writing Vars");
EEPROM.put(EVENINGADDRESS, eveningTemp);
EEPROM.put(NIGHTADDRESS, nightTemp);
if (hour() >= 18 && hour() < 21) {
EEPROM.put(EVENINGADDRESS, tempSetting);
} else if (hour() >= 21) {
EEPROM.put(NIGHTADDRESS, tempSetting);
} else if (hour() < 5) {
EEPROM.put(NIGHTADDRESS, tempSetting);
} else {
EEPROM.put(ADDRESS, tempSetting);
}
setTime(makeTime(timeSetting));
delay(10);
timeset = now();
RTC.set(timeset);
Serial.write(13);
Serial.print("Leaving Menu");
timeNow = now();
lastCheck = now() - 20;
delay(1000);
}
void runHeater()
{
Serial.write(138);
if (state == 1) {
Serial.print("ON");
digitalWrite(heaterPin, HIGH);
digitalWrite(ledPin, HIGH);
} else {
Serial.print("OFF");
digitalWrite(heaterPin, LOW);
digitalWrite(ledPin, LOW);
}
}
void printTempInfo()
{
Serial.write(12);
delay(5);
Serial.print("Temp: " + String(int(tempReading)));
Serial.write(148);
Serial.print("Set: " + String(int(tempSetting)));
}
int varChange(int var, int maximum, int minimum)
{
if (digitalRead(upPin) == HIGH) {
var++;
delay(200);
}
if (digitalRead(downPin) == HIGH) {
var--;
delay(200);
}
if (var > maximum) {
var = minimum;
} else if (var < minimum) {
var = maximum;
}
return var;
}
void printSettingPage(String text, int var)
{
if (text == "Year") {
var = var + 1970;
}
if (initial == 0) {
Serial.write(12);
Serial.print("Set " + text + ": ");
initial = 1;
} else {
Serial.write(140);
if (var < 10) {
Serial.print(0);
}
Serial.print(String(var));
delay(20);
}
}
void printScreen(String line1, String line2)
{
Serial.write(12);
delay(5);
Serial.print(line1);
Serial.write(148);
Serial.print(line2);
}