// definicja LCD Display
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
// koniec definicji
const float BETA = 3950;
int sensor1counter = 0;
int sensor2counter = 0;
const int relay = 10;
int suspendbuzzer = 0;
int suspendbutton = 0; // determine if a suspend button is pressed
// user defined parameters
const int sensor1_possitive_max = 5;
const int sensor2_possitive_max = 5;
const int time_to_turn_off_pump = -14;
const int alarm_suspend_time = 6;
void setup() {
// incjacja LCD Display
lcd.init();
//lcd.backlight();
//koniec inicjacji LCD
pinMode(11, INPUT_PULLUP );
pinMode(10, OUTPUT);
Serial.begin(9600);
digitalWrite(2, LOW); // sensor 1 ready
digitalWrite(3, LOW); // sensor 1 triggered
digitalWrite(relay, LOW); // relay
// display user parameters and wait 2 seconds before this shit will go on
Serial.println("Declared Parameters / Variables: ");
Serial.print("Sensor 1 threshold: "); Serial.println(sensor1_possitive_max);
Serial.print("Sensor 2 threshold: "); Serial.println(sensor2_possitive_max);
Serial.print("Alarm snooze [s]: "); Serial.println(alarm_suspend_time);
Serial.print("Time to shut down the pump [s]: "); Serial.println(time_to_turn_off_pump);
Serial.println("...monitoring will start in 2 secs...");
//powitalna wiadomosc
//lcd.print("System initiating...");
lcd.setCursor(0, 0);
lcd.println("CO: OK");
lcd.setCursor(0, 1);
lcd.println("Smoke: No");
delay (2000);
lcd.clear();
}
void loop() {
//first sensor reading and calculation
int sensor1_analogValue = analogRead(A2);
float sensor1_value = 1 / (log(1 / (1023. / sensor1_analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
// second senor reading and calculation
int sensor2_analogValue = analogRead(A3);
float sensor2_value = 1 / (log(1 / (1023. / sensor2_analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: "); Serial.print(sensor1_value); Serial.println(" ℃");
Serial.print("Temperature: "); Serial.print(sensor2_value); Serial.println(" ℃");
Serial.print("Sensor 1 counter: "); Serial.println(sensor1counter);
Serial.print("Sensor 2 counter: "); Serial.println(sensor2counter);
Serial.print("suspendbutton: "); Serial.println(suspendbutton);
Serial.print("suspendbuzzer: "); Serial.println(suspendbuzzer);
Serial.println("----------");
// sensor 1 triggered
if(sensor1_value>50){
sensor1counter = sensor1counter+1 ;
}
else {
if (sensor1counter == 0) {}
else{
sensor1counter = sensor1counter-1;
}
}
if (sensor1counter >= sensor1_possitive_max) {
digitalWrite(3, HIGH);
if (suspendbuzzer <=0) {tone(12, 262, 125);}
suspendbutton = digitalRead(11);
if (suspendbutton == HIGH){
suspendbuzzer = alarm_suspend_time;}
//LCD Notification
// lcd.clear();
//lcd.setCursor(0, 0);
//lcd.print("S1 Alarm Suspended");
// lcd.setCursor(0, 1);
// lcd.print(suspendbuzzer);
// lcd.print (" sec left");
suspendbuzzer = suspendbuzzer-1;
if (suspendbuzzer>0) {digitalWrite(3, LOW);}
}
// sensor 2 triggered
if(sensor2_value>50){
sensor2counter = sensor2counter+1 ;
}
else {
if (sensor2counter == 0) {}
else{
sensor2counter = sensor2counter-1;
}
}
if (sensor2counter >= sensor2_possitive_max) {
digitalWrite(4, HIGH);
if (suspendbuzzer <=0) {tone(12, 262, 125);}
suspendbutton = digitalRead(11);
if (suspendbutton == HIGH){
suspendbuzzer = alarm_suspend_time;}
suspendbuzzer = suspendbuzzer-1;
if (suspendbuzzer>0) {digitalWrite(4, LOW);}}
// display sensor status on LCD
if (sensor1counter >= sensor1_possitive_max) {
//lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.println("CO: HIGH");
} else {
lcd.noBacklight();
lcd.setCursor(0, 0);
lcd.println("CO: OK");
}
if (sensor2counter >= sensor2_possitive_max) {
//lcd.clear();
lcd.backlight();
lcd.setCursor(0, 1);
lcd.println("SMOKE: HIGH");
} else {
lcd.noBacklight();
lcd.setCursor(0, 1);
lcd.println("SMOKE: No");
}
// prepare pump or relay to stop
if (suspendbuzzer < time_to_turn_off_pump) {
digitalWrite(relay, HIGH);
Serial.println("the Fuel Pump Has Been Stopped");
lcd.clear();
lcd.print("Pump shut-off");
delay (100000);
}
delay (1000);
}