FastLED
3.9.15
Loading...
Searching...
No Matches
PJRCSpectrumAnalyzer.h
Go to the documentation of this file.
1
2
6
7
// LED Audio Spectrum Analyzer Display
8
//
9
// Creates an impressive LED light show to music input
10
// using Teensy 3.1 with the OctoWS2811 adaptor board
11
// http://www.pjrc.com/store/teensy31.html
12
// http://www.pjrc.com/store/octo28_adaptor.html
13
//
14
// Line Level Audio Input connects to analog pin A3
15
// Recommended input circuit:
16
// http://www.pjrc.com/teensy/gui/?info=AudioInputAnalog
17
//
18
// This example code is in the public domain.
19
20
#define USE_OCTOWS2811
21
#include <OctoWS2811.h>
22
#include <
FastLED.h
>
23
#include <
Audio.h
>
24
#include <Wire.h>
25
#include <SD.h>
26
#include <SPI.h>
27
28
// The display size and color to use
29
const
unsigned
int
matrix_width
= 60;
30
const
unsigned
int
matrix_height
= 32;
31
const
unsigned
int
myColor
= 0x400020;
32
33
// These parameters adjust the vertical thresholds
34
const
float
maxLevel
= 0.5;
// 1.0 = max, lower is more "sensitive"
35
const
float
dynamicRange
= 40.0;
// total range to display, in decibels
36
const
float
linearBlend
= 0.3;
// useful range is 0 to 0.7
37
38
CRGB
leds
[
matrix_width
*
matrix_height
];
39
40
// Audio library objects
41
AudioInputAnalog
adc1
(A3);
//xy=99,55
42
AudioAnalyzeFFT1024
fft
;
//xy=265,75
43
AudioConnection
patchCord1
(
adc1
,
fft
);
44
45
46
// This array holds the volume level (0 to 1.0) for each
47
// vertical pixel to turn on. Computed in setup() using
48
// the 3 parameters above.
49
float
thresholdVertical
[
matrix_height
];
50
51
// This array specifies how many of the FFT frequency bin
52
// to use for each horizontal pixel. Because humans hear
53
// in octaves and FFT bins are linear, the low frequencies
54
// use a small number of bins, higher frequencies use more.
55
int
frequencyBinsHorizontal
[
matrix_width
] = {
56
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
57
2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
58
3, 3, 3, 3, 4, 4, 4, 4, 4, 5,
59
5, 5, 6, 6, 6, 7, 7, 7, 8, 8,
60
9, 9, 10, 10, 11, 12, 12, 13, 14, 15,
61
15, 16, 17, 18, 19, 20, 22, 23, 24, 25
62
};
63
64
65
66
// Run setup once
67
void
setup
() {
68
// the audio library needs to be given memory to start working
69
AudioMemory(12);
70
71
// compute the vertical thresholds before starting
72
computeVerticalLevels
();
73
74
// turn on the display
75
FastLED
.addLeds<
OCTOWS2811
>(
leds
,(
matrix_width
*
matrix_height
) / 8);
76
}
77
78
// A simple xy() function to turn display matrix coordinates
79
// into the index numbers OctoWS2811 requires. If your LEDs
80
// are arranged differently, edit this code...
81
unsigned
int
xy
(
unsigned
int
x
,
unsigned
int
y
) {
82
if
((
y
& 1) == 0) {
83
// even numbered rows (0, 2, 4...) are left to right
84
return
y
*
matrix_width
+
x
;
85
}
else
{
86
// odd numbered rows (1, 3, 5...) are right to left
87
return
y
*
matrix_width
+
matrix_width
- 1 -
x
;
88
}
89
}
90
91
// Run repetitively
92
void
loop
() {
93
unsigned
int
x
,
y
, freqBin;
94
float
level;
95
96
if
(
fft
.available()) {
97
// freqBin counts which FFT frequency data has been used,
98
// starting at low frequency
99
freqBin = 0;
100
101
for
(
x
=0;
x
<
matrix_width
;
x
++) {
102
// get the volume for each horizontal pixel position
103
level =
fft
.read(freqBin, freqBin +
frequencyBinsHorizontal
[
x
] - 1);
104
105
// uncomment to see the spectrum in Arduino's Serial Monitor
106
// Serial.print(level);
107
// Serial.print(" ");
108
109
for
(
y
=0;
y
<
matrix_height
;
y
++) {
110
// for each vertical pixel, check if above the threshold
111
// and turn the LED on or off
112
if
(level >=
thresholdVertical
[
y
]) {
113
leds
[
xy
(
x
,
y
)] =
CRGB
(
myColor
);
114
}
else
{
115
leds
[
xy
(
x
,
y
)] =
CRGB::Black
;
116
}
117
}
118
// increment the frequency bin count, so we display
119
// low to higher frequency from left to right
120
freqBin = freqBin +
frequencyBinsHorizontal
[
x
];
121
}
122
// after all pixels set, show them all at the same instant
123
FastLED
.show();
124
// Serial.println();
125
}
126
}
127
128
129
// Run once from setup, the compute the vertical levels
130
void
computeVerticalLevels
() {
131
unsigned
int
y
;
132
float
n, logLevel, linearLevel;
133
134
for
(
y
=0;
y
<
matrix_height
;
y
++) {
135
n = (float)
y
/ (
float
)(
matrix_height
- 1);
136
logLevel = pow10f(n * -1.0 * (
dynamicRange
/ 20.0));
137
linearLevel = 1.0 - n;
138
linearLevel = linearLevel *
linearBlend
;
139
logLevel = logLevel * (1.0 -
linearBlend
);
140
thresholdVertical
[
y
] = (logLevel + linearLevel) *
maxLevel
;
141
}
142
}
leds
CRGB leds[NUM_LEDS]
Definition
AdafruitBridge.ino:13
y
int y
Definition
simple.h:93
x
int x
Definition
simple.h:92
FastLED
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
Definition
FastLED.cpp:74
OCTOWS2811
@ OCTOWS2811
Definition
FastLED.h:139
FastLED.h
central include file for FastLED, defines the CFastLED class/object
thresholdVertical
float thresholdVertical[matrix_height]
Definition
PJRCSpectrumAnalyzer.h:49
patchCord1
AudioConnection patchCord1(adc1, fft)
matrix_height
const unsigned int matrix_height
Definition
PJRCSpectrumAnalyzer.h:30
xy
unsigned int xy(unsigned int x, unsigned int y)
Definition
PJRCSpectrumAnalyzer.h:81
linearBlend
const float linearBlend
Definition
PJRCSpectrumAnalyzer.h:36
setup
void setup()
Definition
PJRCSpectrumAnalyzer.h:67
myColor
const unsigned int myColor
Definition
PJRCSpectrumAnalyzer.h:31
dynamicRange
const float dynamicRange
Definition
PJRCSpectrumAnalyzer.h:35
fft
AudioAnalyzeFFT1024 fft
Definition
PJRCSpectrumAnalyzer.h:42
matrix_width
const unsigned int matrix_width
Definition
PJRCSpectrumAnalyzer.h:29
frequencyBinsHorizontal
int frequencyBinsHorizontal[matrix_width]
Definition
PJRCSpectrumAnalyzer.h:55
maxLevel
const float maxLevel
Definition
PJRCSpectrumAnalyzer.h:34
adc1
AudioInputAnalog adc1(A3)
computeVerticalLevels
void computeVerticalLevels()
Definition
PJRCSpectrumAnalyzer.h:130
loop
void loop()
Definition
PJRCSpectrumAnalyzer.h:92
audio.h
CRGB::Black
@ Black
<div style='background:#000000;width:4em;height:4em;'></div>
Definition
crgb.h:567
CRGB
Representation of an RGB pixel (Red, Green, Blue)
Definition
crgb.h:86
examples
Ports
PJRCSpectrumAnalyzer
PJRCSpectrumAnalyzer.h
Generated on Fri Aug 22 2025 20:59:33 for FastLED by
1.13.2