Guest User

Digit Squad - March 2016

a guest
Feb 10th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /*
  2.      * Turn ON the LED if motion detected
  3.      * Turn OFF the LED if no motion detected
  4.      * Commented by Praneet Sah
  5.      * this code is famously used for showing PIR sensor's usage
  6.      */
  7.      
  8.     int ledPin = 13;                // This LED is embedded in your Arduino
  9.     int inputPin = 8;               // Choose the input pin (for PIR sensor)
  10.     int pirState = LOW;             // we start, assuming no motion detected
  11.     int val = 0;                    // variable for reading the pin status
  12.      
  13.     void setup() {
  14.       pinMode(ledPin, OUTPUT);      // declare LED as output
  15.       pinMode(inputPin, INPUT);     // declare sensor as input
  16.      
  17.       Serial.begin(9600);
  18.     }
  19.      
  20.     void loop(){
  21.       val = digitalRead(inputPin);  // read input value of the sensor
  22.       if (val == HIGH) {            // check if the sensor's input is HIGH
  23.         digitalWrite(ledPin, HIGH);  // then turn LED ON
  24.         if (pirState == LOW) {      // if first loop, then pirState will be LOW unless val changes to LOW
  25.          
  26.           Serial.println("Motion detected!"); //Tell user that motion detected!
  27.          
  28.           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
  29.         }
  30.       } else {
  31.         digitalWrite(ledPin, LOW); // val is LOW and thus the turn LED OFF
  32.         if (pirState == HIGH){      //
  33.          
  34.           Serial.println("Motion ended!"); // Tell user that NO motion detected!
  35.          
  36.           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
  37.         }
  38.       }
  39.     }
Add Comment
Please, Sign In to add comment