#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=7;
int ledPin2=8;
int ledPin3=9;
float cutoff = 48.80; //Cutoff voltage
float overvoltage1 = 56.20; //Overvoltage
float overvoltage2 =56.20;
float overvoltage3 =56.20;
float voltageHysteresis = 0.20;
int analogInput1 = A0; // voltage measurement pin
int value1 = 0;
float vout1 = 0.0;
float vin1 = 0.0;
float R1a =235000;
float pot1 = 10000;
bool relay1 = true;
bool pinCheck1 = true;
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.begin(9600);
lcd.setCursor(0,0);
lcd.print("Batt protection");
delay(3000);
//Serial.println();
}
void loop() {
value1 = analogRead(analogInput1);
vout1 = (value1 * 5.0) / 1024;
vin1 = vout1 / (pot1/(R1a+pot1));
lcd.setCursor(8,1);
lcd.print("V1:");
lcd.setCursor(11,1);
lcd.print(vin1,1);
lcd.setCursor(15,1);
lcd.print("V");
if (vin1<=cutoff){
digitalWrite(ledPin1,LOW); //Change the state
digitalWrite(relay1Pin,LOW);
lcd.setCursor(0,1);
lcd.print("Alarm ");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Low ");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Voltage");
delay(1000);
Serial.println("Alarm cutoff");
}
if (vin1>cutoff){
digitalWrite(ledPin1,HIGH); //Change the state
digitalWrite(relay1Pin,HIGH);
lcd.setCursor(0,0);
lcd.print("Batt protection");
lcd.setCursor(0,1);
lcd.print("Chargin ");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Ok ");
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 ");
}
if (vin1<=overvoltage1 ) {
if (chronoA.elapsed() >= target1){
//if timer has stopped, reset here and trigger alarms
//relay1 = false;
//pinCheck = false;
//chronoA.stop();
//digitalWrite(ledPin1,LOW); //Change the state of the relay1
//digitalWrite(relay1Pin,LOW);
}
}
pinCheck1 = digitalRead(ledPin1);
if (relay1 ==false) {
//Check pin
if (pinCheck1 ==false ) {
//pincheck will need to be changed here to whatever you use
relay1 =true; //Set flag to not test pin
chronoA.restart(0); //Starts timer at 0
//Serial.println(pinCheck);
Serial.println("Chrono Start ledPin read");
digitalWrite(ledPin1,LOW);
digitalWrite(relay1Pin,LOW);
}
}
if (vin1<=cutoff){
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("Countdown ");
Serial.println("Countdown");
lcd.setCursor(11,0);
lcd.print(mins1);
lcd.setCursor(12,0);
lcd.print(":");
lcd.setCursor(13,0);
lcd.print(secs1);
if (secs1<=10){
lcd.print(" ");
}
}
}
}
if (vin1>cutoff){
if (chronoA.elapsed() >= target1){
//if timer has stopped, reset here and trigger alarms
relay1 = true;
//pinCheck = false;
chronoA.stop();
//Serial.println(pinCheck);
digitalWrite(ledPin1,HIGH);
digitalWrite(relay1Pin,HIGH);
Serial.println("chronoA.stop ledPin read");
}
}
}