#include <Arduino.h>
#include <TM1637Display.h>//Inclure la bibliothèque TM1637

const int poussoir = 12;  //La broche 12 est le poussoir
const int relais = 10;    //La broche 10 est le relais
const int ledch = 9;      //La broche 9 est la led "charge"
const int ledef = 7;      //La broche 7 est la led "défaut"
const int GATE = 5;       //La broche 5 est la GATE du MOSFET
const int V = A0;         //La broche A0 est l'entrée analogique V
const int CLK = 3;        //La broche 3 est CLK
const int DIO = 2;        //La broche 2 est DIO
TM1637Display display(CLK, DIO);    //Broches utilisées

int Van;               //Valeur analogique
int Tension;           //Valeur analogique convertie en tension
bool Mes = true;       //Mesure pile
bool Char = false;     //Charge pile
bool Def = false;      //Défaut pile
int T1H = 0;               //Temporisation 1 heure

const uint8_t noPL[] = {
  SEG_C | SEG_E | SEG_G,                            // n
  SEG_C | SEG_D | SEG_E | SEG_G,                    // o
  SEG_A | SEG_B | SEG_E | SEG_F | SEG_G,            // P
  SEG_D | SEG_E | SEG_F                             // L
  };

const uint8_t dEFt[] = {
  SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,            // d
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G,            // E
  SEG_A | SEG_E | SEG_F | SEG_G,                    // F
  SEG_D | SEG_E | SEG_F | SEG_G                     // t
  };

void setup()
{
  display.setBrightness(7);
  pinMode(relais, OUTPUT);
  pinMode(GATE, OUTPUT);
  pinMode(ledef, OUTPUT);
  pinMode(ledch, OUTPUT);
  pinMode (poussoir, INPUT_PULLUP); //Résistance pull up interne
  Serial.begin(57600);          //Initialisation moniteur série
  digitalWrite(ledef, LOW);         //Extinction LED défaut
  digitalWrite(ledch, LOW);         //Extinction LED charge    
  digitalWrite(relais, LOW);        //Ouverture relais
  digitalWrite(GATE, HIGH);         //Blocage MOSFET
}

void loop()
{
  Van = analogRead(V);              //Fausse lecture

debut:
  //Poussoir "charge" 
  if (digitalRead (poussoir) == LOW and Tension > 500)
  {
    Mes = false;
    Char = true;
    digitalWrite(relais, HIGH);     //Fermeture relais
    digitalWrite(GATE, LOW);        //Conduction MOSFET
  }

  delay(1000);                           //Tempo 1 sec
  Van = analogRead(V);                   //Lecture valeur
  //la borne Aref reçoit 2,5V par le TL431   
  Tension = int(Van*2.5/1023*1000);      //Conversion en tension
  Serial.print(Tension);                 //Affichage de la tension
  Serial.println(" mV");                 //en mV
  
  if (Tension > 1800)                
  {
    Serial.print("Erreur Mesure");
    digitalWrite(relais, LOW);        //Ouverture relais
    digitalWrite(GATE, HIGH);         //Blocage MOSFET
    goto debut;
  }

  if (Tension < 500)                //Pile non présente
  {                
    Mes = true;
    Char = false;
    Def = false;
    T1H = 0;
    Serial.println("PAS DE PILE");
    display.setSegments(noPL);        //Affiche noPL
    digitalWrite(relais, LOW);        //Ouverture relais
    digitalWrite(GATE, HIGH);         //Blocage MOSFET
    digitalWrite(ledef, LOW);         //Extinction LED défaut
    digitalWrite(ledch, LOW);         //Extinction LED charge     
    goto debut;
  }

  display.showNumberDec(Tension, false); //Digits significatifs
  
  if (T1H != 0)                     //Fin tempo 1 heure
  {
    T1H = T1H-1;
    Mes = true;
    digitalWrite(GATE, HIGH);       //Blocage MOSFET
    Serial.print("MESURE de la PILE: ");
    goto debut;
  }
  
  //Tension pile < 1,25V   
  if (Tension < 1250 and Tension > 500 and Def==0) 
  {               
    Serial.print("DEFAUT: ");
    display.setSegments(dEFt);        //Affiche dEFt
    Serial.print(Tension);            //Affichage de la tension
    Serial.println(" mV");            //en mV
    digitalWrite(ledef, HIGH);        //Allumage LED défaut
    Mes = true;
    Char = false;
    Def= true;
    delay(5000);
    goto debut;
   }
    
  if (Char == true and Mes == false)    //Charge pile
  {           
       
    if (Tension > 1700)           //Tension pile > 1,7V
    {               
      Serial.println("ARRET DE LA CHARGE");
      digitalWrite(GATE, HIGH);   //Blocage MOSFET
      digitalWrite(ledch, LOW);   //Extinction LED charge
      Mes = true;
      T1H =3600;
    }
    else 
    {
      Serial.print("CHARGE de la PILE: ");  
      digitalWrite(GATE, LOW);            //Conduction MOSFET
      digitalWrite(ledch, HIGH);       //Allumage LED charge
    }
    goto debut; 
  }
  
  else                                    //Mesure pile
  {                               
    Serial.print("MESURE de la PILE: ");
    digitalWrite(GATE, HIGH);             //Blocage MOSFET  
    if (Char==true and Tension < 1550) //Tension pile < 1,55V
    {     
      Mes = false;
      digitalWrite(GATE, LOW);         //Conduction MOSFET
      digitalWrite(ledch, HIGH);       //Allumage LED charge
    }
  }
 }
