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