// Déclaration des constantes et variables

const int PinUC = 0;
const int PinButton = 12;
const int PinRelay = 2;
const int UcMax = 1000;
const int UcMin = 800;

int ValPinUC = 0;
int ValButton = 0;
int OldValButton = 0;
int State = 0;
int OldState = 0;

  

// Initialisation des entrées et sorties

void setup() {
  Serial.begin(9600);
  pinMode(PinButton, INPUT);
  pinMode(PinRelay, OUTPUT);
  digitalWrite(PinRelay, 0);
  while (ValPinUC<UcMax) { 
    ValPinUC = analogRead(PinUC);
    }   
  Serial.println("Appuyez sur le bouton poussoir pour commencer le flash.");
}


// Fonction principale en boucle

void loop() {

ValButton = digitalRead(PinButton);
delay(10);

if ((ValButton == HIGH)&&(OldValButton == LOW)) 
  {
  State=1-State;
  }
OldValButton = ValButton;

if (State==1) 
  {
  if (OldState == 0)
    {
    Serial.println("Flash periodique en cours.");
    OldState=1;
    }
  ValPinUC = analogRead(PinUC);
  if (ValPinUC > UcMax)
  {
    digitalWrite(PinRelay, 1);
    while (ValPinUC>UcMin) { 
      ValPinUC = analogRead(PinUC);
    }
  }
  else {
    digitalWrite(PinRelay, 0);
    while (ValPinUC<UcMax) { 
      ValPinUC = analogRead(PinUC);
      }
    } 
  }
else{
  if (OldState == 1){
    Serial.println("Fin du flash.");
    OldState = 0;
    digitalWrite(PinRelay, 1);
    ValPinUC = analogRead(PinUC);
    while (ValPinUC>UcMin) { 
      ValPinUC = analogRead(PinUC);
    }
  } 
}
}
