/*Détecteurs à ILS (ou à barrières IR) pour 1 seul sens de circulation
   14/01/2024
   Fantasio
   inspiré du projet de DDMARRION (voir lien 1)
   liens:  https://le-forum-du-n.1fr1.net/t38431-detecteurs-a-ils-ou-a-barrieres-ir-pour-1-seul-sens-de-circulation#698186
           https://wokwi.com/projects/386930035091363841
           https://forum.arduino.cc/t/partage-boutons-poussoirs/361656

             Brochage ATtiny85

               =|1  U  8|= VCC
  Sortie <-- 3 =|2     7|= 2
             4 =|3     6|= 1 <-- ILS B--GND
           GND =|4     5|= 0 <-- ILS A--GND

*/

#include "simpleBouton.h" // voir lien 3
const byte Sortie = 3;
bool sauvegardeEtatILS_A = false; // true si ILS A a été activé

simpleBouton ILS_A(0); //Cablage : pin---ILS---GND
simpleBouton ILS_B(1);

void setup() {
  pinMode(Sortie, OUTPUT);
  digitalWrite(Sortie, LOW); // la sortie est au niveau 0
} // fin setup

void loop() {
  ILS_A.actualiser();
  ILS_B.actualiser();

  // état initial les ILS n'ont rien détecté
  if ((ILS_A.estRelache()) && (ILS_B.estRelache()) && (sauvegardeEtatILS_A == false)) {
    digitalWrite(Sortie, LOW); // la sortie est au niveau 0
  } // fin if

  // l'ILS A a détecté un train
  if (ILS_A.vientDEtreEnfonce()) {
    sauvegardeEtatILS_A = true; // on mémorise la détection
  } // fin if

  // l'ILS B a détecté un train
  if ((ILS_B.vientDEtreEnfonce()) ) {
    // si c'est après une détection de l'ILS A, donc bon sens de circulation
    if (sauvegardeEtatILS_A == true) {
      digitalWrite(Sortie, HIGH); // alors la sortie passe au niveau 1 pendant 2 s
      delay(2000);
      sauvegardeEtatILS_A = false; // on retourne aux conditions initiales
    } // fin if
    
    // si c'est avant une détection de l'ILS A, donc mauvais sens de circulation
    else if (sauvegardeEtatILS_A == false) {
      digitalWrite(Sortie, LOW); // la sortie reste au niveau 0
    } // fin else if
  } // fin if
} // fin loop
ATTINY8520PU