#include <Chrono.h>
Chrono chronoA; 
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_ADDRESS 0x3C
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

float cutoff = 48.80; //Cutoff voltage
float overvoltage1 = 56.20; //Overvoltage
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;
int relay1Pin =10;
int relay2Pin =11;
int relay3Pin =12;
int led1Pin=7;
int led2Pin=8;

bool cFlag1 = false; // current state of the pin
bool pinCheck1 = false; // previous state of pin

bool ledPin1State=false;
bool lastledPin1State=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);
  Serial.println(lastledPin1State);
Serial.println(ledPin1State);
pinMode(analogInput1,INPUT); // Set pin A0 as Voltage input
pinMode(relay1Pin, OUTPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
  // initialize OLED display with address 0x3C for 128x64
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);
  }
   delay(1000);         // wait for initializing
display.clearDisplay();// Clear the buffer.
display.setTextSize(1);          // text size
display.setTextColor(WHITE);     // text color
display.setCursor(0, 5);        // position to display
display.println("Battery protection"); // text to display
display.setCursor(0, 20);        // position to display
display.println("V1 input:"); // text to display
display.setCursor(60, 20);        // position to display
display.println(vin1,1); // text to display
display.setCursor(0, 35);        // position to display
display.println("V2 input:"); // text to display
display.setCursor(0, 50);        // position to display
display.println("        "); // text to display
display.display();               // show on OLED
delay(2000);

}

void loop() {
value1 = analogRead(analogInput1);
vout1 = (value1 * 5.0) / 1024;
vin1 = vout1 / (pot1/(R1a+pot1));
Serial.println(vin1);
delay(1000);

  if (vin1<=cutoff){ 
  digitalWrite(led1Pin,HIGH);  //Change the state
  //ledPin1State=true;
  //Serial.println(lastledPin1State);
  //Serial.println(ledPin1State);
  //digitalWrite(relay1Pin, HIGH);
  lowVoltage();
  delay(500);
  Serial.println("Cutoff Alarm"); 
}

if (vin1>cutoff ){
if (led1Pin=LOW){  
//digitalWrite(led1Pin,LOW);//Change the state of the led1
digitalWrite(relay1Pin,HIGH);
Serial.println("vin1>cutoff");
chronoA.restart(0); //Starts timer at 0
Serial.println("Chrono started");
}
}

ledPin1State= digitalRead(led1Pin);
if (ledPin1State != lastledPin1State) { // compare the led1State to its previous state
if (ledPin1State == HIGH) {//if the current state is HIGH then the led went from off to on
Serial.println("on");
} 
}
else {// if the current state is LOW then the led1 went from on to off:
Serial.println("off");
if (chronoA.elapsed() < target1){
  Serial.println("timer running");
    //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);
    }
}
if (chronoA.elapsed() >= target1){ //if timer has stopped, reset here and trigger alarms
    ledPin1State = false; //pinCheck = false;
    chronoA.stop();
    Serial.println("Timer stoped");
    digitalWrite(led1Pin, LOW);
    //Serial.print("Countdown has ended");
  }
}
delay(50);// Delay a little bit to avoid bouncing

lastledPin1State = ledPin1State;// save the current state as the last state, for next time through the loop

}

void lowVoltage()  {
display.clearDisplay();// Clear the buffer.
  display.setTextSize(2);          // text size
  display.setTextColor(WHITE);     // text color
  display.setCursor(0, 0);  
  display.println("Alarm  ");
  delay(500);  
  display.println("Low");  
  display.println("Voltage");
  display.display();    // show on OLED
}
void countdown() {
   //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);

    }
display.clearDisplay();// Clear the buffer.
  display.setTextSize(2);          // text size
  display.setTextColor(WHITE);     // text color
  display.setCursor(0,10); // position to display
  display.println("Countdown"); // text to display
  //display.setCursor(40,70);
  display.print(mins1);
  //display.setCursor(50,80);
  display.print(":");
  //display.setCursor(60,90);
  display.print(secs1);
  if (secs1<=10){
 display.print(" ");
 display.display();    // show on OLED
  }
}
void normal()  {
display.clearDisplay();// Clear the buffer.
  display.setTextSize(2);      // text size
  display.setTextColor(WHITE); // text color
  display.setCursor(0, 30);    // position to display
  display.println("Charging"); // text to display
  display.display();           // show on OLED
  delay(500);
}