-
Notifications
You must be signed in to change notification settings - Fork 5
/
testpirsensor.pde
94 lines (83 loc) · 2.86 KB
/
testpirsensor.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
*
* Switches a LED according to the state of the sensors output pin.
* Determines the beginning and end of continuous motion sequences.
*
* @author: Brad Davis / brad.bdavis1 (_) gmail (_) com / http://braddavis.cc
* @date: 27, July, 2010
*
*
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
*
* Lots of link love should go to
* Kristian Gohlke (krigoo (_) gmail (_) com / http://krx.at) for creating the original
* project that some of this code was taken from.
* The Parallax PIR Sensor is an easy to use digital infrared motion sensor module.
* (http://www.parallax.com/detail.asp?product_id=555-28027)
*
* The sensor's output pin goes to HIGH if motion is present.
* This sets off an led and a buzzer. The arduino then waits a set cool down time before
* setting off the buzzer again.
*
*/
/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
int pirPin = 3; //the digital pin connected to the PIR sensor's output
int ledPin = 13; // shows the state of the sensor
int bellPin = 12; // represents the bell or buzzer that plays
// when someone walks by the sensor
long binglength = 2000; // how long the buzzer/bell will play
long lasttime = 0; // stores the time of the last sensor tripping
long cooldown = 20000; // time between buzzer plays
boolean cool = true;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(bellPin, OUTPUT);
digitalWrite(pirPin, LOW);
digitalWrite(bellPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
////////////////////////////
//LOOP
void loop(){
if(digitalRead(pirPin) == HIGH){ // sensor is tripped
digitalWrite(ledPin, HIGH);
Serial.println(lasttime/1000);
if(cool == true) {
lasttime = millis();
digitalWrite(bellPin, LOW);
cool = false;
}
else {
Serial.print("sensor is hot"); // tells us why the buzzer might not be going off
}
delay(1000);
}
if(millis() > lasttime + binglength){ // turns off buzzer/bell/led
digitalWrite(bellPin, HIGH);
}
if(millis() > lasttime + cooldown){ // resets cooldown
cool = true;
}
if(digitalRead(pirPin) == LOW){
digitalWrite(ledPin, LOW);
}
}