#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
RTC_DS1307 rtc;
char daysOfTheWeek[7][24] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
LiquidCrystal_I2C lcd(0x27, 20, 4);
#define heater 2
#define filter 3
#define light 4
#define waste 5
#define clean 6
#define indicatorWaste 7
#define indicatorClean 8
#define txEnable 9
#define auxRelay 10
#define bklt 11
#define tripATO 12
// #define xx 13
// #define xx 14
// #define xx 15
// #define xx 16
// #define xx 17
// #define SDA 18 (A4)
// #define SCL 19 (A5)
// #define xx 20
// #define xx 21
int address[33] = {
100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
130, 131, 132
};
char data[64] = {0};
int y100;
int y200;
float y[19];
int commandEStop = 9110;
int commandStart = 1200;
int display_1 = 1201;
int display_2 = 1202;
int display_3 = 1203;
int display_4 = 1204;
int commandDisplay = 1250;
int commandStop = 1300;
int commandATO = 1401;
int commandAWC = 1402;
int commandBacklight = 1403;
int commandNoBacklight = 1404;
int packet1 = 201;
int packet2 = 202;
int packet3 = 203;
int packet4 = 204;
int packet5 = 205;
int packet6 = 206;
int comRelease = 207;
long int hourToMinute;
long int hourToSec;
long int minuteToSec;
long int nowSec;
long int lastSec;
int a;
int b;
int c;
int d;
int e;
int f;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Serial: Baud 9600");
delay(1000);
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
if (! rtc.begin()) {
abort();
lcd.setCursor(0,1);
lcd.print("Serial: Baud 9600");
}
else {
lcd.setCursor(0,1);
lcd.print("RTC Begin");
}
delay(1000);
pinMode(heater, OUTPUT);
pinMode(filter, OUTPUT);
pinMode(light, OUTPUT);
pinMode(auxRelay, OUTPUT);
pinMode(txEnable, OUTPUT);
pinMode(indicatorWaste, OUTPUT);
pinMode(indicatorClean, OUTPUT);
pinMode(waste, INPUT_PULLUP);
pinMode(clean, INPUT_PULLUP);
pinMode(bklt, INPUT_PULLUP);
pinMode(tripATO, INPUT_PULLUP);
digitalWrite(heater , LOW);
digitalWrite(filter , LOW);
digitalWrite(light , LOW);
digitalWrite(auxRelay , LOW);
digitalWrite(txEnable , LOW);
digitalWrite(indicatorWaste , LOW);
digitalWrite(indicatorClean , LOW);
DateTime now = rtc.now();
hourToMinute = (now.hour() * 60);
hourToSec = (hourToMinute * 60);
minuteToSec = now.minute() * 60;
nowSec = hourToSec + minuteToSec + now.second();
lastSec = nowSec;
lcd.setCursor(0,2);
lcd.print(nowSec);
delay(1000);
lcd.clear();
lcd.noBacklight();
delay(500);
a = 0;
b = 0;
c = 0;
d = 0;
e = 0;
f = 0;
}
void loop() {
DateTime now = rtc.now();
hourToMinute = (now.hour() * 60);
hourToSec = (hourToMinute * 60);
minuteToSec = now.minute() * 60;
nowSec = hourToSec + minuteToSec + now.second();
if(nowSec == 0) {
lastSec = nowSec;
}
updateDisplay();
xx();
// serial communication
byte n = Serial.available();
if (n != 0) {
byte m = Serial.readBytesUntil('\n', data, 64);
data[m] = '\0'; //null-byte
y100 = atoi(strtok(data, ","));
y200 = atoi(strtok(NULL, ","));
if(y100 == address[5]) {
if(y200 == packet1) {
y[1] = atof(strtok(NULL, ","));
y[2] = atof(strtok(NULL, ","));
y[3] = atof(strtok(NULL, ","));
}
}
}
if((digitalRead(bklt) == LOW) && (e == 0)) {
e = 1;
delay(300);
}
else if((digitalRead(bklt) == LOW) && (e == 1)) {
e = 2;
delay(300);
}
else if((digitalRead(bklt) == LOW) && (e == 2)) {
e = 0;
delay(300);
}
if((digitalRead(tripATO) == LOW) && (f == 0)) {
f = 1;
delay(300);
}
if(e == 0) {
// 10:30 to 22:00
if((nowSec >= 37800) && (nowSec < 79200) ) {
digitalWrite(light, HIGH);
lcd.backlight();
c = 1;
}
else {
digitalWrite(light, LOW);
lcd.noBacklight();
c = 0;
}
}
else if(e == 1) {
digitalWrite(light, HIGH);
lcd.backlight();
c = 1;
}
else if(e == 2) {
digitalWrite(light, LOW);
lcd.noBacklight();
c = 0;
}
if(nowSec == 37800 || nowSec == 50400 || nowSec == 57600 || nowSec == 64800 /*|| now.second() == 10*/) {
if((b == 0) && (digitalRead(clean) == LOW)) {
digitalWrite(txEnable, HIGH);
Serial.print(address[5]);
Serial.print(",");
Serial.print(packet1);
Serial.print(",");
Serial.print(commandStart);
Serial.print(",");
Serial.println(commandATO);
digitalWrite(txEnable, LOW);
b = 1;
}
}
else if((y[1] == commandStop) && (y[2] == commandATO)) {
b = 0;
}
if(now.day() == 3 /*|| now.second() == 10*/) {
if((nowSec == 43200) && (a == 0) && (digitalRead(waste) == HIGH) && (digitalRead(clean) == LOW)) {
digitalWrite(txEnable, HIGH);
Serial.print(address[5]);
Serial.print(",");
Serial.print(packet1);
Serial.print(",");
Serial.print(commandStart);
Serial.print(",");
Serial.println(commandATO);
digitalWrite(txEnable, LOW);
a = 1;
}
else if((a == 1) && (y[1] == commandStop) && (y[2] == commandATO)) {
digitalWrite(txEnable, HIGH);
Serial.print(address[5]);
Serial.print(",");
Serial.print(packet1);
Serial.print(",");
Serial.print(commandStart);
Serial.print(",");
Serial.println(commandAWC);
digitalWrite(txEnable, LOW);
digitalWrite(heater, HIGH);
digitalWrite(filter, HIGH);
a = 2;
y[1] = 0;
y[2] = 0;
}
}
else if((y[1] == commandStop) && (y[2] == commandAWC)) {
digitalWrite(heater, LOW);
digitalWrite(filter, LOW);
a = 0;
}
if((f == 1) && (digitalRead(waste) == HIGH) && (digitalRead(clean) == LOW)) {
digitalWrite(txEnable, HIGH);
Serial.print(address[5]);
Serial.print(",");
Serial.print(packet1);
Serial.print(",");
Serial.print(commandStart);
Serial.print(",");
Serial.println(commandATO);
digitalWrite(txEnable, LOW);
f = 2;
}
else if((f == 2) && (y[1] == commandStop) && (y[2] == commandATO)) {
digitalWrite(txEnable, HIGH);
Serial.print(address[5]);
Serial.print(",");
Serial.print(packet1);
Serial.print(",");
Serial.print(commandStart);
Serial.print(",");
Serial.println(commandAWC);
digitalWrite(txEnable, LOW);
digitalWrite(heater, HIGH);
digitalWrite(filter, HIGH);
f = 3;
}
else if((y[1] == commandStop) && (y[2] == commandAWC)) {
digitalWrite(heater, LOW);
digitalWrite(filter, LOW);
y[1] = 0;
y[2] = 0;
f = 0;
}
}
void updateDisplay() {
DateTime now = rtc.now();
lcd.setCursor(0,0);
if(now.month() < 10){
lcd.print("0");
lcd.print(now.month());
}
else {
lcd.print(now.month());
}
lcd.print("/");
if(now.day() < 10){
lcd.print("0");
lcd.print(now.day());
}
else {
lcd.print(now.day());
}
lcd.print("/");
lcd.print(now.year());
lcd.print(" ");
if(now.hour() < 10){
lcd.print("0");
lcd.print(now.hour());
}
else {
lcd.print(now.hour());
}
lcd.print(":");
if(now.minute() < 10){
lcd.print("0");
lcd.print(now.minute());
}
else {
lcd.print(now.minute());
}
lcd.print(":");
if(now.second() < 10){
lcd.print("0");
lcd.print(now.second());
}
else {
lcd.print(now.second());
}
/* hourToMinute = (now.hour() * 60);
hourToSec = (hourToMinute * 60);
minuteToSec = now.minute() * 60;
nowSec = hourToSec + minuteToSec + now.second(); */
/*
lcd.setCursor(0,1);
lcd.print(now.hour()); lcd.print(" "); lcd.print(hourToSec);
lcd.setCursor(0,2);
lcd.print(now.minute()); lcd.print(" "); lcd.print(minuteToSec);
lcd.setCursor(0,3);
lcd.print(now.second()); lcd.print(" "); lcd.print(nowSec);
*/
lcd.setCursor(0,1);
//Sunday (6 spaces)
if(now.dayOfTheWeek() == 0) {
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(" ");
}
//Monday (6 spaces)
if(now.dayOfTheWeek() == 1) {
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(" ");
}
//Tuesday (5 spaces)
if(now.dayOfTheWeek() == 2) {
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(" ");
}
//Wednesday (3 spaces)
if(now.dayOfTheWeek() == 3) {
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(" ");
}
//Thursday (4 spaces)
if(now.dayOfTheWeek() == 4) {
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(" ");
}
//Friday (6 spaces)
if(now.dayOfTheWeek() == 5) {
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(" ");
}
//Saturday (4 spaces)
if(now.dayOfTheWeek() == 6) {
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(" ");
}
if(nowSec < 10) {
lcd.print("0000");
lcd.print(nowSec);
}
else if(nowSec > 10 && nowSec < 100) {
lcd.print("000");
lcd.print(nowSec);
}
else if(nowSec > 100 && nowSec < 1000) {
lcd.print("00");
lcd.print(nowSec);
}
else if(nowSec > 1000 && nowSec < 10000) {
lcd.print("0");
lcd.print(nowSec);
}
else if(nowSec > 10000) {
lcd.print(nowSec);
}
lcd.setCursor(0,2);
if(a != 0) {
lcd.print("**AWC**");
}
else if(f != 0) {
lcd.print("**MWC**");
}
else if(b == 1) {
lcd.print("**ATO**");
}
else {
lcd.print(" ");
}
lcd.setCursor(8,2);
if(e == 0) {
lcd.print("Light Auto");
}
else if(e == 1) {
lcd.print("Light On ");
}
else if(e == 2) {
lcd.print("Light Off ");
}
lcd.setCursor(0,3);
if((digitalRead(waste) == LOW) && (digitalRead(clean) == HIGH)) {
lcd.print("!!Waste!! !!Clean!! ");
digitalWrite(indicatorWaste, HIGH);
digitalWrite(indicatorClean, HIGH);
}
else if ((digitalRead(waste) == LOW) && (digitalRead(clean) == LOW)) {
lcd.print("!!Waste!! ");
digitalWrite(indicatorWaste, HIGH);
digitalWrite(indicatorClean, LOW);
}
else if ((digitalRead(waste) == HIGH) && (digitalRead(clean) == LOW)) {
lcd.print("a:");
lcd.print(a);
lcd.print(" b:");
lcd.print(b);
lcd.print(" f:");
lcd.print(f);
digitalWrite(indicatorWaste, LOW);
digitalWrite(indicatorClean, LOW);
}
else if ((digitalRead(waste) == HIGH) && (digitalRead(clean) == HIGH)) {
lcd.print("!!Clean!! ");
digitalWrite(indicatorWaste, LOW);
digitalWrite(indicatorClean, HIGH);
}
}
void xx() {
if(nowSec >= lastSec) {
if((nowSec > lastSec) && (nowSec < (lastSec + 6)) && (d == 0)) {
digitalWrite(txEnable, HIGH);
Serial.print(address[5]);
Serial.print(",");
Serial.print(packet1);
Serial.print(",");
Serial.print(commandDisplay);
Serial.print(",");
Serial.print(display_1);
Serial.print(",");
if(c == 1) {
Serial.println(commandBacklight);
}
else if (c == 0) {
Serial.println(commandNoBacklight);
}
digitalWrite(txEnable, LOW);
d = 1;
}
else if((nowSec > (lastSec + 6)) && (nowSec < (lastSec + 12)) && (d == 1)) {
digitalWrite(txEnable, HIGH);
Serial.print(address[5]);
Serial.print(",");
Serial.print(packet1);
Serial.print(",");
Serial.print(commandDisplay);
Serial.print(",");
Serial.print(display_2);
Serial.print(",");
if(c == 1) {
Serial.println(commandBacklight);
}
else if (c == 0) {
Serial.println(commandNoBacklight);
}
digitalWrite(txEnable, LOW);
d = 2;
}
else if((nowSec > (lastSec + 12)) && (nowSec < (lastSec + 18)) && (d == 2)) {
digitalWrite(txEnable, HIGH);
Serial.print(address[5]);
Serial.print(",");
Serial.print(packet1);
Serial.print(",");
Serial.print(commandDisplay);
Serial.print(",");
Serial.print(display_3);
Serial.print(",");
if(c == 1) {
Serial.println(commandBacklight);
}
else if (c == 0) {
Serial.println(commandNoBacklight);
}
digitalWrite(txEnable, LOW);
d = 3;
}
else if((nowSec > (lastSec + 18)) && (nowSec < (lastSec + 24)) && (d == 3)) {
digitalWrite(txEnable, HIGH);
Serial.print(address[5]);
Serial.print(",");
Serial.print(packet1);
Serial.print(",");
Serial.print(commandDisplay);
Serial.print(",");
Serial.print(display_4);
Serial.print(",");
if(c == 1) {
Serial.println(commandBacklight);
}
else if (c == 0) {
Serial.println(commandNoBacklight);
}
digitalWrite(txEnable, LOW);
d = 4;
}
else if(nowSec >= (lastSec + 24)) {
lastSec = nowSec;
d = 0;
}
}
}