#include <LiquidCrystal.h>
//System state
const char onMessage[] = "Alarm on";
const char offMessage[] = "Alarm off";
const char activeMessage[][16] = {"Gas Detected", "Please evacuate","Fire Detected"};
const int OFF=0, ON=1, ACTIVE=2;
int state = OFF;
//Pins
const int tempSensor = A2;
const int gasSensor = A3;
const int led = 5;
const int piezo = 3;
const int power = 2;
const int rs = 8, en = 9, d4=10, d5=11, d6=12, d7=13;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int temperature;
int gas;
char degree = 176; //ASCII value of Degree
unsigned long time;
void setup()
{
Serial.begin(9600); //
delay(3000); //
Serial.println("Start"); //
pinMode(led, OUTPUT);
pinMode(piezo, OUTPUT);
pinMode(A3,INPUT); //
pinMode(A2,INPUT); //
lcd.begin(16, 2);
setOff();
}
void loop()
{
int on = digitalRead(power);
int Gas_state = analogRead(A3); //
int tmp = analogRead(A0);//Reading data from the sensor.This voltage is stored as a 10bit number.
float voltage = (tmp * 5.0)/1024;//(5*temp)/1024 is to convert the 10 bit number to a voltage reading.
float milliVolt = voltage * 1000;//This is multiplied by 1000 to convert it to millivolt.
float tmpCel = (milliVolt-500)/10 ;//For TMP36 sensor. Range(−40°C to +125°C)
float tmpFer = (((tmpCel*9)/5)+32);//used to convert Celsius -> Fahrenheit
if(on && (state == OFF)){
setOn();
Serial.println("Gas Leaked at your premises");
}else if(!on && (state != OFF)){
setOff();
}
if(state == ON) {
readSensors();
if(gas > 60) {
setActive();
}else if(temperature > 100){
setActive1();
}
}else if(state == ACTIVE) {
int ledState = digitalRead(led);
digitalWrite(led, !ledState);
if(ledState == HIGH) {
delay(50);
}else {
delay(20);
}
}
time = millis();
Serial.print("Time: ");
Serial.println(time);
/*Serial.print("10bit number(0-1023): ");
Serial.println(tmp);
Serial.print("voltage: ");
Serial.print(voltage);
Serial.println("V");
Serial.print("millivolt: ");
Serial.print(milliVolt);
Serial.println("mV");*/
Serial.print("Celsius: ");
Serial.print(tmpCel);
Serial.println(degree);
Serial.print("Fahrenheit: ");
Serial.println(tmpFer);
Serial.println("");
delay(1000);
}
void setOff() {
state = OFF;
digitalWrite(led, LOW);
noTone(piezo);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print(offMessage);
}
void setOn() {
state = ON;
digitalWrite(led, HIGH);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print(onMessage);
}
void setActive(){
state = ACTIVE;
tone(piezo, 750);
lcd.clear();
lcd.print(activeMessage[0]);
lcd.setCursor(0, 1);
lcd.print(activeMessage[1]);
}
void setActive1(){
state = ACTIVE;
tone(piezo, 750);
lcd.clear();
lcd.print(activeMessage[2]);
lcd.setCursor(0, 1);
lcd.print(activeMessage[1]);
}
void readSensors(){
int t = analogRead(tempSensor);
temperature = map(t, 20, 358, -40, 125);
int q = analogRead(gasSensor);
gas = map(q, 157, 600, 0, 100);
}
Comments
Please log in or sign up to comment.