Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Turn ON the LED if motion detected
- * Turn OFF the LED if no motion detected
- * Commented by Praneet Sah
- * this code is famously used for showing PIR sensor's usage
- */
- int ledPin = 13; // This LED is embedded in your Arduino
- int inputPin = 8; // Choose the input pin (for PIR sensor)
- int pirState = LOW; // we start, assuming no motion detected
- int val = 0; // variable for reading the pin status
- void setup() {
- pinMode(ledPin, OUTPUT); // declare LED as output
- pinMode(inputPin, INPUT); // declare sensor as input
- Serial.begin(9600);
- }
- void loop(){
- val = digitalRead(inputPin); // read input value of the sensor
- if (val == HIGH) { // check if the sensor's input is HIGH
- digitalWrite(ledPin, HIGH); // then turn LED ON
- if (pirState == LOW) { // if first loop, then pirState will be LOW unless val changes to LOW
- Serial.println("Motion detected!"); //Tell user that motion detected!
- pirState = HIGH; //Setting pirState to HIGH so that so that if 'val' remains HIGH then we won't spam the user with the previous line
- }
- } else {
- digitalWrite(ledPin, LOW); // val is LOW and thus the turn LED OFF
- if (pirState == HIGH){ //
- Serial.println("Motion ended!"); // Tell user that NO motion detected!
- pirState = LOW; // Setting pirState to LOW so that so that if 'val' remains LOW then we won't spam the user with the previous line
- }
- }
- }
Add Comment
Please, Sign In to add comment