Arduino FFT Audio Spectrum analyzer on 8×32 shade matrix WS2
/* Copyright (c) 2020 Janux Permission is hereby granted, freed from cost, to any individual acquiring a duplicate of this software program and related documentation information (the "Software"), to deal within the Software with out restriction, together with with out limitation the rights to make use of, copy, modify, merge, publish, distribute, sublicense, and/or promote copies of the Software, and to allow individuals to whom the Software is furnished to take action, topic to the next circumstances: The above copyright discover and this permission discover shall be included in all copies or substantial parts of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Based on an authentic undertaking for the MAX72xx LED matrix and FFT lib comprised of Shajeeb. Configuration settings part primarily based on work of Ragnar Ranøyen Homb from Norvegian Creation. */ #outline LIN_OUT 1 //FHT linear output magnitude #outline FHT_N 128 //set SAMPLES for FHT, Must be an influence of two #embody <FHT.h> #outline xres 32 //Total variety of columns within the show, should be <= SAMPLES/2 #outline yres 8 //Total variety of rows within the show #outline ledPIN 6 //out pint to manage Leds #outline NUM_LEDS (xres * yres) //complete leds in Matrix #embody <Adafruit_NeoPixel.h> #embody <Adafruit_NeoMatrix.h> #outline colorPIN 5 //pin to alter ledcolor #outline brightnessPIN 10 //pin to alter brightness byte displaycolor = 0; //default shade worth byte brightness = 1; //default brightness stage #embody <EEPROM.h> #outline CONFIG_START 32 //Memory begin location #outline CONFIG_VERSION "VER01" //Config model configuration typedef struct { char model[6]; byte displaycolor; byte brightness; } configuration_type; configuration_type CONFIGURATION = { CONFIG_VERSION, displaycolor, brightness }; byte yvalue; int peaks[xres]; byte state = HIGH; // the present studying from the enter pin byte previousState = LOW; // the earlier studying from the enter pin unsigned lengthy lastDebounceTime = 0; // the final time the output pin was toggled unsigned lengthy debounceDelay = 100; // the debounce time; enhance if the output glints byte data_avgs[xres]; //Array for samplig // Parameter 1 = variety of leds in matrix // Parameter 2 = pin quantity // Parameter 3 = pixel kind flags, add collectively as wanted: // NEO_KHZ800 800 KHz bitstream (most NeoPixel merchandise w/WS2812 LEDs) // NEO_KHZ400 400 KHz (traditional 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel merchandise) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) Adafruit_NeoPixel pixel = Adafruit_NeoPixel(NUM_LEDS, ledPIN, NEO_GRB + NEO_KHZ800); // EQ filter byte eq[32] = { 60, 65, 70, 75, 80, 85, 90, 95, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 115, 125, 140, 160, 185, 200, 200, 200 }; bool EQ_ON = true; // set to false to disable eq //Define 5 set of colours for leds, 0 for single customized shade byte colours[][8] = { {170, 160, 150, 140, 130, 120, 1, 1}, {1, 5, 10, 15, 20, 25, 90, 90}, {90, 85, 80, 75, 70, 65, 1, 1}, {90, 90, 90, 30, 30, 30, 1, 1}, {170, 160, 150, 140, 130, 120, 110, 0} }; //Define chars for show settings byte charBitmap[] = { 0x1C, 0x10, 0x10, 0x10, 0x10, 0x1C, 0x08, 0x18, 0x08, 0x08, 0x08, 0x1C, 0x0C, 0x12, 0x04, 0x08, 0x10, 0x1E, 0x0C, 0x12, 0x02, 0x06, 0x12, 0x0C, 0x10, 0x10, 0x10, 0x14, 0x1E, 0x04, 0x1E, 0x10, 0x1E, 0x02, 0x12, 0x0C, 0x1E, 0x10, 0x10, 0x1E, 0x12, 0x1E, 0x1E, 0x02, 0x04, 0x08, 0x08, 0x08, 0x0C, 0x12, 0x0C, 0x12, 0x12, 0x0C, 0x1C, 0x12, 0x1C, 0x12, 0x12, 0x1C }; void setup() { pixel.start(); //initialize Led Matrix //Begin FFT operations ADCSRA = 0b11100101; // set ADC to free working mode and set pre-scaler to 32 (0xe5) ADMUX = 0b00000000; // use pin A0 and exterior voltage reference // Read config information from EEPROM if (loadConfig()) { displaycolor = CONFIGURATION.displaycolor; brightness = CONFIGURATION.brightness; } //Set brightness loaded from EEPROM pixel.setBrightness(brightness * 24 + 8); //Show present config on begin //change true to false if you do not need this presentSettings(3, true); } void loop() { whereas (1) { // reduces jitter Sampling(); // FHT Library use just one information array RearrangeFHT(); // re-arrange FHT end result to match with no. of show columns SendToDisplay(); // ship to show in accordance measured worth colorChange(); // test if button pressed to alter shade brightnessChange(); // test if button pressed to alter brightness delay(10); // delay to scale back flickering (FHT is just too quick :D) } } void Sampling() { for (int i = 0; i < FHT_N; i++) m; // kind into an int worth -= 0x0200; // kind right into a signed int worth <<= 6; // kind right into a 16b signed int fht_input[i] = worth / 8; // copy to fht enter array after compressing // ++ start FHT information course of -+-+--+-+--+-+--+-+--+-+--+-+--+-+- fht_window(); // window the information for higher frequency response fht_reorder(); // reorder the information earlier than doing the fht fht_run(); // course of the information within the fht fht_mag_lin(); // take the output of the fht } void RearrangeFHT() { // FHT return actual worth unsing just one array // after fht_mag_lin() calling the samples worth are in // the primary FHT_N/2 place of the array fht_lin_out[] int step = (FHT_N / 2) / xres; int c = 0; for (int i = 0; i < (FHT_N / 2); i += step) { data_avgs[c] = 0; for (int okay = 0 ; okay < step ; okay++) { data_avgs[c] = data_avgs[c] + fht_lin_out[i + k]; // linear output magnitude } data_avgs[c] = data_avgs[c] / step ; // save avgs worth c++; } } void SendToDisplay() { for (int i = 0; i < xres; i++) { if (EQ_ON) data_avgs[i] = data_avgs[i] * (float)(eq[i]) / 100; // apply eq filter data_avgs[i] = constrain(data_avgs[i], 0, 80); // set max & min values for buckets to 0-80 data_avgs[i] = map(data_avgs[i], 0, 80, 0, yres); // remap averaged values to yres 0-8 yvalue = data_avgs[i]; peaks[i] = peaks[i] - 1; // decay by one mild if (yvalue > peaks[i]) peaks[i] = yvalue; // save peak if > previuos peak yvalue = peaks[i]; // choose peak to show setColumn(i, yvalue); // draw columns } pixel.present(); // present column } // Light up leds of x column in keeping with y worth void setColumn(byte x, byte y) { int led, i; for (i = 0; i < yres; i++) { led = GetLedFromMatrix(x, i); //retrieve present led by x,y coordinates if (peaks[x] > i) { swap (displaycolor) { case 4: if (colours[displaycolor][i] == 0) { // present customized shade with zero worth in array pixel.setPixelColor(led, 255, 255, 255); //withe } else { // normal shade outlined in colours array pixel.setPixelColor(led, Wheel(colours[displaycolor][i])); } break; case 5: //change shade by column pixel.setPixelColor(led, Wheel(x * 16)); break; case 6: //change shade by row pixel.setPixelColor(led, Wheel(i * y * 3)); break; case 7: //change shade by... nation :D //Italy flagh //if (x < 11) pixel.setPixelColor(led, 0, 255, 0); //if (x > 10 && x < 21) pixel.setPixelColor(led, 255, 255, 255); //if (x > 20) pixel.setPixelColor(led, 255, 0, 0); //stars and stripes if (i < yres - 2) { if (x & 0x01) { pixel.setPixelColor(led, 0, 0, 255); } else { pixel.setPixelColor(led, 255, 0, 0); } } else { pixel.setPixelColor(led, 255, 255, 255); } break; default: //show colours outlined in shade array pixel.setPixelColor(led, Wheel(colours[displaycolor][i])); } //END SWITCH } else { //Light off leds pixel.setPixelColor(led, pixel.Color(0, 0, 0)); } } } //================================================================ // Calculate a led quantity by x,y coordinates // legitimate for WS2812B with serpentine structure positioned in horizzontal // and nil led at backside proper (DIN connector on the precise aspect) // enter worth: x= 0 to xres-1 , y= 0 to yres-1 // return a led quantity from 0 to NUM_LED //================================================================ int GetLedFromMatrix(byte x, byte y) { int led; x = xres - x - 1; if (x & 0x01) { //Odd columns enhance backwards led = ((x + 1) * yres - y - 1); } else { //Even columns enhance usually led = ((x + 1) * yres - yres + y); } return constrain(led, 0, NUM_LEDS); } //================================================================ void colorChange() { int studying = digitalRead(colorPIN); if (studying == HIGH && previousState == LOW && millis() - lastDebounceTime > debounceDelay) { displaycolor++; if (displaycolor > 7) displaycolor = 0; presentSettings(1, true); //set to false if you do not need this saveConfig(); lastDebounceTime = millis(); } previousState = studying; } void brightnessChange() { int studying = digitalRead(brightnessPIN); if (studying == HIGH && previousState == LOW && millis() - lastDebounceTime > debounceDelay) { brightness++; if (brightness > 7) brightness = 0; pixel.setBrightness(brightness * 24 + 8); presentSettings(2, true); //set to false if you do not need this saveConfig(); lastDebounceTime = millis(); } previousState = studying; } // Utility from Adafruit Neopixel demo sketch // Input a worth 0 to 255 to get a shade worth. // The colors are a transition R - G - B - again to R. unsigned lengthy Wheel(byte WheelPos) { WheelPos = 255 - WheelPos; if (WheelPos < 85) { return pixel.Color(255 - WheelPos * 3, 0, WheelPos * 3); } if (WheelPos < 170) { WheelPos -= 85; return pixel.Color(0, WheelPos * 3, 255 - WheelPos * 3); } WheelPos -= 170; return pixel.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } // load whats in EEPROM in to the native CONFIGURATION if it's a legitimate setting int loadConfig() { if (EEPROM.learn(CONFIG_START + 0) == CONFIG_VERSION[0] && EEPROM.learn(CONFIG_START + 1) == CONFIG_VERSION[1] && EEPROM.learn(CONFIG_START + 2) == CONFIG_VERSION[2] && EEPROM.learn(CONFIG_START + 3) == CONFIG_VERSION[3] && EEPROM.learn(CONFIG_START + 4) == CONFIG_VERSION[4]) { // load (overwrite) the native configuration struct for (unsigned int i = 0; i < sizeof(CONFIGURATION); i++) { *((char*)&CONFIGURATION + i) = EEPROM.learn(CONFIG_START + i); } return 1; // return 1 if config loaded } return 0; // return 0 if config NOT loaded } // save the CONFIGURATION in to EEPROM void saveConfig() { CONFIGURATION.displaycolor = displaycolor; CONFIGURATION.brightness = brightness; for (unsigned int i = 0; i < sizeof(CONFIGURATION); i++) EEPROM.write(CONFIG_START + i, *((char*)&CONFIGURATION + i)); } // 1 show shade stage, 2 show brightness stage, 3 each void presentSettings(byte num, bool present) { if (present) { pixel.clear(); if (num == 1 || num == 3) { drawChar(0, 0); drawChar(displaycolor + 1, 5); } if (num == 2 || num == 3) { drawChar(9, xres - 9); drawChar(brightness + 1, xres - 4); } delay(1000); pixel.clear(); } } // Draw customized chars void drawChar(byte val, byte pos) { for (int x = 4; x >= 0; x--) { for (int y = 5; y >= 0; y--) { if ((charBitmap[val * 6 + 5 - y] >> x) & 0x01) { pixel.setPixelColor(GetLedFromMatrix(4 - x + pos, y + 1), Wheel((pos > 10) * 170)); pixel.present(); } } } } //by Janux®, Last model on 28/06/2020.
HI-FI News
through Explore all initiatives – Page 1 – Hackster.io https://www.hackster.io
April 13, 2024 at 01:06PM
-
Product on sale
Audiophile Vinyl Records Cleaning BundleOriginal price was: €69.90.€59.90Current price is: €59.90. incl. VAT -
Product on sale
Easy Start Vinyl Records Cleaning KitOriginal price was: €69.90.€59.90Current price is: €59.90. incl. VAT -
Vinyl Records Cleaner Easy Groove Concentrate€34.95 incl. VAT -
Easy Groove Super Set€199.00 incl. VAT -
Easy Groove Enzycaster – vinyl records prewash cleaner€34.95 incl. VAT -
Easy Groove Extreme MK. II- vinyl records prewash cleaner€45.00 incl. VAT -
Vinyl Records Cleaner – Easy Groove Virgin Concentrate€35.00 incl. VAT -
Easy Groove Spray&Wipe vinyl records cleaner€34.95 incl. VAT








