-
Notifications
You must be signed in to change notification settings - Fork 0
/
desk-mood-lighting.ino
227 lines (192 loc) · 6.42 KB
/
desk-mood-lighting.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// This #include statement was automatically added by the Particle IDE.
#include "Sunrise/Sunrise.h"
// This #include statement was automatically added by the Particle IDE.
#include "neopixel/neopixel.h"
SYSTEM_MODE(AUTOMATIC);
// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define LEFT_LED 31
#define BACK_LED 100
#define RIGHT_LED 31
#define MOTION_PIN A3
#define PIXEL_PIN D6
#define PIXEL_COUNT 162
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
uint32_t cOffset = 0;
bool vMotion = 0;
bool vMotionPrevious = 0;
uint32_t lastMotion = 0;
uint8_t sunriseHour = 0;
uint8_t sunsetHour = 0;
uint8_t lastHour = 24; // hour() returns zero through twenty-three
uint32_t debug1 = 0;
uint32_t debug2 = 0;
uint32_t debug3 = 0;
uint8_t brightness = 255;
void setup()
{
Particle.subscribe("hook-response/check_sunrise", gotSunriseData, MY_DEVICES);
strip.begin();
for (int i=0; i <= strip.numPixels(); i++) {
strip.setPixelColor(i, 0);
}
strip.show(); // Initialize all pixels to 'off'
Particle.variable("Debug1", debug1);
Particle.variable("Debug2", debug2);
Particle.variable("Debug3", debug3);
pinMode(MOTION_PIN, INPUT);
Particle.publish("check_sunrise"); //Get sunrise sunset times
delay(1000);
}
void loop() {
onceADay(); //Refresh
vMotion = checkMotion();
setBrightness();
if (vMotion != vMotionPrevious) {
fade(vMotion);
vMotionPrevious = vMotion;
}
if (vMotion) {
//side desk strips
for (int i=0; i <= LEFT_LED; i++) {
int c = abs(((i + cOffset) % 90) - 45) + 191;
strip.setPixelColor(i, Wheel(c));
strip.setPixelColor(i + 132, Wheel(c));
}
//back desk strip
for (int i = LEFT_LED + 1; i <= ( LEFT_LED + BACK_LED ); i++) {
int c = abs(((i + cOffset) % 70) - 35) + 75;
strip.setPixelColor(i, Wheel(c));
}
cOffset++;
//} else {
} else {
for (int i=0; i <= strip.numPixels(); i++) {
strip.setPixelColor(i, (brightness*0/255), (brightness*0/255), (brightness*50/255));
}
}
strip.show();
delay(200);
debug1 = strip.getPixelColor(5);
}
void onceADay() {
uint8_t currentHour = Time.hour();
if (lastHour != currentHour && currentHour == 1) {
Particle.publish("check_sunrise"); //This runs every day at 1:00 AM
lastHour = currentHour;
}
}
void gotSunriseData(const char *name, const char *data) {
String str = String(data);
char strBuffer[400] = "";
str.toCharArray(strBuffer, 400);
//lastGotSunrise = Time.now();
sunriseHour = atoi(strtok(strBuffer, "~"));
sunsetHour = atoi(strtok(NULL, "~"));
//debug1 = sunriseHour;
//debug2 = sunsetHour;
}
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color((brightness*(WheelPos * 3)/255), (brightness*(255 - WheelPos * 3)/255), (brightness*(0)/255));
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color((brightness*(255 - WheelPos * 3)/255), (brightness*(0)/255), (brightness*(WheelPos * 3)/255));
} else {
WheelPos -= 170;
return strip.Color((brightness*(0)/255), (brightness*(WheelPos * 3)/255), (brightness*(255 - WheelPos * 3)/255));
}
}
bool checkMotion() { //Check for motion
uint16_t motion = analogRead(MOTION_PIN); // Analog Read
debug2 = motion;
static bool intMotion = 1;
if ( motion > 0 ) {
intMotion = 1;
lastMotion = Time.now();
}
if ( motion < 5 && Time.now() - lastMotion >= 1 ) { //600
intMotion = 0;
}
return intMotion;
}
void setBrightness() {
bool i;
if ((Time.hour() < sunsetHour || Time.hour() > sunriseHour) && vMotion) {
i = 1;
} else {
i = 0;
}
if ( i && brightness != 255) {
brightness++;
} else if ( !i && brightness != 100) {
brightness--;
}
}
// int c = abs((j % 90) - 45) + 191;
// strip.setPixelColor(i, Wheel(c));
//c = abs(((i + cOffset) % 70) - 35) + 75;
//
void fade(bool a) {
int16_t steps = 20;
if (a) {
for (int16_t i = 0 ; i < steps - 1 ; ++i) {
//side desk strips
for (int j=0; j <= LEFT_LED; j++) {
int final = Wheel(abs((j % 90) - 45) + 191);
int diff = final - strip.getPixelColor(j);
int c = strip.getPixelColor(j) + (diff * (i / steps));
strip.setPixelColor(j, c);
strip.setPixelColor(j + 132, c);
}
//back desk strip
for (int j = LEFT_LED + 1; j <= ( LEFT_LED + BACK_LED ); j++) {
int final = Wheel(abs((j % 70) - 35) + 75);
int diff = final - strip.getPixelColor(j);
int c = strip.getPixelColor(j) + (diff * (i / steps));
strip.setPixelColor(j, c);
}
strip.show();
delay(200);
}
} else {
for (int16_t i = 0 ; i < steps - 1 ; ++i) {
//side desk strips
for (int j=0; j <= strip.numPixels(); j++) {
int final = Wheel(abs((j % 90) - 45) + 191);
int diff = final - strip.getPixelColor(j);
int c = strip.getPixelColor(j) + (diff * (i / steps));
strip.setPixelColor(j, c);
strip.setPixelColor(j + 132, c);
}
strip.show();
delay(200);
}
}
}
/*void loop() {
color %= 255;
runner %= 81;
for (int k = 0; k < 20; k++){
//left desk strip
for (int i=0; i <= LEFT_LED; i++) {
//strip.setPixelColor(i, 25, 175, 175); //teal full-brightness
strip.setPixelColor(i, 10, 70, 70);
}
//back desk strip
for (int i = LEFT_LED + 1; i <= ( LEFT_LED + BACK_LED ); i++) {
strip.setPixelColor(i, 100, 0, 0); //Red
}
//back desk strip
for (int i = ( LEFT_LED + BACK_LED ) + 1; i <= ( LEFT_LED + BACK_LED + RIGHT_LED ); i++) {
strip.setPixelColor(i, 10, 70, 70);
}
for (int j = LEFT_LED + 1; j <= (LEFT_LED + BACK_LED ); j += 20 ) {
strip.setPixelColor(j+k,200,200,200); //White
}
strip.show();
delay(70);
}
color++;
runner++;
}*/