// Déclaration des constantes et variables

const int PinUC = 0;
const int PinButton = 12;
const int PinRelay = 2;

int ValPinUC = 0;
float UC = 0.0;
int StateHigh = 0;
int StateLow = 0;
  
int ValButton = 0;


// Initialisation des entrées et sorties

void setup() {
  Serial.begin(9600);
  pinMode(PinButton, INPUT);
  pinMode(PinRelay, OUTPUT);
  Serial.println("Charge du condensateur en cours.");
  digitalWrite(PinRelay, 0);
  while (ValPinUC<1022) { 
    ValPinUC = analogRead(PinUC);
    }   
  Serial.println("Condensateur charge.");
  Serial.print("Maintenez le bouton poussoir appuye ");
  Serial.println("pour decharger le condensateur.");
}


// Fonction principale en boucle

void loop() {
  ValButton = digitalRead(PinButton);

if (ValButton == HIGH)
  {
  if (StateHigh==0){
    Serial.println("Decharge du condensateur:");
    StateHigh = 1;
    StateLow = 0;
  }
  digitalWrite(PinRelay, 1);
  ValPinUC = analogRead(PinUC);
  UC = (ValPinUC/1023.0)*5.0;
  Serial.print("UC (V): ");
  Serial.println(UC,2);
  delay(200);
  }
else {
  if (StateLow==0){
    ValPinUC = analogRead(PinUC);
    if (ValPinUC<1022){
      Serial.println("Charge du condensateur:");
    }
    StateLow = 1;
    StateHigh = 0;
  }
  digitalWrite(PinRelay, 0);
  while (ValPinUC<1022) { 
    ValPinUC = analogRead(PinUC);
    UC = (ValPinUC/1023.0)*5.0;
    Serial.print("UC (V): ");
    Serial.println(UC,2);
    delay(200);
    }
  } 
}
