/*
**************************************************************
* Coal Stoker Flame Size Detector *
* Inspired by Uri Shaked project at *
* https://wokwi.com/arduino/projects/305193627138654786 *
* Layout used as starting point *
* Copyright 2022 John Clark *
* Version 0.10 *
**************************************************************
*/
/*
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); //define I2C address 0x27, 16 column and 2 rows
*/
//#include <SPI.h>
#include <Wire.h>
#include "Adafruit_GFX.h"
#include "Adafruit_SSD1306.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int flamelevel = 0; // mapped and inverted % of sensor range
#define greenLED 7 // fire okay or firing
#define redLED 2 // warning or flashing alert
// empirically relate flame % to actual fire condition (TBD)
const int minSurvive = 15; // minimum level for idle, below is outfire
const int idleLow = 20; // lowest reading for healthy idle
const int idleTarget = 30; // target reading for resting idle
const int firingLow = 70; // lowest reading for actively firing
const int firingHigh = 90; // reading for full firing
String stringOne, stringTwo, stringThree;
int analogValue;
void setup() {
delay(10);
/*lcd.init();
lcd.clear();
lcd.backlight();
*/
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.clearDisplay();
display.display();
Serial.begin(9600);
display.clearDisplay();
display.setCursor(5,10);
display.println(" Flame Sensor");
printOled();
pinMode(greenLED, OUTPUT); // set green pin led as output
digitalWrite(greenLED, LOW); // turn off green led
pinMode(redLED, OUTPUT); // set red led pin as output
digitalWrite(redLED, LOW); // turn off red led
}
void loop() {
analogValue = analogRead(A0);
Serial.print("Sensor RAW: ");
Serial.println(analogValue);
flamelevel = map(analogValue, 0, 1024, 100, 0);
Serial.print(flamelevel);
Serial.println("%");
// disabling the lcd commands makes serial print work
/*lcd.setCursor(0, 0);
lcd.print(F("Flame: "));
*/
/*display.clearDisplay();
drawText(5, 10, 2, "Thanks For The Help!");
display.display();
*/
if (flamelevel >= firingHigh) { // stoker is fully firing
// lcd.print("Full Fire");
display.clearDisplay();
display.setCursor(5,10);
display.println(" Full Fire");
printOled();
/*display.print(" ");
display.print(flamelevel);
display.println("%");
display.print(" Raw:");
display.print(analogValue);
display.display();
*/
/*stringOne = "Full Fire ";
stringOne = stringOne + flamelevel;
stringTwo = stringOne + "% Raw:";
stringThree = stringTwo + analogValue;
Serial.println(stringOne);
drawText(5, 10, 2, stringThree);
display.setCursor(5,10);
*/
digitalWrite(greenLED, HIGH); // turn on green led
digitalWrite(redLED, LOW); // turn off red led
delay(300);
digitalWrite(greenLED, LOW); // turn off green led for flash
}
if ((flamelevel >= firingLow) && (flamelevel < firingHigh)) { // stoker is firing
//lcd.print("Firing ");
display.clearDisplay();
display.setCursor(5,10);
display.println(" Firing");
printOled();
/*
display.print(" ");
display.print(flamelevel);
display.println("%");
display.print(" Raw:");
display.print(analogValue);
display.display();
*/
/*display.clearDisplay();
stringOne = " Firing ";
stringOne = stringOne + flamelevel;
stringTwo = stringOne + "% Raw:";
stringThree = stringTwo + analogValue;
Serial.println(stringOne);
drawText(5, 10, 2, stringThree);
display.display();
*/
digitalWrite(greenLED, HIGH); // turn on green led
digitalWrite(redLED, LOW); // turn off red led
}
if ((flamelevel < firingLow) && (flamelevel > idleLow) ) { // idle fire
//lcd.print("Idle fire ");
display.clearDisplay();
display.setCursor(5,10);
display.println(" Idle Fire");
printOled();
/*
display.clearDisplay();
stringOne = "Idle Fire ";
stringOne = stringOne + flamelevel;
stringTwo = stringOne + "% Raw:";
stringThree = stringTwo + analogValue;
Serial.println(stringOne);
drawText(5, 10, 2, stringThree);
display.display();
*/
digitalWrite(greenLED, HIGH); // turn on green led
digitalWrite(redLED, HIGH); // turn off red led
}
if ((flamelevel <= idleLow) && (flamelevel >= minSurvive) ) { // low fire
//lcd.print("Low fire ");
display.clearDisplay();
display.setCursor(5,10);
display.println(" Low Fire");
printOled();
/*
display.clearDisplay();
stringOne = " Low Fire ";
stringOne = stringOne + flamelevel;
stringTwo = stringOne + "% Raw:";
stringThree = stringTwo + analogValue;
Serial.println(stringOne);
drawText(5, 10, 2, stringThree);
display.display();
*/
digitalWrite(greenLED, LOW); // turn on green led
digitalWrite(redLED, HIGH); // turn off red led
// trigger stoker run timer = 2 mins?
}
if (flamelevel < minSurvive) { // fire out
//lcd.print("FIRE OUT! ");
display.clearDisplay();
display.setCursor(5,10);
display.println("FIRE OUT!!");
printOled();
/*
display.clearDisplay();
stringOne = "FIRE OUT!! ";
stringOne = stringOne + flamelevel;
stringTwo = stringOne + "% Raw:";
stringThree = stringTwo + analogValue;
Serial.println(stringOne);
drawText(5, 10, 2, stringThree);
display.display();
*/
digitalWrite(greenLED, LOW); // turn on green led
digitalWrite(redLED, HIGH); // turn off red redLED
delay(300);
digitalWrite(redLED, LOW); // turn off red led for flash
// send alert
}
/*lcd.setCursor(0, 1);
lcd.print(" Level: ");
lcd.print(flamelevel, 0);
lcd.print("% ");
*/
delay(200);
}
/*void drawText(int x, int y, int size, String text) {
display.setCursor(x, y);
display.setTextColor(2);
display.setTextSize(size);
display.print(text);
}
*/
void printOled() {
display.print(" ");
display.print(flamelevel);
display.println("%");
display.print(" Raw:");
display.print(analogValue);
display.display();
}