Advertisement
Xylitol

PIR sensor tester

Mar 9th, 2014
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. /*
  2.  * PIR sensor tester
  3.  */
  4.  
  5. int ledPin = 13;                // choose the pin for the LED
  6. int inputPin = 2;               // choose the input pin (for PIR sensor)
  7. int pirState = LOW;             // we start, assuming no motion detected
  8. int val = 0;                    // variable for reading the pin status
  9.  
  10. void setup() {
  11.   pinMode(ledPin, OUTPUT);      // declare LED as output
  12.   pinMode(inputPin, INPUT);     // declare sensor as input
  13.  
  14.   Serial.begin(9600);
  15. }
  16.  
  17. void loop(){
  18.   val = digitalRead(inputPin);  // read input value
  19.   if (val == HIGH) {            // check if the input is HIGH
  20.     digitalWrite(ledPin, HIGH);  // turn LED ON
  21.     if (pirState == LOW) {
  22.       // we have just turned on
  23.       Serial.println("Motion detected!!");
  24.       delay(10000);
  25.       // We only want to print on the output change, not state
  26.       pirState = LOW;
  27.     }
  28.   } else {
  29.     digitalWrite(ledPin, LOW); // turn LED OFF
  30.     if (pirState == HIGH){
  31.       // we have just turned of
  32.       Serial.println("Motion ended!");
  33.       // We only want to print on the output change, not state
  34.       pirState = LOW;
  35.     }
  36.   }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement