0
|
1 void setup() {
|
|
2
|
|
3 pinMode(2, INPUT);
|
|
4 digitalWrite(2, LOW);
|
|
5 // PIR sensor on here is a
|
|
6 // http://octopart.com/555-28027-parallax-708653 in a
|
|
7 // http://octopart.com/1551ggy-hammond-15686 box
|
|
8
|
|
9 // the phototransistor on analog2 is jameco 2006414
|
|
10
|
|
11 Serial.begin(115200);
|
|
12 }
|
|
13
|
|
14 int newBlinks = 0;
|
|
15 int lastLevel = 0;
|
|
16 int threshold = 750;
|
|
17 int hold = 3; // pulse must last this many loops. Guessing-- I don't know the loop rate or the pulse width
|
|
18 int seenFor = 0;
|
|
19
|
|
20 void loop()
|
|
21 {
|
|
22 unsigned char head, cmd, arg;
|
|
23 int level = analogRead(3) < threshold;
|
|
24
|
|
25 if (level) {
|
|
26 seenFor++;
|
|
27 if (seenFor == hold) {
|
|
28 newBlinks++;
|
|
29 }
|
|
30 } else {
|
|
31 seenFor = 0;
|
|
32 }
|
|
33
|
|
34 if (Serial.available() >= 3) {
|
|
35 head = Serial.read();
|
|
36 if (head != 0x60) {
|
|
37 Serial.flush();
|
|
38 return;
|
|
39 }
|
|
40 cmd = Serial.read();
|
|
41 arg = Serial.read();
|
|
42 Serial.flush();
|
|
43 if (cmd == 0x00) {
|
|
44 Serial.print("{\"ok\":true}\n");
|
|
45 } else if (cmd == 0x01) { // poll
|
|
46 Serial.print("{\"newBlinks\":");
|
|
47 Serial.print(newBlinks);
|
|
48 Serial.print(", \"motion\":");
|
|
49 Serial.print(digitalRead(2) ? "true" : "false");
|
|
50 Serial.print("}\n");
|
|
51 newBlinks = 0;
|
|
52 } else if (cmd == 0x02) {
|
|
53 // current level
|
|
54 Serial.print("{\"z\":");
|
|
55 Serial.print(analogRead(3));
|
|
56 Serial.print("}\n");
|
|
57 } else if (cmd == 0x03) {
|
|
58 if (arg != 0) {
|
|
59 threshold = arg << 2;
|
|
60 }
|
|
61 Serial.print("{\"threshold\":");
|
|
62 Serial.print(threshold);
|
|
63 Serial.print("}\n");
|
|
64 }
|
|
65 }
|
|
66 }
|
|
67
|
|
68
|