-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathi_PIRsensor.ino
117 lines (107 loc) · 3.38 KB
/
i_PIRsensor.ino
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
*
* Switches video recording according to the state of the sensors output pin.
* Determines the beginning and end of continuous motion sequences.
*
* Original code is located at
* http://playground.arduino.cc/Code/PIRsense
* @author: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at
* @date: 3. September 2006
*
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
*
* Modified by orangkucing for MewPro (c) 2014
*
* The Parallax PIR Sensor is an easy to use digital infrared motion sensor module.
* ( http://www.parallax.com/product/555-28027 )
*
* The sensor's output pin goes to HIGH if motion is present.
* However, even if motion is present it goes to LOW from time to time,
* which might give the impression no motion is present.
* This program deals with this issue by ignoring LOW-phases shorter than a given time,
* assuming continuous motion is present during these phases.
*
*/
#ifdef USE_PIR_SENSOR
void setupPIRSensor()
{
pinMode(PIR_PIN, INPUT);
__debug(F("PIR sensor calibration start (10 seconds)"));
}
void checkPIRSensor()
{
//the time when the sensor outputs a low impulse
static unsigned long lowIn;
//the amount of seconds the sensor has to be low
//before we assume all motion has stopped
const unsigned long pause = 5;
static boolean lockLow = true;
static boolean takeLowTime;
if (millis() < 10000) { // Still calibrating...
return;
}
if (digitalRead(PIR_PIN) == HIGH) {
if (lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
__debug(F("---"));
__debug(F("motion detected at "));
#ifdef USE_TIME_ALARMS
if (debug) {
time_t t = now();
char s[20];
sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d", year(t), month(t), day(t), hour(t), minute(t), second(t));
Serial_println(s);
}
#else
if (debug) {
Serial_print(millis() / 1000);
}
__debug(F(" sec"));
#endif
startRecording();
}
takeLowTime = true;
}
if (digitalRead(PIR_PIN) == LOW) {
if (takeLowTime) {
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if (!lockLow && millis() - lowIn > pause * 1000) {
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
__debug(F("motion finished at ")); //output
#ifdef USE_TIME_ALARMS
if (debug) {
time_t t = now() - pause;
char s[20];
sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d", year(t), month(t), day(t), hour(t), minute(t), second(t));
Serial_println(s);
}
#else
if (debug) {
Serial_print(millis() / 1000 - pause);
}
__debug(F(" sec"));
#endif
stopRecording();
}
}
}
#else
void setupPIRSensor()
{
}
void checkPIRSensor()
{
}
#endif