-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightride.ino
163 lines (137 loc) · 3.22 KB
/
lightride.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
#include <FastLED.h>
#include <time.h>
//from FastLED example
#define LED_PIN 5
#define NUM_LEDS 28
#define BRIGHTNESS 32
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define TURNLIGHT_TIME_MS 500
//lightride-specific
#define F_SIZE 14
CRGB f_leds[F_SIZE];
#define B_SIZE 7
CRGB bl_leds[B_SIZE];
CRGB br_leds[B_SIZE];
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 1000
//input
#include <EasyButton.h>
#define L_PIN 3
#define R_PIN 2
#define F_PIN 4
EasyButton R_button(R_PIN);
EasyButton L_button(L_PIN);
EasyButton F_button(F_PIN);
char mode;
//time
unsigned long _time;
int current_time;
void setup() {
Serial.begin(9600);
delay(3000);
FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
//compute led strip positions
fill(f_leds, F_SIZE, CRGB::White);
fill(bl_leds, B_SIZE, CRGB::Yellow);
fill(br_leds, B_SIZE, CRGB::Red);
R_button.begin();
R_button.onPressed(onPressed_R);
L_button.begin();
L_button.onPressed(onPressed_R);
F_button.begin();
F_button.onPressed(onPressed_F);
}
void loop() {
R_button.read();
L_button.read();
F_button.read();
_time = millis() % 500;
anim_func_turnlights(bl_leds,B_SIZE, _time, 1);
//fill(bl_leds,B_SIZE,CRGB::Red);
switch(mode)
{
case 'L': //Turn left
fill(f_leds, F_SIZE, CRGB::White);
anim_func_turnlights(bl_leds,B_SIZE, _time, 1);
fill(br_leds, B_SIZE, CRGB::Red);
break;
//Turn right
case 'R':
fill(f_leds, F_SIZE, CRGB::White);
fill(bl_leds, B_SIZE, CRGB::Red);
anim_func_turnlights(br_leds,B_SIZE, _time, 0);
break;
//Hazards
case 'H':
fill(f_leds, F_SIZE, CRGB::White);
anim_func_turnlights(br_leds,B_SIZE, _time, 0);
anim_func_turnlights(bl_leds,B_SIZE, _time, 1);
break;
default:
fill(f_leds, F_SIZE, CRGB::White);
fill(bl_leds, B_SIZE, CRGB::Red);
fill(br_leds, B_SIZE, CRGB::Red);
}
//anim_func_turnlights(br_leds,B_SIZE, _time, 0);
update_all();
FastLED.show();
FastLED.delay(1000/UPDATES_PER_SECOND);
}
void fill(CRGB leds[], int n, CRGB color){
for(int i = 0; i < n ;i++){
leds[i] = color;
}
}
void update_all(){
int f_end = F_SIZE;
int bl_end = F_SIZE+B_SIZE;
int br_end = F_SIZE+B_SIZE*2;
for(int i = 0; i < f_end; i++){
leds[i] = f_leds[i];
}
for(int i = f_end; i < bl_end ;i++){
leds[i] = bl_leds[i-f_end];
}
for(int i = bl_end; i < br_end ;i++){
leds[i] = br_leds[i-bl_end];
}
}
void printLEDState(){
for(int i = 0; i < sizeof(leds);i++){
Serial.println(leds[i]);
}
}
int anim_func_turnlights(CRGB this_leds[], int n, float current_time, int reverse){
int current_leds = 0;
current_leds = (current_time/TURNLIGHT_TIME_MS)*n;
fill(this_leds, n, CRGB::Black);
if(reverse){
for(int i= n; i >= B_SIZE-current_leds;i--){
this_leds[i-1] = CRGB::Yellow;
}
}else{
for(int i=0; i <= current_leds;i++){
this_leds[i] = CRGB::Yellow;
}
}
return 0;
}
//INPUTS
void onPressed_R(){
if(mode == 'R'){
mode = 'D';
}else{
mode = 'R';
}
}
void onPressed_L(){
if(mode == 'L'){
mode = 'D';
}else{
mode = 'L';
}
}
void onPressed_F(){
}