#include <LiquidCrystal_I2C.h>
int PIRLED = 13;
int PIRPIN = 4;
int PIRSTATE = LOW;
int REDPIN = 5;
int GREENPIN = 6;
int BLUEPIN = 7;
int ECHOPIN = 2;
int TRIGPIN = 3;
int DISTLED = 12;
int val = 0;
const float BETA = 3950; // Beta Coefficient of the thermistor
LiquidCrystal_I2C LCD(0x27, 30, 4);
void resetLCD() {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Sita's House");
}
void setTempColor(int red, int green, int blue) {
analogWrite(REDPIN, red);
analogWrite(GREENPIN, green);
analogWrite(BLUEPIN, blue);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
LCD.init(); // initialize the lcd
LCD.backlight();
LCD.print("Sita's House");
pinMode(PIRLED, OUTPUT); // declare LED as output;
pinMode(PIRPIN, INPUT); // declare sensor as input
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
pinMode(TRIGPIN, OUTPUT);
pinMode(ECHOPIN, INPUT);
pinMode(DISTLED, OUTPUT);
}
void loop() {
// Temperature
int analogValue = analogRead(A0); // Read temperature sensor value
float celcius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15; // Convert to celcius
if (celcius >= 35.0) {
setTempColor(255, 0, 0);
}
else if (celcius > 10.0 && celcius < 35.0) {
setTempColor(0, 255, 0);
}
else if (celcius <= 10.0) {
setTempColor(0, 0, 255);
}
LCD.setCursor(0, 1);
LCD.print("Temp: ");
LCD.print(celcius);
LCD.print(" C ");
// Motion Detector
val = digitalRead(PIRPIN);
if (val == HIGH) {
digitalWrite(PIRLED, HIGH);
if (PIRSTATE == LOW) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Motion detected!");
PIRSTATE = HIGH;
}
}
else {
digitalWrite(PIRLED, LOW);
if (PIRSTATE == HIGH) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Motion ended!");
delay(3000);
resetLCD();
PIRSTATE = LOW;
}
}
// Distance Sensor
// HC-SR04 Ultrasonic Distance Sensor
long duration;
int distance;
// Send a pulse on the TRIGPIN
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
// Read the echo pin, duration is the time (in microseconds) for the echo to be received
duration = pulseIn(ECHOPIN, HIGH);
// Calculate the distance in centimeters
distance = duration * 0.034 / 2;
// Display distance on the LCD
LCD.setCursor(0, 2);
LCD.print("Distance: ");
LCD.print(distance);
LCD.print(" cm ");
// Control the DISTLED based on the distance
if (distance < 10) {
digitalWrite(DISTLED, HIGH);
} else {
digitalWrite(DISTLED, LOW);
}
delay(500); // Wait for a short time before the next loop
}