-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPIR-MotionDetector.ino
More file actions
112 lines (98 loc) · 3.04 KB
/
Copy pathPIR-MotionDetector.ino
File metadata and controls
112 lines (98 loc) · 3.04 KB
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
/*=====================================================================
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------
Purpose : Using a PIR module we detect motion. Debouncing is used in
such a way that current motion has to stop for a minimum
time interval before we record a new motion.
Date : 22 Feb 2016
Boards : ArduinoUno, LaunchPadF5529
=====================================================================
*/
// Comment/uncomment as suitable for your hardware
// #define LaunchPadF5529
#ifdef LaunchPadF5529
int pirPin = P2_0;
int ledPin = RED_LED;
#else
int pirPin = 3;
int ledPin = 13;
#endif
typedef enum {
NONE, // sensor is not ready for use
INIT, // sensor is being initialized
STILL, // sensor is detecting no motion
MOTION, // sensor is detecting motion
PAUSE // between motion and returning to stillness
} PIR_State;
int calibrationTime = 30;
long unsigned int motionPauseTs; // timestamp of start of motion
long unsigned int pauseInterval = 3000; // idle time in ms to end current motion event
PIR_State pirState = NONE;
void calibrate(int warmupTime)
{
Serial.print("Calibrating sensor ");
for(int i = 0; i < warmupTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
}
void setup()
{
Serial.begin(9600);
pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pirState = INIT;
calibrate(calibrationTime);
pirState = STILL;
delay(50);
}
void loop()
{
long unsigned int currTs = millis();
int pirValue = digitalRead(pirPin);
digitalWrite(ledPin, pirValue); // LED reflects the sensor's output
switch (pirState) {
case STILL:
if (pirValue == HIGH) {
pirState = MOTION;
Serial.print("Motion detected at time ");
Serial.println(currTs/1000);
}
break;
case MOTION:
if (pirValue == LOW) {
pirState = PAUSE;
motionPauseTs = currTs;
}
break;
case PAUSE:
if (pirValue == LOW) {
if (currTs - motionPauseTs > pauseInterval) {
Serial.print("Motion ended at time ");
Serial.println((currTs-pauseInterval)/1000);
Serial.println("");
pirState = STILL;
delay(50);
}
}
else {
pirState = MOTION;
}
break;
default:
pirState = STILL; // reset if something's gone wrong
break;
}
}