//Libraries
#include <DHT.h>;
#include<Servo.h>
//Constants
#define DHTPIN 2 //DHT PIN 2
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
#define LED 3
#define TRIG 4 // Ultrasound Trig Sensor connected to PIN 4
#define ECHO 6 // Ultrasound Trig Sensor connected to PIN 6
#define buzzer 5
#define LDR A3 //LDR input to PIN A3
Servo Myservo; //Servo motor connected to PIN 7 as output
//Variables
//int sound_value; //stores Ultrasonic sound sensor value
float hum; //Stores humidity value
float temp; //Stores temperature value
void setup()
{
Serial.begin(9600);
dht.begin();
pinMode(LED,OUTPUT); //output for LED
pinMode(TRIG, OUTPUT); // Trig output
pinMode(ECHO, INPUT); //input from echo
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
pinMode(LDR, INPUT); //input from LDR
Myservo.attach(7);
}
int pos=0;
int initial=0;
// These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 50;
void loop()
{
//initializing the position of servo motor to 0 (opened)
if(initial==0){
Myservo.write(pos);
initial=1;
}
//DHT22 SENSOR MODULE STARTS
//Read data and store it to variables hum and temp
hum = dht.readHumidity();
temp= dht.readTemperature();
//Print temp and humidity values to serial monitor
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" % Temperature: ");
Serial.print(temp);
Serial.print(" `C");
if(temp>40 || hum>60)
digitalWrite(LED,HIGH);
else
digitalWrite(LED,LOW);
//ULTRASONIC SOUND SENSOR MODULE STARTS
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
int duration=pulseIn(ECHO,HIGH);
int distance=duration/58; //distance value is in cm
Serial.print(" Distance: ");
Serial.print(distance);
Serial.print(" cm ");
if (distance < 35 ) { // check if the distance is less than 35cm
Serial.println();
Serial.println("Stranger is near the baby!!");
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(1500); // ...for 1.5 sec
noTone(buzzer); // Stop sound...
delay(1000); // ...for 1sec delay(500); // delay 100 milliseconds
}
//LDR (PHOTORESISTOR) SENSOR MODULE STARTS
int analogValue = analogRead(LDR);
// Convert the analog value into lux value
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
//if lux = 400 implies office lighting, so kept threshold as 600 as bright
Serial.print("Lux: ");
Serial.println(lux);
if (lux >= 600) {
if(pos==180)
Serial.println("Its still bright, cradle remains covered!!");
else{
Serial.println("Its too Bright, covering the cradle using servo motor..");
for(pos=0;pos<180;pos++){
Myservo.write(pos);
delay(15);
}
}
}
else{
if(pos==0)
Serial.println("Its still dark, Cradle remains opened!!");
else{
Serial.println("Its Dark, Opening the cradle using servo motor..");
for(pos=179;pos>0;pos--){
Myservo.write(pos);
delay(15);
}
}
}
delay(2000); //Delay 2 sec.
}