-
Notifications
You must be signed in to change notification settings - Fork 0
/
RobotLEDs2018.ino
202 lines (183 loc) · 4.25 KB
/
RobotLEDs2018.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 <FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 142
#define BRIGHTNESS 255
#define LED_TYPE NEOPIXEL
const int TYPE_SWITCH_PIN = 12;
const int ALLIANCE_PIN = 10;
// Comment out for prod to save memory
//#define DEBUG 1
CRGB leds[NUM_LEDS];
// This example shows several ways to set up and use 'palettes' of colors
// with FastLED.
void setup() {
#ifdef DEBUG
Serial.begin(9600);
Serial.println("2018 Robot LEDs starting");
#endif
FastLED.addLeds<LED_TYPE, LED_PIN>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
setAll(CRGB(0,0,0));
pinMode(TYPE_SWITCH_PIN, INPUT);
pinMode(ALLIANCE_PIN, INPUT);
}
void loop() {
if (digitalRead(TYPE_SWITCH_PIN)) {
// rainbowSingle(35);
rainbowDouble(30);
} else {
setAll(CRGB(0,0,0));
if (digitalRead(ALLIANCE_PIN)) {
// Blue
nightRiderSides(CRGB(0, 0, 255), CRGB(0,0,0), 5, 10, 0);
} else {
// Red
nightRiderSides(CRGB(255, 0, 0), CRGB(0,0,0), 5, 10, 0);
}
// nightRider(CRGB(255, 0, 0), CRGB(0, 0, 0), 3, 50);
}
// rainbowDouble(35);
// nightRider(CRGB(255, 0, 0), CRGB(255, 255, 255), 3, 50);
// nightRiderSides(CRGB(255, 0, 0), CRGB(0, 0, 0), 3, 1000);
// nightRiderSidesOneWayOpp(CRGB(255, 0, 0), CRGB(0, 0, 0), 3, random(20, 70));
}
void setAll(CRGB c) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = c;
}
FastLED.show();
}
void setDoubleSide(int i, CRGB c) {
if (i < 23) {
// Bottom
leds[22-i] = c;
leds[23+i] = c;
} else if (i < 71) {
// Top
leds[142-i+22] = c;
leds[46+i-23] = c;
}
FastLED.show();
}
void nightRiderSidesOneWay(CRGB c, CRGB c2, int len, int delayTime) {
int i=0;
for (int i=0; i < NUM_LEDS/2; i++) { // Forward
leds[i] = c;
leds[(NUM_LEDS/2) + i] = c;
if (i - len >= 0) {
leds[i - len] = c2;
leds[((NUM_LEDS/2) + i) - len] = c2;
}
FastLED.show();
delay(delayTime);
}
for (int i = len; i >= 0; i--) {
leds[NUM_LEDS - i] = c2;
leds[(NUM_LEDS/2) - i] = c2;
FastLED.show();
delay(delayTime);
}
}
/**
* Nightrider effect
* CRGB c - color
* CRGB c2 - background color
* int len - length
* int delayTime - delay time
* int dir - direction
* 0 = both ways
* 1 = up
* 2 = down
*/
void nightRiderSides(CRGB c, CRGB c2, int len, int delayTime, int dir) {
if (dir == 0 || dir == 1) {
// Up
for (int i = 0; i < NUM_LEDS/2; i++) {
setDoubleSide(i, c);
if (i > len-1) {
setDoubleSide(i-len, c2);
}
delay(delayTime);
}
}
if (dir == 0 || dir == 2) {
// Down
for (int i = 0; i < NUM_LEDS/2; i++) {
setDoubleSide((NUM_LEDS/2) - i, c);
if ((NUM_LEDS/2) - i > len-1) {
setDoubleSide((NUM_LEDS/2) - i + len, c2);
}
delay(delayTime);
}
}
}
void rainbowSingle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<NUM_LEDS; i++) {
leds[i] = Wheel((i+j) & 255);
}
FastLED.show();
delay(wait);
}
}
void rainbowDouble(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<NUM_LEDS/2; i++) {
setDoubleSide(i, Wheel((i+j) & 255));
}
FastLED.show();
delay(wait);
}
}
CRGB Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
}
void nightRider(CRGB c, CRGB c2, int len, int delayTime) {
int i=0;
for (int i=0; i<NUM_LEDS; i++) { //FORWARD
leds[i] = c;
if (i-len >= 0) {
leds[i-len] = c2;
}
FastLED.show();
delay(delayTime);
}
for (int i=0; i<NUM_LEDS; i++) { //BACKWARD
leds[NUM_LEDS-i] = c;
if ((NUM_LEDS-i) <= NUM_LEDS-len) {
leds[(NUM_LEDS-i) + len - 1] = c2;
}
FastLED.show();
delay(delayTime);
}
}
CRGB wheel(int WheelPos, int dim) {
CRGB color;
if (85 > WheelPos) {
color.r=0;
color.g=WheelPos * 3/dim;
color.b=(255 - WheelPos * 3)/dim;;
}
else if (170 > WheelPos) {
color.r=WheelPos * 3/dim;
color.g=(255 - WheelPos * 3)/dim;
color.b=0;
}
else {
color.r=(255 - WheelPos * 3)/dim;
color.g=0;
color.b=WheelPos * 3/dim;
}
return color;
}