Skip to content

Commit 3a383f0

Browse files
authored
Merge pull request #987 from adafruit/pir-eye
Add user_pir.cpp
2 parents b27df91 + 167eefe commit 3a383f0

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

M4_Eyes/user_pir.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#if 0 // Change to 1 to enable this code (must enable ONE user*.cpp only!)
2+
3+
// PIR motion sensor on pin 3 makes eyes open when motion is detected.
4+
5+
#include "globals.h"
6+
7+
#define PIR_PIN 3 // PIR sensor on D3 connector (between MIC & BAT)
8+
9+
static uint8_t priorState;
10+
11+
void user_setup(void) {
12+
pinMode(PIR_PIN, INPUT);
13+
priorState = digitalRead(PIR_PIN);
14+
for(uint8_t e=0; e<NUM_EYES; e++) {
15+
// Init to DEBLINK with an impossibly large duration to hold eyes shut.
16+
eye[e].blink.state = DEBLINK;
17+
eye[e].blink.duration = 0x7FFFFFFF;
18+
eye[e].blink.startTime = micros();
19+
}
20+
}
21+
22+
void user_loop(void) {
23+
uint8_t e, newState = digitalRead(PIR_PIN);
24+
if(newState != priorState) {
25+
if(newState) {
26+
// Initial motion sensed
27+
for(e=0; e<NUM_EYES; e++) { // For each eye...
28+
eye[e].blink.state = DEBLINK; // Opening...
29+
eye[e].blink.duration = 500000; // Slowly, about 1/2 sec
30+
eye[e].blink.startTime = micros(); // Starting now
31+
}
32+
} else {
33+
// PIR timeout; "end" of motion
34+
for(e=0; e<NUM_EYES; e++) { // For each eye...
35+
if(eye[e].blink.state != ENBLINK) { // If not already blinking...
36+
eye[e].blink.state = ENBLINK; // Start closing...
37+
eye[e].blink.duration = 1500000; // Even slower, about 1.5 sec
38+
eye[e].blink.startTime = micros(); // Starting now
39+
}
40+
}
41+
}
42+
priorState = newState;
43+
} else if(!newState) {
44+
// No change in state. If currently no motion active,
45+
// force eyes into closed position at every opportunity.
46+
for(e=0; e<NUM_EYES; e++) { // For each eye...
47+
if(eye[e].blink.state != ENBLINK) { // If not currently blinking...
48+
// Set to DEBLINK with an impossibly large duration -- eyes will
49+
// hold shut until motion event above resets start & duration.
50+
eye[e].blink.state = DEBLINK;
51+
eye[e].blink.duration = 0x7FFFFFFF;
52+
eye[e].blink.startTime = micros();
53+
}
54+
}
55+
}
56+
}
57+
58+
#endif // 0

0 commit comments

Comments
 (0)