#include <Servo.h>
int sen1Value = 0;
int sen2Value = 0;
int const LDR = A0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
Servo servo_7;
void setup()
{
Serial.begin(9600); //initialize serial communication
pinMode(LDR, INPUT); //LDR
pinMode(13, OUTPUT); //connected to led
servo_7.attach(4, 500, 2500); //servo motor
pinMode(5, OUTPUT); //signal to piezo buzzer
pinMode(6, INPUT); //signal to PIR
pinMode(12, OUTPUT); //Red LED
pinMode(11, OUTPUT); //Green LED
}
void loop()
{
//------light intensity control------//
//--------------------------------------------------------------
int val1 = analogRead(LDR);
sen2Value = digitalRead(6);
if ((val1 < 500) || (sen2Value == LOW))
{
digitalWrite(13, LOW);
Serial.print("Bulb OFF = ");
Serial.print(val1);
if (sen2Value == LOW) {
digitalWrite(12, HIGH); // Red LED ON,indicating no motion
digitalWrite(11, LOW);
Serial.print(" || NO Motion Detected " );
}
if (sen2Value == HIGH) {
digitalWrite(12, LOW); // RED LED OFF
digitalWrite(11, HIGH);
Serial.print(" || Motion Detected! " );
}
}
else if ((val1 > 500) && (sen2Value == HIGH))
{
digitalWrite(13, HIGH);
Serial.print("Bulb ON = ");
Serial.print(val1);
digitalWrite(12, LOW); // RED LED OFF
digitalWrite(11, HIGH);
Serial.print(" || Motion Detected! " );
}
/*
--------------------------------------------------------------
------- servo motor ---------
-----------------------------------------------------------
*/
sen1Value = 0.01723 * readUltrasonicDistance(2, 3);
if (sen1Value < 100)
{
servo_7.write(90);
tone(5, 650);
Serial.print(" || Door Open! ; Distance = ");
Serial.print(sen1Value);
Serial.print("\n");
}
else
{
servo_7.write(0);
noTone(5);
Serial.print(" || Door Closed! ; Distance = ");
Serial.print(sen1Value);
Serial.print("\n");
}
delay(10); // Delay a little bit to improve simulation performance
}