FastLED 3.9.3
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
27const unsigned int matrix_width = 60;
28const unsigned int matrix_height = 32;
29const unsigned int myColor = 0x400020;
30
31// These parameters adjust the vertical thresholds
32const float maxLevel = 0.5; // 1.0 = max, lower is more "sensitive"
33const float dynamicRange = 40.0; // total range to display, in decibels
34const float linearBlend = 0.3; // useful range is 0 to 0.7
35
36CRGB leds[matrix_width * matrix_height];
37
38// Audio library objects
39AudioInputAnalog adc1(A3); //xy=99,55
40AudioAnalyzeFFT1024 fft; //xy=265,75
41AudioConnection 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.
47float 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.
53int 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
65void 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...
79unsigned 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
90void 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
128void 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}
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:33
central include file for FastLED, defines the CFastLED class/object
void show(uint8_t scale)
Update all our controllers with the current led colors, using the passed in brightness.
Definition FastLED.cpp:82
static CLEDController & addLeds(CLEDController *pLed, struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset=0)
Add a CLEDController instance to the world.
Definition FastLED.cpp:67
@ Black
Definition crgb.h:479
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:39