int Light = 5; // pin for LED
int PIRpower = 13; // power to PIR sensor
int PIR = 12; // input from PIR sensor
int PIRState = LOW; // status of PIR
int LDR = 2; // input from LDR
int LDRpower = 3; // power to LDR sensor
int LDRState = LOW; // status of LDR
void Night()
{
digitalWrite(PIRpower, HIGH); // power ON PIR sensor
delay(2000);
Serial.println("A. PIR sensor ON, detecting motion");
delay(3000); // wait
PIRState = digitalRead(PIR); // read PIR sensor
delay(500);
Serial.println("B. Motion sensor read, switching PIR OFF");
digitalWrite(PIRpower, LOW);
delay(500);
if (PIRState == HIGH){
Serial.println(">>>>>>>>>> MOTION DETECTED");
delay(500);
Serial.println("I. switching Light ON");
delay(500);
digitalWrite(Light, HIGH);
Serial.println("II. Light 100%");
delay(15000); //wait 15sec
analogWrite(Light, 175);
Serial.println("II. Light 70%");
delay(15000); //wait 15sec
analogWrite(Light, 100);
Serial.println("II. Light 40%");
delay(15000); //wait 15sec
analogWrite(Light, 50);
Serial.println("II. Light 20%");
delay(500);
Serial.println(".......... Wait 5 seconds..........");
delay(1000);
}else {
Serial.println(">>>>>>>>>> MOTION NOT DETECTED"); // We only want to print on the output change, not state
delay(500);
Serial.println(".......... Wait 10 seconds..........");
delay(10000); //wait 10sec
}
}
void setup() {
pinMode(Light, OUTPUT); // declare LED as output
pinMode(PIRpower, OUTPUT); // declare PIRpower as output
pinMode(LDRpower, OUTPUT); // declare LDRpower as output
pinMode(LDR, INPUT); // declare LDR sensor as input
pinMode(PIR, INPUT); // declare PIR sensor as input
digitalWrite(PIRpower, LOW);
digitalWrite(LDRpower, LOW);
Serial.begin(9600);
}
void loop(){
Serial.println(".......... START..........");
digitalWrite(LDRpower, HIGH);
Serial.println("1. LDRpower ON, reading LDR sensor");
delay(3000);
LDRState = digitalRead(LDR);
delay(500);
Serial.println("2. LDR status read, turning LDRpower OFF");
delay(500);
Serial.println("3. LDRpower OFF");
digitalWrite(LDRpower, LOW);
delay(500);
if (LDRState == HIGH){ // read status of LDR (Day or Night)
Serial.println(">>>>>>>>>> DARKNESS DETECTED");
delay(500);
Serial.println("Switch ON Light 20%");
analogWrite(Light, 50);
Night();
Serial.println(".......... Restarting..........");
} else{
Serial.println(">>>>>>>>>> BRIGHTNESS DETECTED");
delay(500);
digitalWrite(Light, LOW);
Serial.println(".......... Waiting 5mins..........");
delay(60000); //wait 1 minute
Serial.println(".......... Waiting 4mins..........");
delay(60000); //wait 1 minute
Serial.println(".......... Waiting 3mins..........");
delay(60000); //wait 1 minute
Serial.println(".......... Waiting 2mins..........");
delay(60000); //wait 1 minute
Serial.println(".......... Waiting 1mins..........");
delay(60000); //wait 1 minute
Serial.println(".......... Restarting..........");
}
}
Comments