// Flame Spy
// Green LED if the fire is burning and everything is okay
// Red and Green LED is alternating and slow beep when the fire is going down and for some time no flame is detected.
// Red Sensor and fast beep when even this time is over.
// Snooze time is when the fire is to be restarted and we wait for the fire starts again. This is about 5 minutes
// Includes
// * No includes *
// Pin-Out Wemos ESP32 D1 Mini
int FlameSensorIn = 22; // Flame Sensor digital In
int SnoozeTastIn = 5; // Snooze Taster In
int BeeperOut = 21; // Beeper Out
int LedFlameSensor = 26; // Flame Sensor LED
int LedSnoozeTime = 19; // Snooze Time is running RGB-LED Blue
int LedFireStateRed = 18; // Fire State RGB-LED Red
int LedFireStateGreen = 23; // Fire State RGB-LED Green
// Variables
int SnoozeState = 0; // Snooze 0=OFF, 1=ON
int BeeperOutState = 0; // Beeper 0=OFF, 1=ON
int BeepCount = 0; // Beeper Sequenz counter for short beep and longer pause
int FlameState = 0; // Fire Sensor Vallue 1=Flame detected 0=No Flame
int FlameCount = 40; // Value is high if a fire is regulary sensed. Value is 0 when no fire is there for X times
int FlameOK = 40; // Vallue for good Fire status
int FireState = 0; // Status of the Fire 2=Okay 0=Down
int Weak = 50; // Beep Speed Status Weak - The higher the vallue the slower the beep
int Off = 5; // Beep Speed Status Off - The higher the vallue the slower the beep
int Ok = 0; // No Beep - Fire Ok
// Timer variables
const unsigned long SensorReadTimerSeq = 3000; // Sensor Sequenz Timer ms
const unsigned long BeeperTimerSeq = 50; // Beeper Sequenz Timer ms
const unsigned long SnoozeTimerSeq = 300000; // Snooze time with no activity, aside the Flame Sensor signal is shown
unsigned long prevSensorTime = 0;
unsigned long prevBeeperTime = 0;
unsigned long TriggerSnoozeTime = 0; // Calculated Time when Snooze Time is over
// SetUp Routine
void setup() {
// Set Serial Monitor
Serial.begin(115200);
// initialize digital pin LED_BUILTIN as an output - Not needed here -.
// pinMode(LED_BUILTIN, OUTPUT);
// Define PinModes
pinMode(FlameSensorIn, INPUT);
pinMode(SnoozeTastIn, INPUT);
pinMode(BeeperOut, OUTPUT);
pinMode(LedSnoozeTime, OUTPUT);
pinMode(LedFlameSensor, OUTPUT);
pinMode(LedFireStateRed, OUTPUT);
pinMode(LedFireStateGreen, OUTPUT);
}
// Main loop function
void loop() {
unsigned long currentTime = millis (); // read actual millis to take action
FlameState = digitalRead (FlameSensorIn); // Read Flame Sensor
digitalWrite (LedFlameSensor, FlameState);
if (FlameState == HIGH) {
FlameCount = FlameOK;
}
SnoozeState = digitalRead (SnoozeTastIn); // Read Snooze button
if (SnoozeState == HIGH){ // When Snooze is active all activities are paused until TriggerSnoozeTime is reached)
TriggerSnoozeTime = currentTime + SnoozeTimerSeq;
Serial.println("Snooze Time Running");
BeeperOutState = LOW; // Stopp the Beeper beeping
digitalWrite(BeeperOut, BeeperOutState);
digitalWrite(LedSnoozeTime, HIGH);// Low down all LEDs - Blue on
digitalWrite(LedFireStateGreen, LOW);
digitalWrite(LedFireStateRed, LOW);
FlameCount = FlameOK; // Set Counter hight that at the next begin it start from good condition
// delay(10000); // Snooze Time in ms
}
if(currentTime > TriggerSnoozeTime){ // Snooze Time is over - then proceed
// Read Flame Sensor every X seconds, Counter gives range between good, weak and bad
if (currentTime - prevSensorTime >= SensorReadTimerSeq){
prevSensorTime = currentTime;
if (FlameState == LOW){ // Flame alive - Calculate Flame Staus and set according LED
if(FlameCount > 0){ // Flame goes down Count slow down
FlameCount--;
}
}
Serial.println(FlameCount);
// set LED Flame Status
if (FlameCount >= FlameOK-20){ // Upper 3 States are okay - Mybe flickering
Serial.println("Flame OK");
FireState = Ok;
digitalWrite(LedFireStateGreen, HIGH);
digitalWrite(LedFireStateRed, LOW);
digitalWrite(LedSnoozeTime, LOW);
}
else {
if (FlameCount <= 0){
Serial.println("Flame OFF"); // Count down to 0 - Flame is down
FireState = Off;
digitalWrite(LedFireStateRed, HIGH);
digitalWrite(LedFireStateGreen, LOW);
digitalWrite(LedSnoozeTime, LOW);
}
else{
Serial.println("Flame WEAK"); // Between Okay and 0 Flame is Weak, have a look
FireState = Weak;
digitalWrite(LedFireStateGreen, !digitalRead(LedFireStateGreen));
digitalWrite(LedFireStateRed, !digitalRead(LedFireStateRed));
}
}
}
// Beeper Sequenz for Fire Off Alarm
if (currentTime - prevBeeperTime >= BeeperTimerSeq){
prevBeeperTime = currentTime;
if (BeeperOutState == LOW && FireState >= Off) {
if (BeepCount > FireState){ // one time ON X time Off - Short Beep
BeeperOutState = HIGH;
BeepCount = 0 ; }
else {
BeeperOutState = LOW;
BeepCount ++;
}
}
else {
BeeperOutState = LOW;
}
digitalWrite(BeeperOut, BeeperOutState);
}
}
}