-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
8x32_matrix_music_visualizer_sparkfun_spectrum_shield.ino
194 lines (153 loc) · 4.14 KB
/
8x32_matrix_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
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#define PIN 6
// PANEL SETUP
int width = 32;
int height = 8;
int NUM_LEDS = width * height;
int band_width = 4;
// 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;
int react_arr[] = {0, 0, 0, 0, 0, 0, 0};
int prereact_arr[] = {0, 0, 0, 0, 0, 0, 0};
// STANDARD VISUALIZER VARIABLES
float modifier = 2.5; // CONTROLS SENSITIVITY OF THE LED REACTION
int loop_max = 0;
int k = 255; // COLOR WHEEL POSITION
int decay = 1; // 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;
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(width, height, PIN,
NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT +
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
NEO_GRB + NEO_KHZ800);
const uint16_t colors[] = {
matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(255, 255, 0),matrix.Color(0, 0, 255), matrix.Color(255, 0, 255), matrix.Color(0, 255, 255), matrix.Color(255, 255, 255)};
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) * modifier; // store left band reading
right[band] = analogRead(audio2) * modifier; // ... and the right
if (left[band] > 1023)
left[band] = 1023;
if (right[band] > 1023)
right[band] = 1023;
digitalWrite(strobe, HIGH);
}
}
uint32_t Scroll(int pos) {
int g;
int r;
int b;
if(pos < 85) {
g = 0;
r = ((float)pos / 85.0f) * 255.0f;
b = 255 - r;
} else if(pos < 170) {
g = ((float)(pos - 85) / 85.0f) * 255.0f;
r = 255 - g;
b = 0;
} else if(pos < 256) {
b = ((float)(pos - 170) / 85.0f) * 255.0f;
g = 255 - b;
r = 1;
}
return matrix.Color(g, r, b);
}
void singleRainbow()
{
int band_width = width / 7;
for(int x = 6, x2 = 0; x >= 0, x2 < 7; x--, x2++)
{
for(int i = height - 1; i >= 0; i--) {
for(int w = 1; w <= band_width; w++)
{
if (i < react_arr[x])
matrix.drawPixel((x2 * band_width) + w, i, Scroll((i * 256 / 50 + k) % 256));
//matrix.setPixelColor((8 * x) + i, Scroll((i * 256 / 50 + k) % 256));
else
matrix.drawPixel((x2 * band_width) + w, i, (0, 0, 0));
//matrix.setPixelColor((8 * x) + i, (0, 0, 0));
}
}
}
matrix.show();
}
void convertSingle(int i)
{
if (left[i] > right[i])
audio_input = left[i];
else
audio_input = right[i];
if (audio_input > 80 * modifier)
{
prereact_arr[i] = ((long)height * (long)audio_input) / 1023L; // TRANSLATE AUDIO LEVEL TO NUMBER OF LEDs
if (prereact_arr[i] > react_arr[i]) // ONLY ADJUST LEVEL OF LED IF LEVEL HIGHER THAN CURRENT LEVEL
react_arr[i] = prereact_arr[i];
Serial.print(audio_input);
Serial.print(" -> ");
Serial.println(prereact_arr[i]);
}
}
// FUNCTION TO VISUALIZE WITH A SINGLE LEVEL
void singleLevel()
{
readMSGEQ7();
for (int i = 0; i < 7; i++)
convertSingle(i);
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;
for (int x = 0; x < 7; x++)
{
if (react_arr[x] > 0)
react_arr[x]--;
}
}
}
void setup() {
// SPECTRUM SETUP
pinMode(audio1, INPUT);
pinMode(audio2, INPUT);
pinMode(strobe, OUTPUT);
pinMode(reset, OUTPUT);
digitalWrite(reset, LOW);
digitalWrite(strobe, HIGH);
matrix.begin();
matrix.setTextWrap(false);
matrix.setBrightness(5);
matrix.setTextColor(colors[0]);
// SERIAL AND INPUT SETUP
Serial.begin(115200);
Serial.println("\nListening...");
}
void loop()
{
singleLevel();
//doubleLevel();
//delay(1);
}