#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
// set the LCD address to 0x27 for a 16 chars and 2 line display
// Initialization
#define ECHO_PIN 2
#define TRIG_PIN 3
int inputPin = 1; // Input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
const int redLed = 12;
const int greenLed = 10;
const int interrupt = 13;
const int buzzerPin = 11;
const int loudness = 1000; // loudness of the buzzer
const int timer = 100; // delay ( 1s )
const int min_distance = 100; // minimum security distance ( 1 m )
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
const float min_temp = 20; // minimum temperature to support ( °C )
const float max_temp = 40; // maximum temperature to support ( °C )
void setup() {
Serial.begin(115200);
pinMode(interrupt, INPUT_PULLUP);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.print(" --> Welcome <-- ");
delay(2000);
lcd.clear();
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
char print_on_lcd(float distance , float celsius, char* message="info: RAS.."){
lcd.setCursor(1,0);
lcd.print("Distance");
lcd.setCursor(14,0);
lcd.print("Temp");
lcd.setCursor(2,1);
lcd.print(distance, 1);
lcd.setCursor(14,1);
lcd.print(celsius, 1);
lcd.setCursor(1,3);
lcd.print(message);
delay(1000);
lcd.clear();
return;
}
bool checkTemp(float celsius, float distance) {
bool temp_OutOfRange = false;
if (celsius > max_temp || celsius < min_temp) {
// turn on RED Led and buzzer
tone(buzzerPin, loudness, timer);
digitalWrite(redLed, HIGH);
print_on_lcd(distance, celsius, "info: abnorm Temp!");
temp_OutOfRange = true;
}
return temp_OutOfRange;
}
void loop() {
// Read interrupt state
int interruptState = digitalRead(interrupt);
// Get the distance
float distance = readDistanceCM();
// If distance < 1m ring the buzzer
bool isNearby = distance < min_distance;
// Read Motion sensor state
int motionState = digitalRead(inputPin);
// Read analogValue from Temperature sensor
int analogValue = analogRead(A0);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
if (interruptState == HIGH && isNearby == false && motionState == LOW && checkTemp(celsius, distance) == false){
print_on_lcd(distance, celsius);
checkTemp(celsius, distance);
}
// If interrupt is ON and Distance < min_distance
if (interruptState == HIGH && isNearby == true) {
if (motionState == HIGH && pirState == LOW){
pirState = HIGH;
// turn on GREEN led
digitalWrite(greenLed, HIGH);
print_on_lcd(distance, celsius, "info: Mov detect!");
}
// turn on RED Led and buzzer
tone(buzzerPin, loudness, timer);
digitalWrite(redLed, HIGH);
print_on_lcd(distance, celsius, "info: Too close!");
checkTemp(celsius, distance);
}
// If interrupt is ON and Distance > min_distance and Motion detected
if (interruptState == HIGH && isNearby == false && motionState == HIGH){
if (pirState == LOW) {
pirState = HIGH;
}
// turn on Green LED
digitalWrite(greenLed, HIGH);
print_on_lcd(distance, celsius, "info: Mov detect!");
checkTemp(celsius, distance);
} else { // If interrupt is OFF = Nothing happens
if (pirState == HIGH) {
pirState = LOW;
}
// turn off LEDs and buzzer
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
noTone(buzzerPin);
lcd.clear();
}
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
ntc1:GND
ntc1:VCC
ntc1:OUT
pir1:VCC
pir1:OUT
pir1:GND
led4:A
led4:C
bz1:1
bz1:2
sw1:1
sw1:2
sw1:3
lcd2:GND
lcd2:VCC
lcd2:SDA
lcd2:SCL
r1:1
r1:2
led1:A
led1:C
r2:1
r2:2