Advertisement
grist

Pizza Boy Detector Sketch (AtTiny version)

Jan 19th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. /* PIR Doorbell & Webcam by grist, Jan 2012
  2.  
  3.  Uses the PIR activated switch from an AEI PIR-9038W to turn on a webcam and activate my doorbell.
  4.  Attiny version.
  5.  
  6.  Compiles to less than 1k, so could maybe run on an Attiny45. I used an Attiny85.
  7. */
  8.  
  9. // Pins
  10. const byte pir_in = 2;
  11. const byte db_pin = 3;
  12. const byte webcam_pin = 4;
  13. const byte led_pin = 1; // indicator that webcam is on
  14.  
  15. // Globals
  16. int ttl = 30000; // minimum number of milliseconds to keep the camera active.
  17. unsigned long end_time; // when to stop capturing. This is reset by any movement during the capture
  18.  
  19. void setup() {
  20.   pinMode(pir_in, INPUT);
  21.   pinMode(db_pin, OUTPUT);
  22.   pinMode(webcam_pin, OUTPUT);
  23.   pinMode(led_pin, OUTPUT);
  24. }
  25.  
  26. void loop() {
  27.   digitalWrite(webcam_pin, LOW);
  28.   digitalWrite(led_pin, LOW);
  29.   digitalWrite(db_pin, LOW);
  30.   // Low means either the sensor has tripped or someone has pulled the wires out.
  31.   // Either way we want to start capturing
  32.   if (digitalRead(pir_in) == LOW) {
  33.     end_time = millis() + ttl; // doesn't allow for millis rollover
  34.     digitalWrite(webcam_pin, HIGH); // start the webcam
  35.     digitalWrite(led_pin, HIGH);
  36.     digitalWrite(db_pin, HIGH); // trigger the doorbell
  37.     delay(200);
  38.     digitalWrite(db_pin, LOW);
  39.     // loop until there hasn't been any movement detected for 30 seconds
  40.     while (millis() < end_time) {
  41.         // If the pin is still LOW push the end time out
  42.         if (digitalRead(pir_in) == LOW) {
  43.           end_time = millis() + ttl; // doesn't allow for millis rollover
  44.         }
  45.     }
  46.     // Fallen out of the loop so there's been ${ttl} milliseconds since movement was detected
  47.   }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement