#include <Servo.h>
#include <LiquidCrystal.h>
// Initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(13, 12, 6, 4, 3, 9);
//Servo slidingGateServo;
Servo windowServo;
Servo garageDoorServo;
const int pirPin = 2;
const int trigPin = 4;
const int echoPin = 5;
const int rainSensorPin = A0;
const int tempSensorPin = A1;
const int ldrPin = A2;
// Define motor pins connected to L298N
const int motorPin1 = A3; // IN1 on L298N for motor direction
const int motorPin2 = A4; // IN2 on L298N for motor direction
const int enablePin = A5; // ENA on L298N for motor speed
// Button to trigger gate
const int buttonPin = 6; // Pin connected to a button for opening/closing the gate
//const int livingRoomLed = 6;
//const int bedroomLed = 7;
const int gardenLed = 8;
const int redWarningLed = 7;
long duration;
int distance;
int tempValue;
int ldrValue;
int rainValue;
int pirState = LOW;
// Variables to track gate state
bool gateOpen = false;
void setup() {
// Servo setup
//slidingGateServo.attach(9); // Sliding Gate Servo on Pin 9
windowServo.attach(10); // Window Servo on Pin 10
garageDoorServo.attach(11); // Garage Door Servo on Pin 11
// Set up the LCD's number of columns and rows
lcd.begin(16, 2);
// Print a message to the LCD
//lcd.setCursor(0, 0);
//lcd.print("Temp Sensor Init");
// LED and sensor setup
pinMode(pirPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//pinMode(livingRoomLed, OUTPUT);
//pinMode(bedroomLed, OUTPUT);
pinMode(gardenLed, OUTPUT);
pinMode(redWarningLed, OUTPUT);
// Set up motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Ultrasonic Sensor for Sliding Gate
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
// Function to open the gate
/*void openGate() {
digitalWrite(motorPin1, HIGH); // Motor moves forward
digitalWrite(motorPin2, LOW);
analogWrite(enablePin, 255); // Full speed
Serial.println("Gate opening...");
}*/
// Function to close the gate
/*void closeGate() {
digitalWrite(motorPin1, LOW); // Motor moves backward
digitalWrite(motorPin2, HIGH);
analogWrite(enablePin, 255); // Full speed
Serial.println("Gate closing...");
}*/
// Function to stop the motor
/*void stopMotor() {
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
analogWrite(enablePin, 0);
Serial.println("Motor stopped");
}*/
/*if (distance < 50) {
//slidingGateServo.write(90); // Open gate when distance < 20cm
digitalWrite(motorPin1, HIGH); // Motor moves forward
digitalWrite(motorPin2, LOW);
analogWrite(enablePin, 255); // Full speed
//lcd.begin(16, 2);
//Serial.println("Gate opening...");
//lcd.print("Gate opening...");
delay(100);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
analogWrite(enablePin, 0);
} else {
//slidingGateServo.write(0); // Close gate
digitalWrite(motorPin1, LOW); // Motor moves backward
digitalWrite(motorPin2, HIGH);
analogWrite(enablePin, 255); // Full speed
//lcd.begin(16, 2);
//lcd.print("Gate closing...");
//Serial.println("Gate closing...");
delay(100);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
analogWrite(enablePin, 0);
}*/
if (distance < 20) {
garageDoorServo.write(90); // Open gate when distance < 20cm
} else {
garageDoorServo.write(0); // Close gate
}
// PIR Sensor for Security (Red LEDs)
pirState = digitalRead(pirPin);
if (pirState == HIGH) {
digitalWrite(redWarningLed, HIGH); // Turn on warning light
} else {
digitalWrite(redWarningLed, LOW); // Turn off warning light
}
// LDR for Garden Lights
ldrValue = analogRead(ldrPin);
if (ldrValue < 300) {
digitalWrite(gardenLed, HIGH); // Turn on garden light
} else {
digitalWrite(gardenLed, LOW); // Turn off garden light
}
// Reading temperature sensor value
int tempValue = analogRead(tempSensorPin);
//float voltage = tempValue * 5.0 / 1024;
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
float temperatureC = 1 / (log(1 / (1023. / tempValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
//float temperatureC = tempValue;
// Display the temperature on the LCD
//lcd.setCursor(0, 0); // Set cursor to the first row
lcd.begin(16, 2);
lcd.print("Temp: ");
lcd.print(temperatureC);
lcd.print("C");
// Raindrop Sensor for Window Control
rainValue = analogRead(rainSensorPin);
if (rainValue < 300) {
windowServo.write(90); // Close window if it rains
} else {
windowServo.write(0); // Keep window open otherwise
}
delay(1000);
}