/
millis_neopixel_buttons.ino
278 lines (233 loc) · 6.93 KB
/
millis_neopixel_buttons.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* This program makes the Arduino listen to Serial Monitor data sent by the Raspberry Pi.
The LEDs inside three arcade buttons can be controlled through the RPI-Arduino
connection. It has a number of states of (in)activeness.
Original millis()-based code by James Lewis, as pwm-fade-with-millis.ino
see baldengineer.com/fading-led-analogwrite-millis-example.html for the source code
Original controlling Arduino with RPI code by Fabio Nelli
see meccanismocomplesso.org/en/controlling-arduino-raspberry-pi/ for the source code
Adapted by Arthur Geel, 03-06-2018
*/
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Pins for LED buttons, Neopixel ring
#define PIN 6
const byte LED_BUTTON_1 = 9;
const byte LED_BUTTON_2 = 10;
const byte LED_BUTTON_3 = 11;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, PIN, NEO_GRB + NEO_KHZ800);
// piInput is input sent from the Raspberry Pi
int piInput;
// define directions for LED fade
#define UP 0
#define DOWN 1
// constants for min and max PWM
const int minPWM = 0;
const int maxPWM = 255;
// State Variable for Fade Direction
byte fadeDirection = UP;
// Global Fade Value
// but be bigger than byte and signed, for rollover
int fadeValue = 0;
byte fadeIncrement = 5;
// millis() timing Variable, just for fading
unsigned long previousFadeMillis;
unsigned long npMillis = 0;
// Neopixel variables
#define NUM_PIXELS 12
uint32_t currentColor;
uint16_t currentPixel = 0;
// How fast to increment?
int fadeInterval = 50;
int fastFadeInterval = 20;
void setup() {
// On startup: ensure buttons and ring are 'off'.
analogWrite(LED_BUTTON_1, 0);
analogWrite(LED_BUTTON_2, 0);
analogWrite(LED_BUTTON_3, 0);
currentColor = strip.Color(50,50,50);
currentPixel = 0;
strip.begin();
strip.show();
Serial.begin(9600);
piInput = 0;
}
void regularFade(unsigned long thisMillis) {
// is it time to update yet?
// if not, nothing happens
if (thisMillis - previousFadeMillis >= fadeInterval) {
// yup, it's time!
if (fadeDirection == UP) {
fadeValue = fadeValue + fadeIncrement;
if (fadeValue >= maxPWM) {
// At max, limit and change direction
fadeValue = maxPWM;
fadeDirection = DOWN;
}
} else {
//if we aren't going up, we're going down
fadeValue = fadeValue - fadeIncrement;
if (fadeValue <= minPWM) {
// At min, limit and change direction
fadeValue = minPWM;
fadeDirection = UP;
}
}
// Only need to update when it changes
analogWrite(LED_BUTTON_1, fadeValue);
analogWrite(LED_BUTTON_2, fadeValue);
analogWrite(LED_BUTTON_3, fadeValue);
// reset millis for the next iteration (fade timer only)
previousFadeMillis = thisMillis;
}
}
void fastFade(unsigned long thisMillis) {
// is it time to update yet?
// if not, nothing happens
if (thisMillis - previousFadeMillis >= fastFadeInterval) {
// yup, it's time!
if (fadeDirection == UP) {
fadeValue = fadeValue + fadeIncrement;
if (fadeValue >= maxPWM) {
// At max, limit and change direction
fadeValue = maxPWM;
fadeDirection = DOWN;
}
} else {
//if we aren't going up, we're going down
fadeValue = fadeValue - fadeIncrement;
if (fadeValue <= minPWM) {
// At min, limit and change direction
fadeValue = minPWM;
fadeDirection = UP;
}
}
// Only need to update when it changes
analogWrite(LED_BUTTON_1, fadeValue);
analogWrite(LED_BUTTON_2, fadeValue);
analogWrite(LED_BUTTON_3, fadeValue);
// reset millis for the next iteration (fade timer only)
previousFadeMillis = thisMillis;
}
}
void fadePlayButton(unsigned long thisMillis) {
// is it time to update yet?
// if not, nothing happens
if (thisMillis - previousFadeMillis >= fastFadeInterval) {
// yup, it's time!
if (fadeDirection == UP) {
fadeValue = fadeValue + fadeIncrement;
if (fadeValue >= maxPWM) {
// At max, limit and change direction
fadeValue = maxPWM;
fadeDirection = DOWN;
}
} else {
//if we aren't going up, we're going down
fadeValue = fadeValue - fadeIncrement;
if (fadeValue <= minPWM) {
// At min, limit and change direction
fadeValue = minPWM;
fadeDirection = UP;
}
}
// Only need to update when it changes
analogWrite(LED_BUTTON_1, fadeValue);
analogWrite(LED_BUTTON_2, 0);
analogWrite(LED_BUTTON_3, 0);
// reset millis for the next iteration (fade timer only)
previousFadeMillis = thisMillis;
}
}
void buttonsOn(){
analogWrite(LED_BUTTON_1, 255);
analogWrite(LED_BUTTON_2, 255);
analogWrite(LED_BUTTON_3, 255);
}
void buttonsOff(){
analogWrite(LED_BUTTON_1, 0);
analogWrite(LED_BUTTON_2, 0);
analogWrite(LED_BUTTON_3, 0);
}
void colorWipe(){
strip.setPixelColor(currentPixel, strip.Color(50,50,50));
strip.show();
currentPixel++;
if(currentPixel == NUM_PIXELS) {
currentPixel = 0;
}
}
void colorWipe_2(){
strip.setPixelColor(currentPixel, strip.Color(50,0,0));
strip.show();
currentPixel++;
if(currentPixel == NUM_PIXELS) {
currentPixel = 0;
}
}
void colorWipe_3(){
strip.setPixelColor(currentPixel, strip.Color(50,50,80));
strip.show();
currentPixel++;
if(currentPixel == NUM_PIXELS) {
currentPixel = 0;
}
}
void pixelOff(){
strip.setPixelColor(currentPixel, strip.Color(0,0,0));
strip.show();
currentPixel++;
if (currentPixel == NUM_PIXELS) {
currentPixel = 0;
}
}
void loop() {
// get the current time, for this time around loop
// all millis() timer checks will use this time stamp
unsigned long currentMillis = millis();
if (Serial.available()){
piInput = Serial.read() - '0';
}
if (piInput == 1) {
buttonsOff();
pixelOff();
}
else if (piInput == 2) {
regularFade(currentMillis);
// Slow pulsing buttons. Subtle ring animation.
if ((unsigned long)(millis() - npMillis) >= fadeInterval) {
npMillis = millis();
colorWipe();
}
}
else if (piInput == 3) {
fastFade(currentMillis);
if ((unsigned long)(millis() - npMillis) >= fadeInterval) {
npMillis = millis();
colorWipe_2();
}
// Fast pulsing button. Prominent ring animations.
}
else if (piInput == 4) {
regularFade(currentMillis);
if ((unsigned long)(millis() - npMillis) >= fadeInterval) {
npMillis = millis();
colorWipe();
}
// buttons gently pulse, LED ring should indicate current time and overall time.
}
else if (piInput == 5) {
fadePlayButton(currentMillis);
if ((unsigned long)(millis() - npMillis) >= fadeInterval) {
npMillis = millis();
pixelOff();
}
// buttons gently pulse, LED ring should indicate current time and overall time.
}
else {
buttonsOff();
pixelOff();
// catch incorrect data: this will turn the buttons off.
}
}