-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
music_visualizer_sparkfun_spectrum_shield.ino
243 lines (207 loc) · 5.47 KB
/
music_visualizer_sparkfun_spectrum_shield.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
#include <FastLED.h>
// Arduino Music Visualizer 0.3
// This music visualizer works off of analog input from a 3.5mm headphone jack
// Just touch jumper wire from A0 to tip of 3.5mm headphone jack
// The code is dynamic and can handle variable amounts of LEDs
// as long as you adjust NUM_LEDS according to the amount of LEDs you are using
// This code uses the Sparkfun Spectrum Shield
// LED LIGHTING SETUP
#define LED_PIN 6
#define NUM_LEDS 200 // 250
#define BRIGHTNESS 64
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
// AUDIO INPUT SETUP
int strobe = 4;
int reset = 5;
int audio1 = A0;
int audio2 = A1;
int left[7];
int right[7];
int band;
int audio_input = 0;
int freq = 0;
// STANDARD VISUALIZER VARIABLES
int midway = NUM_LEDS / 2; // CENTER MARK FROM DOUBLE LEVEL VISUALIZER
int loop_max = 0;
int k = 255; // COLOR WHEEL POSITION
int decay = 0; // HOW MANY MS BEFORE ONE LIGHT DECAY
int decay_check = 0;
long pre_react = 0; // NEW SPIKE CONVERSION
long react = 0; // NUMBER OF LEDs BEING LIT
long post_react = 0; // OLD SPIKE CONVERSION
// RAINBOW WAVE SETTINGS
int wheel_speed = 2;
void setup()
{
// SPECTRUM SETUP
pinMode(audio1, INPUT);
pinMode(audio2, INPUT);
pinMode(strobe, OUTPUT);
pinMode(reset, OUTPUT);
digitalWrite(reset, LOW);
digitalWrite(strobe, HIGH);
// LED LIGHTING SETUP
delay( 3000 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );
// CLEAR LEDS
for (int i = 0; i < NUM_LEDS; i++)
leds[i] = CRGB(0, 0, 0);
FastLED.show();
// SERIAL AND INPUT SETUP
Serial.begin(115200);
Serial.println("\nListening...");
}
// FUNCTION TO GENERATE COLOR BASED ON VIRTUAL WHEEL
// https://github.com/NeverPlayLegit/Rainbow-Fader-FastLED/blob/master/rainbow.ino
CRGB Scroll(int pos) {
pos = abs(pos);
CRGB color (0,0,0);
if(pos < 85) {
color.g = 0;
color.r = ((float)pos / 85.0f) * 255.0f;
color.b = 255 - color.r;
} else if(pos < 170) {
color.g = ((float)(pos - 85) / 85.0f) * 255.0f;
color.r = 255 - color.g;
color.b = 0;
} else if(pos < 256) {
color.b = ((float)(pos - 170) / 85.0f) * 255.0f;
color.g = 255 - color.b;
color.r = 1;
}
/*
Serial.print(pos);
Serial.print(" -> ");
Serial.print("r: ");
Serial.print(color.r);
Serial.print(" g: ");
Serial.print(color.g);
Serial.print(" b: ");
Serial.println(color.b);
*/
return color;
}
// FUNCTION TO GET AND SET COLOR
// THE ORIGINAL FUNCTION WENT BACKWARDS
// THE MODIFIED FUNCTION SENDS WAVES OUT FROM FIRST LED
// https://github.com/NeverPlayLegit/Rainbow-Fader-FastLED/blob/master/rainbow.ino
void singleRainbow()
{
for(int i = NUM_LEDS - 1; i >= 0; i--) {
if (i < react)
leds[i] = Scroll((i * 256 / 50 + k) % 256);
else
leds[i] = CRGB(0, 0, 0);
}
FastLED.show();
}
// FUNCTION TO MIRRORED VISUALIZER
void doubleRainbow()
{
for(int i = NUM_LEDS - 1; i >= midway; i--) {
if (i < react + midway) {
//Serial.print(i);
//Serial.print(" -> ");
leds[i] = Scroll((i * 256 / 50 + k) % 256);
//Serial.print(i);
//Serial.print(" -> ");
leds[(midway - i) + midway] = Scroll((i * 256 / 50 + k) % 256);
}
else
leds[i] = CRGB(0, 0, 0);
leds[midway - react] = CRGB(0, 0, 0);
}
FastLED.show();
}
void readMSGEQ7()
// Function to read 7 band equalizers
{
digitalWrite(reset, HIGH);
digitalWrite(reset, LOW);
for(band=0; band <7; band++)
{
digitalWrite(strobe, LOW); // strobe pin on the shield - kicks the IC up to the next band
delayMicroseconds(30); //
left[band] = analogRead(audio1); // store left band reading
right[band] = analogRead(audio2); // ... and the right
digitalWrite(strobe, HIGH);
}
}
void convertSingle()
{
if (left[freq] > right[freq])
audio_input = left[freq];
else
audio_input = right[freq];
if (audio_input > 80)
{
pre_react = ((long)NUM_LEDS * (long)audio_input) / 1023L; // TRANSLATE AUDIO LEVEL TO NUMBER OF LEDs
if (pre_react > react) // ONLY ADJUST LEVEL OF LED IF LEVEL HIGHER THAN CURRENT LEVEL
react = pre_react;
Serial.print(audio_input);
Serial.print(" -> ");
Serial.println(pre_react);
}
}
void convertDouble()
{
if (left[freq] > right[freq])
audio_input = left[freq];
else
audio_input = right[freq];
if (audio_input > 80)
{
pre_react = ((long)midway * (long)audio_input) / 1023L; // TRANSLATE AUDIO LEVEL TO NUMBER OF LEDs
if (pre_react > react) // ONLY ADJUST LEVEL OF LED IF LEVEL HIGHER THAN CURRENT LEVEL
react = pre_react;
Serial.print(audio_input);
Serial.print(" -> ");
Serial.println(pre_react);
}
}
// FUNCTION TO VISUALIZE WITH A SINGLE LEVEL
void singleLevel()
{
readMSGEQ7();
convertSingle();
singleRainbow(); // APPLY COLOR
k = k - wheel_speed; // SPEED OF COLOR WHEEL
if (k < 0) // RESET COLOR WHEEL
k = 255;
// REMOVE LEDs
decay_check++;
if (decay_check > decay)
{
decay_check = 0;
if (react > 0)
react--;
}
}
// FUNCTION TO VISUALIZE WITH MIRRORED LEVELS
void doubleLevel()
{
readMSGEQ7();
convertDouble();
doubleRainbow();
k = k - wheel_speed; // SPEED OF COLOR WHEEL
if (k < 0) // RESET COLOR WHEEL
k = 255;
// REMOVE LEDs
decay_check++;
if (decay_check > decay)
{
decay_check = 0;
if (react > 0)
react--;
}
}
void loop()
{
//singleLevel();
doubleLevel();
//delay(1);
}