-
Notifications
You must be signed in to change notification settings - Fork 0
/
light_saber.ino
269 lines (255 loc) · 6.88 KB
/
light_saber.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
#define FASTLED_INTERNAL
#include <FastLED.h>
#include "OneButton.h"
// ------ Settings ------
#define LED_PIN 8
#define BUTTON_PIN 5
#define NUM_LEDS 50
#define DELAY 3
bool orange = true;
bool green = true;
bool red = true;
bool white = true;
bool purple = true;
bool yellow = true;
bool blue = true;
bool flickering = true;
// --------------------
int f = 0;
int g = 0;
int done = 0;
bool toggleBrightness = false;
int flickerBrightness = 255;
String color = "blue";
CRGB leds[NUM_LEDS];
OneButton button(BUTTON_PIN, false);
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
button.attachClick(singleclick);
button.attachDoubleClick(doubleclick);
if (blue == false && orange == true) {
color = "orange";
}
else if (blue == false && orange == false && green == true) {
color = "green";
}
else if (blue == false && orange == false && green == false && red == true) {
color = "red";
}
else if (blue == false && orange == false && green == false && red == false && white == true) {
color = "white";
}
else if (blue == false && orange == false && green == false && red == false && white == false && purple == true) {
color = "purple";
}
}
void loop() {
button.tick();
flicker();
}
void singleClick() {
if (f == 0) {
for (int i = 0; i <= NUM_LEDS; i++) {
if (blue == true && color == "blue") {
setLED(i, 0, 0, 255);
flicker();
}
else if (orange == true && color == "orange") {
setLED(i, 255, 128, 0);
g = 1;
flicker();
}
else if (green == true && color == "green") {
setLED(i, 0, 255, 0);
g = 2;
flicker();
}
else if (red == true && color == "red") {
setLED(i, 255, 0, 0);
g = 3;
flicker();
}
else if (white == true && color == "white") {
setLED(i, 255, 255, 255);
g = 4;
flicker();
}
else if (purple == true && color == "purple") {
setLED(i, 127, 0, 255);
g = 5;
flicker();
}
else if (yellow == true && color == "yellow") {
setLED(i, 251, 255, 0);
flicker();
}
}
f = 1;
}
else {
for (int i = NUM_LEDS; i >= 0; i--) {
setLED(i,0, 0, 0);
flicker();
}
f = 0;
}
}
void doubleClick() {
if (f == 1) {
if (g == 0) {
if (orange == true) {
color = "orange";
done = 0;
for (int i = NUM_LEDS; i >= 0; i--) {
setLED(i, 0, 0, 0);
flicker();
}
for (int i = 0; i <= NUM_LEDS; i++) {
setLED(i, 255, 128, 0);
flicker();
}
}
else {
g++;
done = 1;
}
}
if (g == 1) {
if (green == true) {
color = "green";
done = 0;
for (int i = NUM_LEDS; i >= 0; i--) {
setLED(i, 0, 0, 0);
flicker();
}
for (int i = 0; i <= NUM_LEDS; i++) {
setLED(i, 0, 255, 0);
flicker();
}
}
else {
g++;
done = 1;
}
}
if (g == 2) {
if (red == true) {
color = "red";
done = 0;
for (int i = NUM_LEDS; i >= 0; i--) {
setLED(i, 0, 0, 0);
flicker();
}
for (int i = 0; i <= NUM_LEDS; i++) {
setLED(i, 255, 0, 0);
flicker();
}
}
else {
g++;
done = 1;
}
}
if (g == 3) {
if (white == true) {
color = "white";
done = 0;
for (int i = NUM_LEDS; i >= 0; i--) {
setLED(i, 0, 0, 0);
flicker();
}
for (int i = 0; i <= NUM_LEDS; i++) {
setLED(i, 255, 255, 255);
flicker();
}
}
else {
g++;
done = 1;
}
}
if (g == 4) {
if (purple == true) {
color = "purple";
done = 0;
for (int i = NUM_LEDS; i >= 0; i--) {
setLED(i, 0, 0, 0);
flicker();
}
for (int i = 0; i <= NUM_LEDS; i++) {
setLED(i, 127, 0, 255);
flicker();
}
}
else {
g++;
done = 1;
}
}
if (g == 5) {
if (yellow == true) {
color = "yellow";
done = 0;
for (int i = NUM_LEDS; i >= 0; i--) {
setLED(i, 0, 0, 0);
flicker();
}
for (int i = 0; i <= NUM_LEDS; i++) {
setLED(i, 251, 255, 0);
flicker();
}
}
else {
g++;
done = 1;
}
}
if (g == 6) {
if (blue == true) {
color = "blue";
done = 0;
for (int i = NUM_LEDS; i >= 0; i--) {
setLED(i, 0, 0, 0);
flicker();
}
for (int i = 0; i <= NUM_LEDS; i++) {
setLED(i, 0, 0, 255);
flicker();
}
g = -1;
}
else {
g = -1;
done = 0;
}
}
if (done == 0) {
g++;
}
}
}
void setLED(int a, int R, int G, int B) {
leds[a] = CRGB(R, G, B);
delay(DELAY);
FastLED.show();
}
void flicker() {
if (flickering == true) {
for (int i = 0; i <= NUM_LEDS; i++) {
if (toggleBrightness == false) {
flickerBrightness -= 1;
}
else if (toggleBrightness == true) {
flickerBrightness += 1;
}
if (flickerBrightness <= 0) {
toggleBrightness = true;
}
if (flickerBrightness >= 255) {
toggleBrightness = false;
}
}
FastLED.setBrightness(flickerBrightness);
FastLED.show();
}
}