forked from James020118/led_strip_controller
-
Notifications
You must be signed in to change notification settings - Fork 2
/
arduino.ino
285 lines (272 loc) · 7.81 KB
/
arduino.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
279
280
281
282
283
284
285
#include "FastLED.h"
#include <SoftwareSerial.h>
#include <pixeltypes.h>
#define NUM_LEDS 60
#define LED_TYPE WS2812B
#define COLOUR_ORDER GRB
#define RESET 5 // Reset Pin location
#define STROBE 4 // we output values to these to control the shield
#define LEFT A0 // inputs from ICs on the shield
#define RIGHT A1
// Define Colours
#define Blue CRGB(0x0000FF) // A
#define Green CRGB(0x008000) // B
#define HotPink CRGB(0xFF69B4) // C
#define Orange CRGB(0xFFA500) // D
#define Red CRGB(0xFF0000) // E
#define Purple CRGB(0x800080) // F
#define Yellow CRGB(0xFFFF00) // G
#define Turquoise CRGB(0x40E0D0) // H
#define Salmon CRGB(0xFA8072) // I
#define Fuchsia CRGB(255,0, 255) // J
#define Gold CRGB(255, 215, 0) // K
int left[7]; // holds 7 frequencies for the left side
int right[7]; // right side
int band;
int brightness = 60;
const int LED_PIN = 6;
CRGB leds[NUM_LEDS]; // Each index has 3 bytes of data, 1 byte for R, G, B each
SoftwareSerial bluetooth(0, 1);
String state;
CRGB colour;
int whichBand;
void setup() {
pinMode(RESET, OUTPUT); // we will be outputting signals (High->Low) to RESETSparkfun & separate the data it had for the previous seven bands from the incoming data
pinMode(STROBE, OUTPUT); // we do the same w strobe so that we can tell the sparkfun to cycle through the 7 bands of audio information
pinMode(LEFT, INPUT); // We have left and right audio,
pinMode(RIGHT, INPUT);
digitalWrite(RESET, LOW); // we initialize RESET and STROBE to low
digitalWrite(STROBE, LOW);
delay(5);
Serial.begin(38400);
FastLED.addLeds<LED_TYPE, LED_PIN, COLOUR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(brightness);
bluetooth.begin(9600);
}
void loop(){
if (Serial.available() > 0) { // get data from App
state = Serial.readString();
switch (state[0]){ // first character: brightness, value: a-f
case 'a':
FastLED.setBrightness(42);
Serial.println("Light Brightness at 0%");
break;
case 'b':
FastLED.setBrightness(84);
Serial.println("Light Brightness at 20%");
break;
case 'c':
FastLED.setBrightness(126);
Serial.println("Light Brightness at 40%");
break;
case 'd':
FastLED.setBrightness(168);
Serial.println("Light Brightness at 60%");
break;
case 'e':
FastLED.setBrightness(210);
Serial.println("Light Brightness at 80%");
break;
case 'f':
FastLED.setBrightness(252);
Serial.println("Light Brightness at 100%");
break;
default:
Serial.println("Invalid character");
break;
}
switch (state[1]){ // second character: pattern, value: 0-2
case '0':
// Recursion, Switch on colour
colour = determineColour(state);
recursion_lit(0,1, colour);
Serial.println("Recursion");
break;
case '1':
colour = determineColour(state);
whichBand = determineFreq(state);
readShield();
ontoLights(whichBand, colour, state);
Serial.println("REACT Setting");
break;
case '2':
rainbow();
Serial.println("RAINBOW Setting");
break;
default:
Serial.println("Invalid character");
break;
}
}
}
CRGB determineColour(String state){
switch (state[3]){
case 'A':
return Blue;
Serial.println("Color of LED is BLUE");
break;
case 'B':
return Green;
Serial.println("Color of LED is GREEN");
break;
case 'C':
return HotPink;
Serial.println("Color of LED is HOT PINK");
break;
case 'D':
return Orange;
Serial.println("Color of LED is ORANGE");
break;
case 'E':
return Red;
Serial.println("Color of LED is RED");
break;
case 'F':
return Purple;
Serial.println("Color of LED is PURPLE");
break;
case 'G':
return Yellow;
Serial.println("Color of LED is YELLOW");
break;
case 'H':
return Turquoise;
Serial.println("Color of LED is TURQUOISE");
break;
case 'I':
return Fuchsia;
Serial.println("Color of LED is FUCHSIA");
break;
case 'J':
return Gold;
Serial.println("Color of LED is GOLD");
break;
case 'K':
return Salmon;
Serial.println("Color of LED is SALMON");
break;
case 'L':
return Salmon; // Outlier
Serial.println("Color of LED is RAINBOW");
break;
default:
Serial.println("Invalid character");
break;
}
}
int determineFreq(String state){
switch (state[2]){
case '3':
return 0;
Serial.println("Frequency at 63Hz");
break;
case '4':
return 1;
Serial.println("Frequency at 160Hz");
break;
case '5':
return 2;
Serial.println("Frequency at 400Hz");
break;
case '6':
return 3;
Serial.println("Frequency at 1kHz");
break;
case '7':
return 4;
Serial.println("Frequency at 2.5kHz");
break;
case '8':
return 5;
Serial.println("Frequency at 6.25kHz");
break;
case '9':
return 6;
Serial.println("Frequency at 16kHz");
break;
default:
Serial.println("Invalid character");
break;
}
}
void readShield(){
digitalWrite(RESET, HIGH);
digitalWrite(RESET, LOW); // Reset ICs to separate from previous bands
for (band = 0; band < 7; band++){
// this is for it to read the data of one band and then stop it
digitalWrite(STROBE, HIGH);
delay(1);
digitalWrite(STROBE, LOW);
delay(1);
left[band] = analogRead(LEFT);
right[band] = analogRead(RIGHT);
}
}
void fill_rainbow2( struct CRGB * pFirstLED, int numToFill,uint8_t initialhue, uint8_t deltahue ){
CHSV hsv; // declaring the struct?
hsv.hue = initialhue; // initializing its member vars
hsv.val = 255;
hsv.sat = 255;
for( int i = 0; i < numToFill; i++) {
pFirstLED[i] = hsv;
hsv.hue += deltahue;
}
}
void ontoLights(int targetBand, CRGB displayColour, String state){
int react = map(left[targetBand], 0, 1023, 0, NUM_LEDS-1); // map the frequency 'concentration' values of 0 - 1023 to 0 - 60 LEDs
FastLED.clear();
if (state[3] == 'L'){
fill_rainbow2(leds, react, 0, 4); // fill the 'react' amount of LEDs
}
else{
for(int i = 0; i < react; i++){
leds[i] = displayColour; // fill the 'react' amount of LEDS w a specific colour
}
}
FastLED.show();
}
int recursion_lit(int curr_led, int dir, CRGB colour){
delay(5);
FastLED.show();
if (curr_led == NUM_LEDS - 1 && dir == 1){
leds[curr_led] = CRGB(0,0,0);
return recursion_lit(curr_led - 1, 0, colour);
}
else if (curr_led == 0 && dir == 0){
leds[curr_led] = colour;
return recursion_lit(curr_led+1, 1, colour);
}
else if (dir == 1){
leds[curr_led] = colour;
return recursion_lit(curr_led+1,1, colour);
}
else if (dir == 0){
leds[curr_led] = CRGB(0, 0, 0);
return recursion_lit(curr_led-1, 0, colour);
}
}
CHSV Rotate(int pos) {
CHSV colour (pos, 255, 255);
return colour;
}
void rainbow(){
for(int j = 0; j < 256; j++) {
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = Rotate((i * 256 / NUM_LEDS + j) % 256); // (5*i + j) % 256 works too
}
FastLED.show();
delay(1);
}
}
void graphFrequencies() {
Serial.print( (left[0] + right[0]) / 2 );
Serial.print(" ");
Serial.print( (left[2] + right[2]) / 2 );
Serial.print(" ");
Serial.print( (left[4] + right[4]) / 2 );
Serial.print(" ");
Serial.print( (left[6] + right[6]) / 2 );
Serial.print(" ");
Serial.println();
delay(20);
}