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