/*************************************************
TEST B1 + B2
Coupure zones B1 si B2 occupé
*************************************************/
// ===== B1 =====
const int B1_OCC = 22;
const int B1_LIB = 23;
const int B1_APP = 24;
const int B1_ZONE_STOP = 30;
const int B1_ZONE_APP = 31;
// ===== B2 =====
const int B2_OCC = 25;
const int B2_LIB = 26;
const int B2_APP = 27;
// ===== ETATS =====
bool b1Occupied = false;
bool b1Approach = false;
bool b2Occupied = false;
// mémoires boutons
bool lastB1Occ = HIGH;
bool lastB1Lib = HIGH;
bool lastB1App = HIGH;
bool lastB2Occ = HIGH;
bool lastB2Lib = HIGH;
void setup() {
Serial.begin(9600);
Serial.println("B1 + B2 prêt");
pinMode(B1_OCC, INPUT_PULLUP);
pinMode(B1_LIB, INPUT_PULLUP);
pinMode(B1_APP, INPUT_PULLUP);
pinMode(B2_OCC, INPUT_PULLUP);
pinMode(B2_LIB, INPUT_PULLUP);
pinMode(B1_ZONE_STOP, OUTPUT);
pinMode(B1_ZONE_APP, OUTPUT);
// zones alimentées au départ
digitalWrite(B1_ZONE_STOP, HIGH);
digitalWrite(B1_ZONE_APP, HIGH);
}
void loop() {
readButtons();
computeLogic();
delay(50);
}
void readButtons() {
// ===== B1 =====
bool o1 = digitalRead(B1_OCC);
bool l1 = digitalRead(B1_LIB);
bool a1 = digitalRead(B1_APP);
if (lastB1Occ == HIGH && o1 == LOW) {
b1Occupied = true;
Serial.println("B1 OCCUPE");
}
if (lastB1Lib == HIGH && l1 == LOW) {
b1Occupied = false;
b1Approach = false;
Serial.println("B1 LIBERE");
}
if (lastB1App == HIGH && a1 == LOW) {
b1Approach = true;
Serial.println("B1 APPROCHE");
}
lastB1Occ = o1;
lastB1Lib = l1;
lastB1App = a1;
// ===== B2 =====
bool o2 = digitalRead(B2_OCC);
bool l2 = digitalRead(B2_LIB);
if (lastB2Occ == HIGH && o2 == LOW) {
b2Occupied = true;
Serial.println("B2 OCCUPE");
}
if (lastB2Lib == HIGH && l2 == LOW) {
b2Occupied = false;
Serial.println("B2 LIBERE");
}
lastB2Occ = o2;
lastB2Lib = l2;
}
void computeLogic() {
// Condition ferroviaire
if (b2Occupied && b1Approach) {
digitalWrite(B1_ZONE_STOP, LOW);
digitalWrite(B1_ZONE_APP, LOW);
} else {
digitalWrite(B1_ZONE_STOP, HIGH);
digitalWrite(B1_ZONE_APP, HIGH);
}
}
Canton B1