forked from jjinno/led_twinkle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwinkle.ino
202 lines (165 loc) · 4.91 KB
/
twinkle.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
#include "SPI.h"
#define __WS2801
#ifdef __WS2801
#include "Adafruit_WS2801.h"
#else
#include <Adafruit_NeoPixel.h>
#endif
#define UNINITIALIZED -1 // This is how we identify an uninitialized pixel
#define LED_STRIP_LENGTH 42 // The total number of LED pixels
#define TOTAL_TWINKLES 10 // The number of LED pixels that will twinkle at once
#define INPUT1_PIN 8 // A pin that is attached to the accelerometer
#define INPUT2_PIN 9 // A pin that is attached to the accelerometer
#define DATA_PIN 13 // Data wire for pixels (both WS2801 & WS2811 style)
#ifdef __WS2801
#define CLOCK_PIN 12 // Clock wire for WS2801-style pixels only
Adafruit_WS2801 strip = Adafruit_WS2801(LED_STRIP_LENGTH, DATA_PIN, CLOCK_PIN);
#else
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_STRIP_LENGTH, DATA_PIN, NEO_GRB + NEO_KHZ800);
#endif
struct Twinkle
{
int index;
uint8_t r;
uint8_t g;
uint8_t b;
uint32_t color; // What we are heading for...
int denominator; // The magic number we are dividing by
int numerator; // where we are in the multiplication
char direction; // the direction we are headed (brighter/dimmer)
};
Twinkle twinkles[TOTAL_TWINKLES];
// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}
void initTwinkle()
{
uint8_t i=0, j=0, m=0;
int led=0;
if (digitalRead(INPUT1_PIN) != HIGH) {
return;
}
for (i=0; i<TOTAL_TWINKLES; i++) {
// We set the index to 0 on purpose to identify when a specific
// twinkle needs to be re-initialized... cause they will come and
// go at random
// Skip any non-zero (already initialized) twinkles...
if (twinkles[i].index != UNINITIALIZED) continue;
pickNewRandom:
led = random(LED_STRIP_LENGTH);
// Is that number already used?
for (j=0; j<TOTAL_TWINKLES; j++) {
if (twinkles[j].index == led) {
goto pickNewRandom;
}
}
// Give the next LED that new value
twinkles[i].index = led;
// Pick a random END color...
twinkles[i].r = random(256);
twinkles[i].g = random(256);
twinkles[i].b = random(256);
// And the color we display each time...
twinkles[i].color = Color(0,0,0);
// Pick a percentage of brightness we are at now...
twinkles[i].denominator = random(100,255);
twinkles[i].numerator = 0;
twinkles[i].direction = random(16);
}
}
void resetTwinkles()
{
uint8_t i=0;
for (i=0; i<TOTAL_TWINKLES; i++) {
twinkles[i].index = UNINITIALIZED;
twinkles[i].r = 0;
twinkles[i].g = 0;
twinkles[i].b = 0;
twinkles[i].numerator = 0;
twinkles[i].direction = 1;
}
// Initialize all uninitialized twinkles...
// which at this point is all of them...
initTwinkle();
}
void displayTwinkles(uint8_t wait)
{
uint8_t i=0,j=0;
boolean set = false;
for (i=0; i<LED_STRIP_LENGTH; i++) {
// If it was one of the Twinkles, set it here
for (j=0; j<TOTAL_TWINKLES; j++) {
if (twinkles[j].index != i) continue;
strip.setPixelColor(i, twinkles[j].color);
set = true;
break;
}
// If it was not one of the Twinkles, clear it
if (set == false) {
strip.setPixelColor(i, 0);
}
// Clear the value
set = false;
}
strip.show();
delay(wait);
}
void stepTwinkles()
{
uint8_t i=0,j=0;
for (i=0; i<LED_STRIP_LENGTH; i++) {
for (j=0; j<TOTAL_TWINKLES; j++) {
if (twinkles[j].index != i) continue;
// set the color...
twinkles[j].color = Color(
(byte)(((double)twinkles[j].r)*((double)twinkles[j].numerator / (double)twinkles[j].denominator)),
(byte)(((double)twinkles[j].g)*((double)twinkles[j].numerator / (double)twinkles[j].denominator)),
(byte)(((double)twinkles[j].b)*((double)twinkles[j].numerator / (double)twinkles[j].denominator))
);
// add 'd' to 'y' until it reaches 'x' or '0'...
twinkles[j].numerator += twinkles[j].direction;
if (twinkles[j].numerator >= twinkles[j].denominator) {
twinkles[j].numerator = twinkles[j].denominator;
twinkles[j].direction = -1 * twinkles[j].direction;
}
else
if (twinkles[j].numerator <= 0) {
twinkles[j].numerator = 0;
twinkles[j].direction = 1;
twinkles[j].index = UNINITIALIZED;
}
// If we got here it was because index == i, so we can break
break;
}
}
}
void setup() {
Serial.begin(19200);
strip.begin();
strip.show();
pinMode(INPUT1_PIN, INPUT);
pinMode(INPUT2_PIN, INPUT);
randomSeed(analogRead(0));
resetTwinkles();
}
void loop() {
if (digitalRead(INPUT2_PIN) != HIGH) {
displayTwinkles(10);
stepTwinkles();
initTwinkle();
}
else {
for (int i=0; i<LED_STRIP_LENGTH; i++) {
strip.setPixelColor(i, Color(255,0,0));
}
strip.show();
}
}