#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
// --- CONFIGURATION DES BROCHES ---
#define PIN_TEMP 32 // Potentiomètre simulation Température
#define PIN_PRESSURE 36 // Potentiomètre simulation Pression
#define PIN_ESTOP 34 // Bouton (Pull-down externe 10k)
#define PIN_FLAME 33 // Switch IR (LOW = Flamme détectée)
#define PIN_SRV_BUT 12
#define PIN_SRV_SYN 13
#define PIN_REL_BUT 25 // LED Rouge
#define PIN_REL_SYN 26 // LED Verte
#define PIN_REL_PUMP 27 // LED Bleue
#define PIN_REL_IGN 14 // LED Violette (Étincelle)
// --- OBJETS ---
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo servoButane;
Servo servoSyngas;
// --- VARIABLES D'ÉTAT ---
float tempC = 0.0;
float pressureBar = 0.0;
bool eStopPressed = false;
bool flameDetected = false;
String systemState = "IDLE"; // IDLE, RUNNING, ESTOP
unsigned long lastUpdate = 0;
void setup() {
Serial.begin(115200);
// Initialisation I/O
pinMode(PIN_ESTOP, INPUT);
pinMode(PIN_FLAME, INPUT_PULLUP);
pinMode(PIN_REL_BUT, OUTPUT);
pinMode(PIN_REL_SYN, OUTPUT);
pinMode(PIN_REL_PUMP, OUTPUT);
pinMode(PIN_REL_IGN, OUTPUT);
shutDownAll(); // Sécurité au démarrage
// Initialisation Servos
servoButane.attach(PIN_SRV_BUT);
servoSyngas.attach(PIN_SRV_SYN);
servoButane.write(0); // Vanne fermée
servoSyngas.write(0); // Vanne fermée
// Initialisation LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Smart Pyrolyse");
lcd.setCursor(0, 1);
lcd.print("System Ready");
delay(2000);
}
void loop() {
if (millis() - lastUpdate > 500) {
readSensors();
checkSafety();
updateLCD();
executeLogic();
lastUpdate = millis();
}
}
void readSensors() {
// SIMULATION TEMPERATURE (0 à 4095 -> 0 à 500°C)
int tempRaw = analogRead(PIN_TEMP);
tempC = map(tempRaw, 0, 4095, 0, 500);
// SIMULATION PRESSION (0 à 4095 -> 0.00 à 5.00 Bar)
int pressRaw = analogRead(PIN_PRESSURE);
pressureBar = (pressRaw / 4095.0) * 5.0;
eStopPressed = (digitalRead(PIN_ESTOP) == HIGH);
flameDetected = (digitalRead(PIN_FLAME) == LOW);
}
void shutDownAll() {
digitalWrite(PIN_REL_BUT, LOW);
digitalWrite(PIN_REL_SYN, LOW);
digitalWrite(PIN_REL_PUMP, LOW);
digitalWrite(PIN_REL_IGN, LOW);
servoButane.write(0);
servoSyngas.write(0);
}
void checkSafety() {
if (eStopPressed) {
systemState = "ESTOP";
shutDownAll();
return;
}
if (pressureBar > 0.10) {
systemState = "P_ALARM";
shutDownAll();
return;
}
if (systemState == "RUNNING" && !flameDetected) {
systemState = "NO_FLAME";
shutDownAll();
return;
}
// Auto-recovery
if (!eStopPressed && pressureBar <= 0.10 && (systemState == "ESTOP" || systemState == "P_ALARM")) {
systemState = "IDLE";
}
}
void executeLogic() {
if (systemState != "IDLE" && systemState != "RUNNING") return;
if (systemState == "IDLE") {
if (flameDetected) {
systemState = "RUNNING";
}
}
if (systemState == "RUNNING") {
digitalWrite(PIN_REL_BUT, HIGH);
if (tempC < 400.0) {
servoButane.write(90);
} else {
servoButane.write(10);
}
}
}
void updateLCD() {
lcd.clear();
if (systemState == "ESTOP") {
lcd.setCursor(0, 0); lcd.print("!!! E-STOP !!!");
lcd.setCursor(0, 1); lcd.print("VALVES CLOSED");
return;
}
if (systemState == "P_ALARM") {
lcd.setCursor(0, 0); lcd.print("! HIGH PRESSURE!");
lcd.setCursor(0, 1); lcd.print("P: " + String(pressureBar, 2) + " Bar");
return;
}
if (systemState == "NO_FLAME") {
lcd.setCursor(0, 0); lcd.print("! FLAME LOST !");
lcd.setCursor(0, 1); lcd.print("SYSTEM HALTED");
return;
}
lcd.setCursor(0, 0);
lcd.print("T:"); lcd.print(tempC, 0); lcd.print("C P:"); lcd.print(pressureBar, 2);
lcd.setCursor(0, 1);
if (systemState == "RUNNING") {
lcd.print("ST:RUNNING");
} else {
lcd.print("ST:IDLE / READY");
}
}