/*
Arduino with PIR motion sensor
For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
Modified by Rui Santos based on PIR sensor by Limor Fried
*/
int led = 14; // the pin that the LED is attached to
int sensor = 12; // the pin that the sensor is attached to
int state = LOW; // by default, no motion detected
int prevState = LOW; // previous state of the sensor
int val = 0; // variable to store the sensor status (value)
void setup() {
pinMode(led, OUTPUT); // initialize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}
void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH && prevState == LOW) { // check if the sensor went from LOW to HIGH
digitalWrite(led, HIGH); // turn LED ON
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
if (val == LOW && prevState == HIGH) { // check if the sensor went from HIGH to LOW
digitalWrite(led, LOW); // turn LED OFF
Serial.println("No motion detected!");
state = LOW; // update variable state to LOW
}
prevState = val; // update previous state of the sensor
}