Advertisement
baldengineer

Updated PIR code

Apr 4th, 2016
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. //the time we give the sensor to calibrate (10-60 secs according to the datasheet)
  2. int calibrationTime = 30;
  3.  
  4. //the time when the sensor outputs a low impulse
  5. unsigned long   lowIn; // this the correct type
  6.  
  7. //the amount of milliseconds the sensor has to be low
  8. //before we assume all motion has stopped
  9. const unsigned long   pause = 8000; // corrected type
  10.  
  11. boolean lockLow = true;
  12. boolean takeLowTime;
  13.  
  14. const int pirPin = 3;    //the digital pin connected to the PIR sensor's output
  15. const int ledPin = 13;
  16.  
  17.  
  18. /////////////////////////////
  19. //SETUP
  20. void setup() {
  21.   Serial.begin(9600);
  22.   pinMode(pirPin, INPUT);
  23.   pinMode(ledPin, OUTPUT);
  24.   digitalWrite(pirPin, LOW);  // why?
  25.  
  26.   //give the sensor some time to calibrate
  27.   Serial.print(F("calibrating sensor "));
  28.   for (int i = 0; i < calibrationTime; i++) {
  29.     Serial.print(F("."));
  30.     // delay(000); // why are you including these?
  31.   }
  32.   Serial.println(F(" done"));
  33.   Serial.println(F("SENSOR ACTIVE"));
  34.   // delay(0); // why?
  35. }
  36.  
  37. ////////////////////////////
  38. //LOOP
  39. void loop() {
  40.   unsigned long currentTime = millis();
  41.   boolean pirState = digitalRead(pirPin);
  42.  
  43.   // read the PIR once, set the LED state once.
  44.   digitalWrite(ledPin, !pirState); //the led visualizes the sensors output pin state
  45.  
  46.   if (pirState) {
  47.     if (lockLow) {
  48.       //makes sure we wait for a transition to LOW before any further output is made:
  49.       lockLow = false;
  50.       Serial.println(F("---"));
  51.       Serial.print(F("motion detected at "));
  52.       Serial.print(currentTime / 1000);
  53.       Serial.println(F(" sec"));
  54.       //delay(0000); // why bother with this?
  55.     }
  56.     takeLowTime = true;
  57.   }
  58.  
  59.   if (!pirState) {
  60.     if (takeLowTime) {
  61.       lowIn = currentTime;        //save the time of the transition from high to LOW
  62.       takeLowTime = false;       //make sure this is only done at the start of a LOW phase
  63.     }
  64.    
  65.     //if the sensor is low for more than the given pause,
  66.     //we assume that no more motion is going to happen
  67.     if (!lockLow && (currentTime - lowIn > pause)) {
  68.       //makes sure this block of code is only executed again after
  69.       //a new motion sequence has been detected
  70.       lockLow = true;
  71.       Serial.print(F("motion ended at "));      //output
  72.       Serial.print((currentTime - pause) / 1000);
  73.       Serial.println(F(" sec"));
  74.       // delay(0); //again, why?
  75.     }
  76.   }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement