/* Project: PIR Motion Sensor, LEDs - motion detection system Function:we will read the output of the PIR sensor and if its high or if an object is detected it will switch on the red LED and sends the message to Serial monitor, green LED will be off. Green LED will be on and red LED will be off if nothing detected. */ //************************************************************* const int pirPin = 8; //PIR sensor attached to digital pin 8 const int redledPin = 7; //red LED attached to digital pin 7 const int greenledPin = 6; //green LED attached to digital pin 6 int state = LOW; // by default, no motion detected //************************************************************** void setup() { pinMode(pirPin, INPUT); //sets pirPin as INPUT pinMode(redledPin, OUTPUT); //sets redledPin as OUTPUT pinMode(greenledPin, OUTPUT); //sets greenledPin as OUTPUT Serial.begin(9600); //initialize serial communications at 9600 bps } void loop() { int sensorValue = digitalRead(pirPin);//reads the status of PIR sensor if (sensorValue == HIGH) {//checks if sensor active digitalWrite(redledPin, HIGH); //red LED is on digitalWrite(greenledPin, LOW);//green LED is off delay(100); //sets delay for 100 milliseconds if (state == LOW) { Serial.print ("Alarm!!! Motion detected");//prints message on Serial Monitor state = HIGH; //update variable state to HIGH } } else { digitalWrite(redledPin, LOW); //red LED is off digitalWrite(greenledPin, HIGH);//green LED is on delay(200); //sets delay for 200 milliseconds if (state == HIGH){ Serial.println("Motion stopped!"); state = LOW; //update variable state to LOW } } }