-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPIR_Motion_Sensor_Test.ino
More file actions
58 lines (50 loc) · 1.45 KB
/
PIR_Motion_Sensor_Test.ino
File metadata and controls
58 lines (50 loc) · 1.45 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
//PIR Motion Sensor Tester
int ledPin = 13; // choose the pin for the LED
int inputPin = 3; // choose the input pin (for PIR sensor)
int pirState = true; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int minimummSecsLowForInactive = 5000; // If the sensor reports low for
// more than this time, then assume no activity
long unsigned int timeLow;
boolean takeLowTime;
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
//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);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState) {
// we have just turned on
pirState = false;
Serial.println("Motion detected!");
// We only want to print on the output change, not state
delay(50);
}
takeLowTime = true;
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (takeLowTime){
timeLow = millis();
takeLowTime = false;
}
if(!pirState && millis() - timeLow > minimummSecsLowForInactive){
pirState = true;
Serial.println("Motion ended!");
delay(50);
}
}
}