#include <ACS712.h>
#include "Wire.h"
#include "TimerOne.h"
#include "LiquidCrystal_I2C.h"
ACS712 mySensor(ACS712_05B,A1);
ACS712 mySensor2(ACS712_05B,A0);

LiquidCrystal_I2C LCD(0x27,16,2); // définit le type d'ecran lcd 16 x 2
int pin1 = 2,pin2 = 3;
unsigned short compt = 0;
bool Flag = false;
short demi_cycle = 0;
bool Activate = true;

void setup() {

  Serial.begin(9600);
  LCD.init(); // initialisation de l'afficheur
  LCD.backlight();
   
  LCD.setCursor(1, 0);
  LCD.print("Nombre de cycle :");
  LCD.setCursor(1, 1);
  LCD.println(compt);
  LCD.setCursor(1,2);
  LCD.print("HUB1 : ");
  LCD.setCursor(1,3);
  LCD.print("HUB2 : ");
  pinMode(pin1,OUTPUT);
  pinMode(pin2,OUTPUT);

  mySensor.calibrate();
  mySensor2.calibrate();

  Timer1.initialize(5000000);
  Timer1.attachInterrupt(time);
}

void loop() {
  if(Activate != false){

    float current = mySensor.getCurrentAC();
    float current2 = mySensor2.getCurrentAC();
  
    Serial.println(current-0.05);
    Serial.println(current2-0.05);

  }

  LCD.setCursor(1,1);
  //Serial.println(compt);
  if ((Flag == true) && (Activate == true)) {
    digitalWrite(pin1,HIGH);
    digitalWrite(pin2,HIGH);
  }else{
    digitalWrite(pin1,LOW);
    digitalWrite(pin2,LOW);
  }
  
  if (compt == 2){
    LCD.setCursor(1, 2);  
    Activate = false;
    digitalWrite(pin1,HIGH);
    digitalWrite(pin2,HIGH);
    //compt = 0;
  }else{
    LCD.print(compt);
  }
  //Serial.println(compt);
  LCD.display();
  
}

void time(void){

  Flag=!Flag;
  if(Activate != false){
  demi_cycle += 1;
  //Serial.println(demi_cycle);
  if (demi_cycle == 3){
    compt += 1;
    demi_cycle=0;
}
}

}