#include <LiquidCrystal_I2C.h>  // if you don´t have I2C version of the display, use LiquidCrystal.h library instead

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

const int pin = 3;
int pin2 = 2;
const int pinSortie = 9;
const int pinSortie2 = 10; // Utilisez la broche 9 (ou 10) qui est reliée au Timer 1
unsigned long Hpulse;
unsigned long Lpulse;
int cycletime = 0;
int frequency = 0;
unsigned long dureeHaute; 
unsigned long dureeBasse; 
int periode = 0;
int frequence = 0;
int RPM = 0;
unsigned long delai;  // Variable pour stocker le décalage temporel entre les signaux
char buffer[50]; 

void setup() {
  lcd.init();                       
  lcd.backlight();                  
  lcd.setCursor(0,1);
  lcd.print("   DynoDriver V1.0");
  delay(50);
  lcd.setCursor(0,2); 
  lcd.print("    GoSister Perf");
  delay(150);
  lcd.setCursor(0,1); 
  lcd.print("                   ");
  lcd.setCursor(0,2); 
  lcd.print("                   ");

  pinMode(pin, INPUT);
  pinMode(pinSortie, OUTPUT);
  pinMode(pinSortie2, OUTPUT);
  digitalWrite(pinSortie, LOW); // Assurez que la sortie commence en LOW

  // Configuration du Timer1 en mode CTC (Clear Timer on Compare Match)
  TCCR1A = 0;
  TCCR1B = 0;
  TCCR1B |= (1 << WGM12); // Mode CTC
  
  OCR1A = 91130; //91130
  OCR1B = 8056; //8056
  TIMSK1 |= (1 << OCIE1A) | (1 << OCIE1B);
  TCCR1B |= (1 << CS11);
  attachInterrupt(digitalPinToInterrupt(pin), changementEtat, CHANGE);
}

void changementEtat() {
  //delay(2);
  // Lis l'état de la broche 6 et le reproduit sur la broche 10
  int etatEntree = digitalRead(pin);
  digitalWrite(pinSortie2, etatEntree);
}

ISR(TIMER1_COMPA_vect) {
  digitalWrite(pinSortie, HIGH);
}

ISR(TIMER1_COMPB_vect) {
  digitalWrite(pinSortie, LOW);
}

void loop() {
  dureeHaute = pulseIn(pin, HIGH); // Durée en microsecondes du niveau haut
  dureeBasse = pulseIn(pin, LOW);  // Durée en microsecondes du niveau bas
  periode = dureeHaute + dureeBasse + 1;  
  frequence = 1000000 / periode;     
  Hpulse = pulseIn(pin2, HIGH);
  Lpulse = pulseIn(pin2, LOW);
  cycletime = Hpulse + Lpulse + 1;
  frequency = 1000000 / cycletime;
  RPM = frequence * 60;
    // Calcul du décalage temporel entre les deux signaux
  unsigned long debutSignal1 = pulseIn(pin, HIGH, 1000000);
  unsigned long debutSignal2 = pulseIn(pin2, HIGH, 1000000);
  delai = abs(debutSignal1 - debutSignal2);  // Décalage en microsecondes

  // Affichage sur l'écran LCD
  lcd.setCursor(0,0);                         
  lcd.print("Cycle Puls+ Puls-");
  lcd.setCursor(0,1);                         
  sprintf(buffer, "%2d", periode);   
  lcd.print(buffer);
  lcd.setCursor(6,1);                         
  sprintf(buffer, "%2d", dureeHaute);    
  lcd.print(buffer);
  lcd.setCursor(12,1);                         
  sprintf(buffer, "%2d", dureeBasse);
  lcd.print(buffer);   
  lcd.setCursor(0,2);                         
  sprintf(buffer, "%2d", cycletime);   
  lcd.print(buffer);
  lcd.setCursor(6,2);                         
  sprintf(buffer, "%2d", Hpulse);    
  lcd.print(buffer);
  lcd.setCursor(12,2);                         
  sprintf(buffer, "%2d", Lpulse);  
  lcd.print(buffer);
  lcd.setCursor(0,3);                         
  sprintf(buffer, "D:%3d", delai);   
  lcd.print(buffer);
  lcd.setCursor(11,3);                         
  sprintf(buffer, " RPM:%3d", RPM);
  lcd.print(buffer);
 
}