-
Notifications
You must be signed in to change notification settings - Fork 1
/
NeoPixelProximityWithClass.ino
113 lines (112 loc) · 3.68 KB
/
NeoPixelProximityWithClass.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
#include <Adafruit_CircuitPlayground.h>
#include <Proximity.h>
#if !defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS)
#error "Infrared support is only for the Circuit Playground Express, it doesn't work with other boards."
#endif
Proximity prox;
void setup() {
//Setup everything
CircuitPlayground.begin();
//Change this number for your sensitivity, make higher if you can't get NeoPixels to turn off, make lower if you can't get it to turn NeoPixels on.
prox.begin(80);
Serial.begin(19200);
}
void on() {
//Turn all NeoPixels on
CircuitPlayground.setPixelColor(0, 255, 255, 255);
CircuitPlayground.setPixelColor(1, 255, 255, 255);
CircuitPlayground.setPixelColor(2, 255, 255, 255);
CircuitPlayground.setPixelColor(3, 255, 255, 255);
CircuitPlayground.setPixelColor(4, 255, 255, 255);
CircuitPlayground.setPixelColor(5, 255, 255, 255);
CircuitPlayground.setPixelColor(6, 255, 255, 255);
CircuitPlayground.setPixelColor(7, 255, 255, 255);
CircuitPlayground.setPixelColor(8, 255, 255, 255);
CircuitPlayground.setPixelColor(9, 255, 255, 255);
}
void off() {
//Turn all NeoPixels off
CircuitPlayground.setPixelColor(0, 0, 0, 0);
CircuitPlayground.setPixelColor(1, 0, 0, 0);
CircuitPlayground.setPixelColor(2, 0, 0, 0);
CircuitPlayground.setPixelColor(3, 0, 0, 0);
CircuitPlayground.setPixelColor(4, 0, 0, 0);
CircuitPlayground.setPixelColor(5, 0, 0, 0);
CircuitPlayground.setPixelColor(6, 0, 0, 0);
CircuitPlayground.setPixelColor(7, 0, 0, 0);
CircuitPlayground.setPixelColor(8, 0, 0, 0);
CircuitPlayground.setPixelColor(9, 0, 0, 0);
}
void gray(int val) {
//Turn all NeoPixels to a percent of white
CircuitPlayground.setPixelColor(0, val, val, val);
CircuitPlayground.setPixelColor(1, val, val, val);
CircuitPlayground.setPixelColor(2, val, val, val);
CircuitPlayground.setPixelColor(3, val, val, val);
CircuitPlayground.setPixelColor(4, val, val, val);
CircuitPlayground.setPixelColor(5, val, val, val);
CircuitPlayground.setPixelColor(6, val, val, val);
CircuitPlayground.setPixelColor(7, val, val, val);
CircuitPlayground.setPixelColor(8, val, val, val);
CircuitPlayground.setPixelColor(9, val, val, val);
}
void mid() {
//Turn all NeoPixels to gray
gray(64);
}
bool prev;
bool isOn;
long lastActivation;
void loop() {
if (CircuitPlayground.slideSwitch()) {
//Run if mode is proximity
//Turn off LED to show proximity mode
digitalWrite(13, LOW);
//Check proximity
if (prox.close()) {
//Close to object
if (!prev) {
//Close to object but not when ran last loop
mid();
prev = true;
lastActivation = millis();
} else {
//Close to object in last loop and this loop
on();
prev = true;
lastActivation = millis();
}
} else {
//Not close to object
if (millis() - lastActivation < 10000 && millis() - lastActivation > 9700) {
//Been close to object in 9700-10000 milliseconds
mid();
prev = false;
} else if (millis() - lastActivation > 10000) {
//Haven't been close to object within 10 seconds
off();
prev = false;
} else {
//Fix bug for when you only activate the IR sense in 1 loop so the NeoPixels are at mid bright for the time
on();
}
}
} else {
//Manual mode, so show LED and push back lastActivation
lastActivation = -50000;
digitalWrite(13, HIGH);
if (CircuitPlayground.rightButton()) {
//On button is pressed
isOn = true;
} else if (CircuitPlayground.leftButton()) {
//Off button is pressed and on button isn't pressed
isOn = false;
}
//Manage NeoPixels
if (isOn) {
on();
} else {
off();
}
}
}