#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 20, 4);
#define motion_pin 13 //name the pins
#define temp_pin 12
#define led_pin 14
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
int motion_detection;
int analogValue;
int new_action = 0;
float aver_temp = 0.0;
float a = 0.0;
int b = 0;
void printTemperature( float temp ) {
LCD.setCursor(0, 1);
LCD.println("Temperature:");
LCD.setCursor(13, 1);
LCD.println(temp);
}
//Define pins mode and initialize LCD display
void setup() {
Serial.begin(115200);
pinMode(motion_pin, INPUT);// declare motion sensor as input
pinMode(temp_pin, INPUT);// declare temp sensor as input
pinMode(led_pin, OUTPUT);// declare LED as output
LCD.init();
LCD.backlight();
}
void loop() {
motion_detection = digitalRead(motion_pin);
while ( motion_detection ){
analogValue = analogRead(temp_pin);
float celsius = 1 / (log(1 / (4095. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
if ( digitalRead(motion_pin) == 1 ){
LCD.setCursor(0, 0);
LCD.println("Motion Detcted ");
aver_temp += celsius;
a = a + 1.0;//number of temp
}
printTemperature(celsius);
Serial.print("Temperature is: "); Serial.println(celsius);
if (celsius < 10){
digitalWrite(led_pin, HIGH);
LCD.setCursor(0, 2);
LCD.println("LED On");
Serial.println("LED On");
}
if (celsius >= 10){
digitalWrite(led_pin, LOW);
LCD.setCursor(0, 2);
LCD.println("LED Off");
Serial.println("LED off");
}
if ( digitalRead(motion_pin) == 0 ){
LCD.setCursor(0, 0);
LCD.println("NO Motion Detcted");
LCD.setCursor(0, 3);
LCD.println("Ave temp:");
LCD.setCursor(10, 3);
LCD.println(aver_temp/a);
delay(2500);
b++;
}
delay(500);//0.5 seconds
if ( (b == 3) & (digitalRead(motion_pin)==0) ){
LCD.clear();
digitalWrite(led_pin, LOW);
break;
}
}
}