Guest User

Untitled

a guest
May 22nd, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <FastLED.h>
  2. #include <elapsedMillis.h>
  3. #include <Audio.h>
  4. #include <Wire.h>
  5. #include <SPI.h>
  6. #include <SD.h>
  7. #include <SerialFlash.h>
  8.  
  9. #define SDCARD_CS_PIN    10
  10. #define SDCARD_MOSI_PIN  7
  11. #define SDCARD_SCK_PIN   14
  12.  
  13. #define LED_PIN 8
  14. #define NUM_LEDS 20
  15. #define BRIGHTNESS 255
  16. #define LED_TYPE    WS2811
  17. #define COLOR_ORDER GRB
  18.  
  19. AudioPlaySdWav           wavPlayer;
  20. AudioAnalyzeFFT1024      myFFT;
  21. AudioAnalyzePeak         peak;
  22. AudioMixer4              mixer;
  23. AudioOutputI2S           audioOutput;
  24. AudioConnection          patchCord1(wavPlayer, 0, mixer, 0);
  25. AudioConnection          patchCord2(wavPlayer, 0, audioOutput, 0);
  26. AudioConnection          patchCord3(wavPlayer, 1, mixer, 1);
  27. AudioConnection          patchCord4(wavPlayer, 1, audioOutput, 1);
  28. AudioConnection          patchCord5(mixer, peak);
  29. AudioConnection          patchCord6(mixer, myFFT);
  30. AudioControlSGTL5000     sgtl5000_1;
  31.  
  32. boolean isPlaying = false;
  33. float currentPeak = 0.0;
  34. float frequencyBands[3];
  35. int numPeaks = 2;
  36.  
  37. CRGB leds[NUM_LEDS];
  38.  
  39. int current = 0;
  40.  
  41. CRGBPalette16 palette;
  42. TBlendType currentBlending;
  43.  
  44. elapsedMillis rms;
  45. elapsedMillis fps;
  46.  
  47. void setup() {
  48.   AudioMemory(12);
  49.   sgtl5000_1.enable();
  50.   sgtl5000_1.volume(0.25);
  51.  
  52.   delay(1000);
  53.  
  54.   SPI.setMOSI(SDCARD_MOSI_PIN);
  55.   SPI.setSCK(SDCARD_SCK_PIN);
  56.   if (!(SD.begin(SDCARD_CS_PIN))) {
  57.     while (1) {
  58.       Serial.println("Unable to access the SD card");
  59.       delay(500);
  60.     }
  61.   }
  62.  
  63.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  64.   FastLED.setBrightness(  BRIGHTNESS );
  65. }
  66.  
  67. void playFile(char *filename){
  68.   wavPlayer.play(filename);
  69. }
  70.  
  71. void checkAudio(){
  72.   if(rms > 30){
  73.     if(peak.available()){
  74.       rms=0;
  75.       currentPeak = peak.read();
  76.     }
  77.   }
  78.   if(myFFT.available()){
  79.       frequencyBands[0] = myFFT.read(0, 10)*255;
  80.       frequencyBands[1] = myFFT.read(6, 32)*210;
  81.       frequencyBands[2] = myFFT.read(16, 360)*30;
  82.   }
  83.     int rOut = (int) frequencyBands[0];
  84.     rOut = min(rOut, 255);
  85.     int gOut = (int) frequencyBands[1];
  86.     gOut = min(gOut, 255);
  87.     int bOut = (int) frequencyBands[2];
  88.     bOut = min(bOut, 255);
  89.     pushToLEDs(currentPeak, rOut, gOut, bOut);
  90. }
  91.  
  92. void pushToLEDs(float currentPeak, int r, int g, int b){
  93.   int numberOfLEDs = (int) (currentPeak*(NUM_LEDS+.1));
  94.   for(int i=0; i<NUM_LEDS/2; i++){
  95.     if(i<currentPeak){
  96.       leds[NUM_LEDS/2+i]=CRGB(r,g,b);
  97.       leds[NUM_LEDS/2-i]=CRGB(r,g,b);
  98.     }
  99.   }
  100. }
  101.  
  102. void updateLEDs(){
  103.   blur1d(leds, NUM_LEDS, 100);
  104.   FastLED.show();
  105.   fadeToBlackBy(leds, NUM_LEDS, 25);
  106. }
  107.  
  108. void loop() {
  109.   if(!isPlaying){
  110.     delay(1000);
  111.     playFile("MOON.wav");
  112.     isPlaying = true;
  113.   }
  114.   checkAudio();
  115.   updateLEDs();
  116. }
Advertisement
Add Comment
Please, Sign In to add comment