#include <Chrono.h>
Chrono chronoA;
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long previousTime0=0;
unsigned long lcdOn=60000;
int relay1Pin =10;
int relay2Pin =11;
int ledPin=12;
float cutoff = 10.80; //Cutoff voltage
float overvoltage = 14.20; //Overvoltage
float voltageHysteresis = 0.20;
int upButton =4;
int downButton =5;
int leftButton =6;
int rightButton =7;
int enterButton =8;
int buttonPushEnterCounter = 0; // counter for the number of button presses
int buttonEnterState = LOW;
int lastButtonEnterState = LOW;
//boolean buttonEnterState = LOW; // current state of the button
//boolean lastButtonEnterState = LOW; // previous state of the button
int analogInput1 = A0; // voltage measurement pin
int value1 = 0;
float vout1 = 0.0;
float vin1 = 0.0;
float R1a =235000;
float pot1 = 10000;
bool cFlag1 = false;
bool pinCheck1 = false;
long target1 = 20000; //1.1 min in sec
int mins1 = (target1 / 1000)/60;
int secs1 = round(target1 /1000)%60;
void setup() {
//Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.begin(16, 2);
lcd.setBacklight(1);
pinMode(analogInput1,INPUT); // Set pin A0 as Voltage input
pinMode(ledPin, OUTPUT);
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(upButton,INPUT_PULLUP);
pinMode(downButton,INPUT_PULLUP);
pinMode(leftButton,INPUT_PULLUP);
pinMode(rightButton,INPUT_PULLUP);
pinMode(enterButton,INPUT_PULLUP);
digitalWrite(ledPin,HIGH);
digitalWrite(enterButton, LOW);
lcd.setCursor(0,0);
lcd.print("Batt protection");
lcd.setCursor(0,1);
lcd.print("Vin:");
lcd.setCursor(5,1);
lcd.print(vin1,1);
lcd.setCursor(9,1);
lcd.print("Volt");
}
void loop() {
bool refresh = false;
value1 = analogRead(analogInput1);
vout1 = (value1 * 5.0) / 1024;
vin1 = vout1 / (pot1/(R1a+pot1));
unsigned long currentTime = millis();
if (currentTime - previousTime0 >=lcdOn){
lcd.setBacklight(0);
previousTime0=currentTime;
digitalWrite(ledPin,LOW);
}
buttonEnterState = digitalRead(enterButton); // read the pushbutton input pin
if (buttonEnterState != lastButtonEnterState)// compare the buttonState to its previous state
{
if (buttonEnterState == HIGH)// if the current state is HIGH then the button went from off to on
{
refresh = true;
lcd.setBacklight(1);
digitalWrite(ledPin,HIGH);
buttonPushEnterCounter++; // add one to counter
//Serial.println("on");
//Serial.print("number of button pushes: ");
//Serial.println(buttonPushEnterCounter);
if (buttonPushEnterCounter >3) // if counter over 5 reset the counter to 1 to show "Vin1:"
{
buttonPushEnterCounter =1;
}
if (refresh == true ) {
lcd.clear();
}
switch (buttonPushEnterCounter) // choose what to display based on buttonPushCounter value
{
case 1:
//lcd.clear();
if (refresh == true ) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("cutoff:");
lcd.setCursor(8,0);
lcd.print(cutoff,1);
lcd.setCursor(13,0);
lcd.print("V");
lcd.setCursor(0,1);
lcd.print("overvoltage");
lcd.setCursor(12,1);
lcd.print(overvoltage,1);
lcd.setCursor(16,1);
lcd.print("V");
}
break;
case 2:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Vin:");
lcd.setCursor(5,0);
lcd.print(vin1,1);
lcd.setCursor(10,0);
lcd.print("Volt");
lcd.setCursor(0,1);
lcd.print("Vin3:");
lcd.setCursor(5,1);
lcd.print(vin1,1);
lcd.setCursor(10,1);
lcd.print("Volt");
break;
case 3:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp Battery1 :");
lcd.setCursor(0,1);
lcd.print("C");
break;
}
}
lastButtonEnterState = buttonEnterState;// save the current state as the last state,for next time through the loop
}
if (vin1>=overvoltage + voltageHysteresis) {
digitalWrite(relay1Pin, HIGH);//Change the state of the relay1
lcd.setCursor(0,1);
lcd.print("Alarm ");
}
if (vin1<=overvoltage ) {
if (chronoA.elapsed() >= target1){
//if timer has stopped, reset here and trigger alarms
cFlag1 = false;
//pinCheck = false;
chronoA.stop();
digitalWrite(relay1Pin,LOW); //Change the state of the relay1
}
}
if(digitalRead(upButton)==0){
cutoff+=0.1;
lcd.setCursor(0,0);
lcd.print("cutoff=");
//Serial.print("cutoff=");
//Serial.println(cutoff);
lcd.setCursor(8,0);
lcd.print(cutoff,1);
while(digitalRead(upButton)==0);
delay(20);//Debounce
}
if(digitalRead(downButton)==0){
cutoff-= 0.1;
lcd.setCursor(0,0);
lcd.print("cutoff=");
//Serial.print("cutoff=");
//Serial.println(cutoff);
lcd.setCursor(8,0);
lcd.print(cutoff,1);
while(digitalRead(downButton)==0);
delay(20);//Debounce
}
}