#include <Chrono.h>
Chrono chronoA;
Chrono chronoB;
Chrono chronoC;
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int relay1Pin =10;
int relay2Pin =11;
int relay3Pin =12;
int ledPin1=9;
int ledPin2=8;
int ledPin3=7;
float cutoff = 10.80; //Cutoff voltage
float overvoltage1 = 14.20; //Overvoltage
float overvoltage2 =15.20;
float overvoltage3 =16.20;
float voltageHysteresis = 0.20;
int analogInput1 = A0; // voltage measurement pin
int value1 = 0;
float vout1 = 0.0;
float vin1 = 0.0;
float R1a =280000;
float pot1 = 10000;
bool cFlag1 = false;
bool pinCheck1 = false;
bool cFlag2 = false;
bool pinCheck2 = false;
bool cFlag3 = false;
bool pinCheck3 = false;
long target1 = 20000; //1.1 min in sec
int mins1 = (target1 / 1000)/60;
int secs1 = round(target1 /1000)%60;
long target2 = 30000; //1.1 min in sec
int mins2 = (target2 / 1000)/60;
int secs2 = round(target2 /1000)%60;
long target3 = 40000; //1.1 min in sec
int mins3 = (target3 / 1000)/60;
int secs3 = round(target3 /1000)%60;
void setup() {
pinMode(analogInput1,INPUT); // Set pin A0 as Voltage input
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(relay3Pin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
lcd.init(); // initialize the lcd
lcd.begin(16, 2);
lcd.setBacklight(1);
//Serial.println();
}
void loop() {
value1 = analogRead(analogInput1);
vout1 = (value1 * 5.0) / 1024;
vin1 = vout1 / (pot1/(R1a+pot1));
lcd.setCursor(0,1);
lcd.print("Vin:");
lcd.setCursor(4,1);
lcd.print(vin1,1);
lcd.setCursor(8,1);
lcd.print("V");
delay(1000);
if (vin1>=overvoltage1 + voltageHysteresis) {
digitalWrite(ledPin1,HIGH); //Change the state of the relay1
digitalWrite(relay1Pin, HIGH);
lcd.setCursor(0,1);
lcd.print("Alarm ");
delay(1000);
}
if (vin1<=overvoltage1 ) {
if (chronoA.elapsed() >= target1){
//if timer has stopped, reset here and trigger alarms
cFlag1 = false;
//pinCheck = false;
chronoA.stop();
digitalWrite(ledPin1,LOW); //Change the state of the relay1
digitalWrite(relay1Pin,LOW);
}
}
if (vin1>=overvoltage2 + voltageHysteresis) {
digitalWrite(ledPin2,HIGH); //Change the state of the relay1
digitalWrite(relay2Pin, HIGH);
}
if (vin1<=overvoltage2 ) {
if (chronoB.elapsed() >= target2){
//if timer has stopped, reset here and trigger alarms
cFlag2 = false;
//pinCheck = false;
chronoB.stop();
digitalWrite(ledPin2,LOW); //Change the state of the relay1
digitalWrite(relay2Pin,LOW);
}
}
if (vin1>=overvoltage3 + voltageHysteresis) {
digitalWrite(ledPin3,HIGH); //Change the state of the relay1
digitalWrite(relay3Pin, HIGH);
}
if (vin1<=overvoltage3 ) {
if (chronoC.elapsed() >= target3){
//if timer has stopped, reset here and trigger alarms
cFlag3 = false;
//pinCheck = false;
chronoC.stop();
digitalWrite(ledPin3,LOW); //Change the state of the relay1
digitalWrite(relay3Pin,LOW);
}
}
pinCheck1 = digitalRead(relay1Pin);
if (cFlag1 == false) {
//Check pin
if (pinCheck1 == true) {
//pincheck will need to be changed here to whatever you use
cFlag1 = true; //Set flag to not test pin
chronoA.restart(0); //Starts timer at 0
//Serial.println(pinCheck);
//Serial.println("Start");
digitalWrite(ledPin1, HIGH);
}
} else if (chronoA.elapsed() < target1){
//If timer running, send to display here
long test1 = target1 - chronoA.elapsed();
int testSecs1 = round(test1 / 1000);
if (round(testSecs1/60) != mins1){
mins1 = round(testSecs1/60);
}
if (round(testSecs1%60)!= secs1) {
secs1 = round(testSecs1%60);
lcd.setCursor(0,0);
lcd.print("C1");
lcd.setCursor(3,0);
lcd.print(mins1);
lcd.setCursor(4,0);
lcd.print(":");
lcd.setCursor(5,0);
lcd.print(secs1);
if (secs1<=10){
lcd.print(" ");
lcd.setCursor(6,0);
}
}
}
else if (chronoA.elapsed() >= target1){
//if timer has stopped, reset here and trigger alarms
cFlag1 = false;
//pinCheck = false;
chronoA.stop();
//Serial.println(pinCheck);
digitalWrite(ledPin1, LOW);
//Serial.print("Countdown has ended");
}
pinCheck2 = digitalRead(relay2Pin);
if (cFlag2 == false) {
//Check pin
if (pinCheck2 == true) {
//pincheck will need to be changed here to whatever you use
cFlag2 = true; //Set flag to not test pin
chronoB.restart(0); //Starts timer at 0
//Serial.println(pinCheck);
//Serial.println("Start");
digitalWrite(ledPin2, HIGH);
}
} else if (chronoB.elapsed() < target2){
//If timer running, send to display here
long test2 = target2 - chronoB.elapsed();
int testSecs2 = round(test2 / 1000);
if (round(testSecs2/60) != mins2){
mins2 = round(testSecs2/60);
}
if (round(testSecs2%60)!= secs2) {
secs2 = round(testSecs2%60);
//Serial.print(mins); // Print out countdown time
//Serial.print(":");
//Serial.println(secs);
lcd.setCursor(9,0);
lcd.print("C2");
lcd.setCursor(12,0);
lcd.print(mins2);
lcd.setCursor(13,0);
lcd.print(":");
lcd.setCursor(14,0);
lcd.print(secs2);
if (secs2<=10){
lcd.print(" ");
lcd.setCursor(15,0);
}
}
} else if (chronoB.elapsed() >= target2){
//if timer has stopped, reset here and trigger alarms
cFlag2 = false;
//pinCheck = false;
chronoB.stop();
//Serial.println(pinCheck);
digitalWrite(ledPin2, LOW);
//Serial.print("Countdown has ended");
}
pinCheck3 = digitalRead(relay3Pin);
if (cFlag3 == false) {
//Check pin
if (pinCheck3 == true) {
//pincheck will need to be changed here to whatever you use
cFlag3 = true; //Set flag to not test pin
chronoC.restart(0); //Starts timer at 0
//Serial.println(pinCheck);
//Serial.println("Start");
digitalWrite(ledPin3, HIGH);
}
} else if (chronoC.elapsed() < target3){
//If timer running, send to display here
long test3 = target3 - chronoC.elapsed();
int testSecs3 = round(test3 / 1000);
if (round(testSecs3/60) != mins3){
mins3 = round(testSecs3/60);
}
if (round(testSecs3%60)!= secs3) {
secs3 = round(testSecs3%60);
lcd.setCursor(9,1);
lcd.print("C3");
lcd.setCursor(12,1);
lcd.print(mins3);
lcd.setCursor(13,1);
lcd.print(":");
lcd.setCursor(14,1);
lcd.print(secs3);
if (secs2<=10){
lcd.print(" ");
lcd.setCursor(15,1);
}
}
} else if (chronoC.elapsed() >= target3){
//if timer has stopped, reset here and trigger alarms
cFlag3 = false;
//pinCheck = false;
chronoC.stop();
//Serial.println(pinCheck);
digitalWrite(ledPin3, LOW);
//Serial.print("Countdown has ended");
}
}