int Led1 = 12; //initialization pin 12 in arduino as LED1 Pin (red)
int Led2 = 11; //initialization pin 11 in arduino as LED2 Pin (yellow)
int Led3 = 10; //initialization pin 10 in arduino as LED3 Pin (green)
int LedLdr = 13; //initialization pin 13 in arduino as LED LDR Pin
int LdrPin = A0; //initialization pin A0 in arduino as LDR Pin
int PirPin = 2; //initialization pin 2 in arduino as PIR Pin
int PirStatus = 0; //starting value of PirStatus is 0 ; mean there is no movement
int LdrState = LOW; //starting value of LdrState is Low ; mean the Pir is off use for serial print
int BuzzPin = 9; //initialization pin 9 in arduino as Buzzer Pin
int PirState = LOW; //starting value of PirState is Low ; mean there is no movement used for serial print
void setup() {
Serial.begin(9600); //initializing serial begin of Arduino Uno
pinMode(Led1, OUTPUT); //set pin of Led1 as outuput
pinMode(Led2, OUTPUT); //set pin of Led2 as outuput
pinMode(Led3, OUTPUT); //set pin of Led3 as outuput
pinMode(LedLdr, OUTPUT); //set pin of LedLdr as outuput
pinMode(LdrPin, INPUT); //set pin of LdrPin as input
pinMode(PirPin, INPUT); //set pin of PirPin as input
}
void loop() {
int LdrStatus = analogRead(LdrPin); //LdrStatus reading analog signal from LdrPin
int PirStatus = digitalRead(PirPin); //PirStatus reading digital data from PirPin
if(LdrStatus >512){ //if value of LdrStatus more than 512 (bright)
digitalWrite(LedLdr,HIGH); //LedLdr will high and Led will on
if(PirStatus == HIGH){ //after check value of LdrStatus now check if PirStatus is high
digitalWrite(Led1, HIGH); //Led1 will high and red led will on
digitalWrite(Led2, HIGH); //Led2 will high and yellow led will on
digitalWrite(Led3, HIGH); //Led3 will high and green led will on
tone(BuzzPin, 1000); //BuzzPin will high and Buzzer will play sound at 1KHz
delay(200); //Led1, Led2, Led3, and BuzzPin will high during 200ms
digitalWrite(Led1, LOW); //Led1 will low and red led will off
digitalWrite(Led2, LOW); //Led2 will low and yellow led will off
digitalWrite(Led3, LOW); //Led3 will low and green led will off
noTone(BuzzPin); //BuzzPin will low and Buzzer will off
delay(200); //Led1, Led2, Led3, and BuzzPin will off during 200ms
if (PirState == LOW) { //if PirState is Low
Serial.println(" Hay ladrones, amigo.!!!!"); //Serial will print this
PirState = HIGH; //and change PirState into high so its only print 1 times
}
}
else{ //after check value of LdrStatus now check if PirStatus is not high
digitalWrite(Led1, LOW); //Led1 will low and red led will off
digitalWrite(Led2, LOW); //Led2 will low and yellow led will off
digitalWrite(Led3, LOW); //Led3 will low and green led will off
noTone(BuzzPin); //BuzzPin will low and Buzzer will off
if (PirState == HIGH) { //if PirState is High
Serial.println("El ladrón se ha escapado, jefe."); //Serial will print this
PirState = LOW; //and change PirState into Low so its only print 1 times
}
}
if(LdrState == LOW){ //if LdrState is Low
Serial.println("El sensor está encendido, amigo."); //Serial will print this
LdrState = HIGH; //and change LdrState into High so its only print 1 times
}
}
else{ //if value of LdrStatus no more than 512 (dark)
digitalWrite(LedLdr, LOW); //LedLdr will Low and Led will off
if(LdrState == HIGH){ //if LdrState is Low
Serial.println("Ya es de día, el sensor está muerto."); //Serial will print this
digitalWrite(Led1, LOW); //Led1 will low and red led will off
digitalWrite(Led2, LOW); //Led2 will low and yellow led will off
digitalWrite(Led3, LOW); //Led3 will low and green led will off
LdrState = LOW; //and change LdrState into High so its only print 1 times
}
}
}