// include the library code:
#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>
unsigned long time;
int  buttonPin = 6;     

// Data wire is plugged into pin A2 on the Arduino
#define ONE_WIRE_BUS A2

// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

int pompe1 = A4; // Diesel Pump
int pompe2 = A5; // Oil Pump
int interrup = 13; // Manual Switch
int vanne1 = 7; // Back diesel valve
int vanne2 = 8; // Back oil valve
int vanne3 = 9; // Diesel income valve
int vanne4 = 10; // Oil income valve

int buttonPushCounter = 0;   
int buttonState = 0;       
int lastButtonState = 0 ;  
int INTER = 0;
int positionCounter = 0;

//float tem1=0;
//float tem2=0;
float tem3 = 0.0;
//float vem1=0;
//float vem2=0;
//float vem3=0;
float tconsigne = 60; 
float dep = 0;
float fin = 0;
float a = 0;
float b = 0;
float c = 0;
float d = 0;
int i = 0;
int j = 0;
int k = 0;

// initialize the LCD with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  
  Serial.begin(9600);

  sensors.begin();
 
  pinMode(buttonPin, INPUT);

  pinMode(pompe1, OUTPUT);
  pinMode(pompe2, OUTPUT);
  pinMode(vanne1, OUTPUT);
  pinMode(vanne2, OUTPUT);
  pinMode(vanne3, OUTPUT);
  pinMode(vanne4, OUTPUT);
  pinMode(interrup, INPUT);
  //  pinMode(temp3, INPUT);
}

void loop() {

  // Write the state of the switch and store it in a variable
  buttonState = digitalRead(buttonPin);
  INTER = digitalRead(interrup);
  if (INTER == 0 && buttonState == LOW)
  {
    // Activation pompe 1
    analogWrite(pompe1, 0);
    // Desactivation pompe 2
    analogWrite(pompe2, 255);
    // Electrovanne 3 et 1 ON
    digitalWrite(vanne1, LOW);
    digitalWrite(vanne3, LOW);
    // Electrovanne 4 et 2 off
    digitalWrite(vanne2, HIGH);
    digitalWrite(vanne4, HIGH);
    lcd.begin(16, 2);
    // Print a message to the LCD.
    lcd.print("MANUAL DIESEL");
    lcd.setCursor(0, 1);
    lcd.print("ACTIVATED");
    delay(5000);
    buttonState = digitalRead(buttonPin);
    INTER = digitalRead(interrup);

    // set up the LCD's number of columns and rows:

    while (INTER == 0 && buttonState == LOW)
    {
      INTER = digitalRead(interrup);
      analogWrite(pompe1, 0);
      analogWrite(pompe2, 255);
      digitalWrite(vanne1, LOW);
      digitalWrite(vanne2, HIGH);
      digitalWrite(vanne3, LOW);
      digitalWrite(vanne4, HIGH);
      delay(1000);
      buttonState = digitalRead(buttonPin);
      INTER = digitalRead(interrup);
    }

  }

  if (buttonState == HIGH && INTER == 0)
  {

    while (digitalRead(buttonPin) == HIGH)
    {
      buttonState = digitalRead(buttonPin);
      INTER = digitalRead(interrup);
      analogWrite(pompe1, 255);
      analogWrite(pompe2, 255);
      digitalWrite(vanne1, HIGH);
      digitalWrite(vanne2, HIGH);
      digitalWrite(vanne3, HIGH);
      digitalWrite(vanne4, HIGH);
      lcd.begin(16, 2);
      lcd.print("System Break ");
      lcd.setCursor(0, 1);
      lcd.print("zzz... ");
      i = 0;
      j = 0;
      k = 0;
      delay(1000);
    }
  }
  if (buttonState == LOW && INTER == 1)
  {
    buttonState = digitalRead(buttonPin);
    INTER = digitalRead(interrup);
    sensors.requestTemperatures();
    tem3 = sensors.getTempCByIndex(0);
    if (tem3 < tconsigne && buttonState == LOW  && INTER == 1)
    {

      if (i == 0) {
        // Lancement tempo
        dep = millis();
      }
      // Activation pump 1
      analogWrite(pompe1, 0);
      // Desactivation pump 2
      analogWrite(pompe2, 255);
      // Valves 3 and 1 on
      digitalWrite(vanne1, LOW);
      digitalWrite(vanne3, LOW);
      // Valves 4 and 2 off
      digitalWrite(vanne2, HIGH);
      digitalWrite(vanne4, HIGH);
      i = i + 1;

      //    while (tem3 <= tconsigne  && INTER == 1)
      //    {
      buttonState = digitalRead(buttonPin);
      INTER = digitalRead(interrup);
      sensors.requestTemperatures();
      tem3 = sensors.getTempCByIndex(0);
      lcd.begin(16, 2);
      // Print a message to the LCD.
      lcd.print(" V.O. Preheating "); 
      lcd.setCursor(0, 1);
      lcd.print("Tliq : ");
      lcd.print(tem3);
      lcd.print(" c");
      delay(2000);
      //    }
    }
    if (tem3 > tconsigne && buttonState == LOW  && INTER == 1)
    {
      buttonState = digitalRead(buttonPin);
      INTER = digitalRead(interrup);
      if (j == 0) {
       
        a = millis();
        b = a - dep;
      }
      // Activation pump 2
      analogWrite(pompe2, 0);
      // Desactivation pump 1
      analogWrite(pompe1, 255);
      // Valves 3 and 1 off
      digitalWrite(vanne1, HIGH);
      digitalWrite(vanne3, HIGH);
      // Valves 4 and 2 on
      digitalWrite(vanne2, LOW);
      digitalWrite(vanne4, LOW);
   
      // set up the LCD's number of columns and rows:
      lcd.begin(16, 2);
      // Print a message to the LCD.
      lcd.print("Oil mode");
      lcd.setCursor(0, 1);
      lcd.print("Tliq:");
      lcd.print(tem3);
      lcd.print(" c");
      delay(5000);
      j = j + 1;
 
    }
  }

  if (buttonState == HIGH && INTER == 1)
  {
    buttonState = digitalRead(buttonPin);
    INTER = digitalRead(interrup);
    if (k == 0) {
      fin = millis();
      c = fin - a;
      d = c / (fin - dep) * 100;
    }
    // Activation pump 1
    analogWrite(pompe1, 0);
    // Desactivation pump 2
    analogWrite(pompe2, 255);
    // Valves 3   and 2 on
    digitalWrite(vanne2, LOW);
    digitalWrite(vanne3, LOW);
    // Valves 4 and 1 off
    digitalWrite(vanne1, HIGH);
    digitalWrite(vanne4, HIGH);
    
    lcd.begin(16, 2);

    lcd.print("Engine Cleaning..."); 
    delay(5000);
    lcd.setCursor(0, 1);

    lcd.print("Oil Mode ");
    lcd.print(d);
    lcd.print("%");
    k = k + 1;
    delay(25000);
    while (digitalRead(buttonPin) == HIGH)
    {
      buttonState = digitalRead(buttonPin);
      INTER = digitalRead(interrup);
      analogWrite(pompe1, 255);
      analogWrite(pompe2, 255);
      digitalWrite(vanne1, HIGH);
      digitalWrite(vanne2, HIGH);
      digitalWrite(vanne3, HIGH);
      digitalWrite(vanne4, HIGH);
      lcd.begin(16, 2);
      lcd.print("System Break");
      i = 0;
      j = 0;
      k = 0;
      delay(1000);
    }

  }

}