Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define MATRIX
- //
- #ifdef MATRIX
- #include <FastLED.h>
- #define LED_PIN 3
- #define COLOR_ORDER GRB
- #define CHIPSET WS2812
- #define BRIGHTNESS 64
- // Params for width and height
- const uint8_t kMatrixWidth = 8;
- const uint8_t kMatrixHeight = 32;
- // Param for different pixel layouts
- const bool kMatrixSerpentineLayout = true;
- #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
- CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
- CRGB* const leds( leds_plus_safety_pixel + 1);
- uint16_t XY( uint8_t x, uint8_t y)
- {
- uint16_t i;
- if( kMatrixSerpentineLayout == false) {
- i = (y * kMatrixWidth) + x;
- }
- if( kMatrixSerpentineLayout == true) {
- if( y & 0x01) {
- // Odd rows run backwards
- uint8_t reverseX = (kMatrixWidth - 1) - x;
- i = (y * kMatrixWidth) + reverseX;
- } else {
- // Even rows run forwards
- i = (y * kMatrixWidth) + x;
- }
- }
- return i;
- }
- #endif
- #include <Audio.h>
- ///////////////////////////////////
- // copy the Design Tool code here
- ///////////////////////////////////
- #include <Audio.h>
- #include <Wire.h>
- #include <SPI.h>
- #include <SD.h>
- #include <SerialFlash.h>
- // GUItool: begin automatically generated code
- AudioInputI2S i2s2; //xy=210,60
- AudioPlayMemory playMem1; //xy=261,210
- AudioSynthWaveform waveform1; //xy=284,269
- AudioMixer4 mixer1; //xy=450,196
- AudioOutputI2S i2s1; //xy=669,145
- AudioAnalyzeFFT1024 fft1024_1; //xy=677,201
- AudioConnection patchCord1(i2s2, 0, mixer1, 0);
- AudioConnection patchCord2(i2s2, 1, mixer1, 1);
- AudioConnection patchCord3(playMem1, 0, mixer1, 2);
- AudioConnection patchCord4(waveform1, 0, mixer1, 3);
- AudioConnection patchCord5(mixer1, 0, i2s1, 0);
- AudioConnection patchCord6(mixer1, 0, i2s1, 1);
- AudioConnection patchCord7(mixer1, fft1024_1);
- AudioControlSGTL5000 sgtl5000_1; //xy=526,301
- // GUItool: end automatically generated code
- int matrix_height = 8;
- int matrix_width = 32;
- const int lowerFFTBins[] = {0,1,2,3,4,5,6,8,10,12,15,18,22,27,32,38,45,53,63,74,87,102,119,138,160,186,216,250,289,334,385,444};
- const int upperFFTBins[] = {0,1,2,3,4,5,7,9,11,14,17,21,26,31,37,44,52,62,73,86,101,118,137,159,185,215,249,288,333,384,443,511};
- // Use these with the Teensy Audio Shield
- #define SDCARD_CS_PIN 10
- #define SDCARD_MOSI_PIN 7
- #define SDCARD_SCK_PIN 14
- // Use these with the Teensy 3.5 & 3.6 SD card
- //#define SDCARD_CS_PIN BUILTIN_SDCARD
- //#define SDCARD_MOSI_PIN 11 // not actually used
- //#define SDCARD_SCK_PIN 13 // not actually used
- // Use these for the SD+Wiz820 or other adaptors
- //#define SDCARD_CS_PIN 4
- //#define SDCARD_MOSI_PIN 11
- //#define SDCARD_SCK_PIN 13
- const int myInput = AUDIO_INPUT_LINEIN;
- void setup() {
- Serial.begin(57600);
- #ifdef MATRIX
- FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
- FastLED.setBrightness( BRIGHTNESS );
- for (int i=0;i<NUM_LEDS;i++) {
- leds[i] = CRGB( 0, 0, 64);
- delay(5);
- FastLED.show();
- }
- for (int i=0;i<NUM_LEDS;i++) {
- leds[i] = CRGB( 0, 0, 0);
- }
- delay(100);
- FastLED.show();
- #endif
- AudioMemory(12);
- sgtl5000_1.enable();
- sgtl5000_1.volume(0.5);
- // SPI.setMOSI(SDCARD_MOSI_PIN);
- // SPI.setSCK(SDCARD_SCK_PIN);
- // if (!(SD.begin(SDCARD_CS_PIN))) {
- // while (1) {
- // Serial.println("Unable to access the SD card");
- // delay(500);
- // }
- // }
- // Enable audio in
- sgtl5000_1.enable();
- sgtl5000_1.inputSelect(myInput);
- sgtl5000_1.volume(1);
- mixer1.gain(0, 1.0);
- mixer1.gain(1, 1.0);
- mixer1.gain(2, 0.0);
- mixer1.gain(3, 0.0);
- // Uncomment one these to try other window functions
- // fft1024_1.windowFunction(NULL);
- // fft1024_1.windowFunction(AudioWindowBartlett1024);
- // fft1024_1.windowFunction(AudioWindowFlattop1024);
- fft1024_1.windowFunction(AudioWindowHanning1024);
- delay(2000);
- //playSdWav1.play("SDTEST2.WAV");
- }
- void loop() {
- if (fft1024_1.available()) {
- for(int x=0; x< matrix_width ; x++){
- spectrum(x, fft1024_1.read(lowerFFTBins[x],upperFFTBins[x]));
- }
- #ifdef MATRIX
- FastLED.show();
- #endif
- }
- #ifdef MATRIX
- for (int i=0;i<NUM_LEDS;i++) {
- leds[i] = CHSV( 0, 0, 0);
- }
- delay(10);
- FastLED.show();
- #endif
- } // end of loop()
- void spectrum(int x, float n) {
- // x = band
- // y = led height
- int y = LEDnumber(n);
- #ifdef MATRIX
- // use CHSV rainbow colours
- switch (y) {
- case 0:
- leds[XY(y,x)] = CHSV( 224, 255, 255);
- break;
- case 1:
- leds[XY(y,x)] = CHSV( 192, 255, 255);
- break;
- case 2:
- leds[XY(y,x)] = CHSV( 160, 255, 255);
- break;
- case 3:
- leds[XY(y,x)] = CHSV( 128, 255, 255);
- break;
- case 4:
- leds[XY(y,x)] = CHSV( 96, 255, 255);
- break;
- case 5:
- leds[XY(y,x)] = CHSV( 64, 255, 255);
- break;
- case 6:
- leds[XY(y,x)] = CHSV( 32, 255, 255);
- break;
- case 7:
- leds[XY(y,x)] = CHSV( 0, 255, 128);
- break;
- default:
- break;
- }
- #endif
- Serial.print(x);
- Serial.print(",");
- Serial.print(y);
- Serial.println();
- }
- int LEDnumber(float n) {
- int led = 0;
- if (n > 0.250) {
- led = 0;
- } else if ( n > 0.220) {
- led = 0;
- } else if ( n > 0.180) {
- led = 1;
- } else if ( n > 0.100) {
- led = 2;
- } else if ( n > 0.060) {
- led = 3;
- } else if ( n > 0.030) {
- led = 4;
- } else if ( n > 0.010) {
- led = 5;
- } else if ( n > 0.008) {
- led = 6;
- } else if ( n < 0.009) {
- led = 7;
- }
- return led;
- }
Advertisement
Add Comment
Please, Sign In to add comment