#include<Wire.h>
#include <LiquidCrystal_I2C.h>
#include <math.h>
const int trigpin = 3;
const int echopin = 4;
const int ntcpin = A0;
int pirsense;
int pirpin = 2;
int alert = 0;
int flashyiter = 5;
int templed = 9;
int buzzer = 8;
int systemled = 5;
int pirled = 6;
int ultrasonicled = 7;
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define SOUND_SPEED 0.034
#define hightempthres 40.00
#define lowtempthres 8.00
#define thresdistance 20.00
const float BETA = 3950;
String alertmsg[4] = {"HIGH Temp", "LOW Temp", "Motion","Obstacle"};
void setup() {
Wire.begin();
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
pinMode(pirpin, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(templed, OUTPUT);
pinMode(pirled, OUTPUT);
pinMode(ultrasonicled, OUTPUT);
pinMode(systemled, OUTPUT);
digitalWrite(systemled, HIGH);
delay(1000);
digitalWrite(systemled, LOW);
lcd.init();
lcd.backlight();
}
void loop() {
int analogValue = analogRead(ntcpin);
float celsius = 1 / (log(1 / (1023.0 / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
long duration = pulseIn(echopin, HIGH);
float distanceCm = duration * SOUND_SPEED / 2;
pirsense = digitalRead(pirpin);
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(celsius);
lcd.print("C ");
lcd.setCursor(9, 0);
lcd.print("D:");
lcd.print(distanceCm);
//lcd.print("cm");
lcd.setCursor(0, 1);
lcd.print("M:");
lcd.print(pirsense);
alert = 0;
if (celsius >= hightempthres) {
alert++;
}
if (celsius <= lowtempthres) {
alert++;
}
if (pirsense == 1) {
alert++;
}
if (distanceCm<=thresdistance) {
alert++;
}
if (alert > 0) {
for (int i = 0; i < flashyiter; i++) {
lcd.setCursor(4, 1);
if (celsius >= hightempthres) {
Lcdprojection(0,templed);
} else if (celsius <= lowtempthres) {
Lcdprojection(1,templed);
} else if (pirsense == 1) {
Lcdprojection(2,pirled);
} else if(distanceCm<=thresdistance){
Lcdprojection(3,ultrasonicled);
}
if (alert > 1) {
lcd.setCursor(4, 1);
lcd.print("Other Alert");
delay(500);
lcd.setCursor(4, 1);
lcd.print(" ");
}
}
}
delay(1000);
}
void beep(int pin) {
digitalWrite(buzzer, HIGH);
digitalWrite(pin, HIGH);
delay(500);
digitalWrite(buzzer, LOW);
digitalWrite(pin, LOW);
}
void Lcdprojection(int j,int pin){
lcd.print(alertmsg[j]);
beep(pin);
delay(500);
lcd.setCursor(4, 1);
lcd.print(" ");
}