Advertisement
atuline

FHT, Arduino and FastLED work in progress

Oct 26th, 2014
1,570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1.  
  2. /* fht_min.ino
  3.  
  4. https://github.com/TJC/arduino/blob/master/fhttest/fhttest.cpp
  5.  
  6. */
  7.  
  8.  
  9. #define qsubd(x, b) ((x>b)?wavebright:0)                     // A digital unsigned subtraction macro. if result <0, then => 0. Otherwise, take on fixed value.
  10. #define qsuba(x, b) ((x>b)?x-b:0)                            // Unsigned subtraction macro. if result <0, then => 0.
  11.  
  12. #define wavebright 128                                        // qsubd result will be this value if subtraction is >0.
  13.  
  14. #include "FastLED.h"                                          // FastLED library. Preferably the latest copy of FastLED 2.1.
  15.  
  16. // Fixed definitions cannot change on the fly.
  17. #define LED_DT 13                                             // Data pin to connect to the strip.
  18. #define COLOR_ORDER GRB                                       // Are they RGB, GRB or what??
  19. #define LED_TYPE WS2812B                                       // What kind of strip are you using?
  20. #define NUM_LEDS 15                                           // Number of LED's.
  21.  
  22. // Initialize changeable global variables.
  23. uint8_t max_bright = 128;                                     // Overall brightness definition. It can be changed on the fly.
  24.  
  25. struct CRGB leds[NUM_LEDS];                                   // Initialize our LED array.
  26.  
  27.  
  28.  
  29.  
  30. #define LOG_OUT 1
  31. // #define OCT_NORM 1
  32. // #define OCTAVE 1
  33.  
  34.  
  35. #define FHT_N 256 // set to 256 point fht
  36. #define inputPin A5
  37.  
  38. #include <FHT.h> // include the library
  39.  
  40. uint8_t D[NUM_LEDS][12];                                      // Our initial samples used to normalize the output.
  41. uint8_t E[NUM_LEDS];                                          // Our normalized values.
  42.  
  43. uint8_t hueinc;                                               // A hue increment value to make it rotate a bit.
  44. uint8_t micmult = 10;
  45.  
  46.  
  47. void setup() {
  48.   analogReference(EXTERNAL);
  49.   Serial.begin(57600); // use the serial port
  50.   LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  51.   FastLED.setBrightness(max_bright);
  52.  
  53.   for (int k = 0; k<12; k++) {                                // Let's get some initial FHT samples.
  54.     GetFHT();                                                 // Get 'em fast
  55.     for (int i=0; i<NUM_LEDS; i++){
  56.  
  57.       D[i][k] = fht_log_out[2*i+2];
  58. //        D[i][k] = fht_oct_out[2*i+2];
  59.  
  60.       Serial.print(fht_log_out[2*i+2]);
  61. //      Serial.print(fht_oct_out[2*i+2]);
  62.  
  63.       Serial.print(", ");
  64.     }
  65.     Serial.println();
  66.   }
  67.  
  68.   for (int m = 0; m <NUM_LEDS; m++) {                         // We need to average out those samples to mellow out the noise.
  69.       E[m] = (D[m][0] + D[m][1] + D[m][2] + D[m][3] + D[m][4] + D[m][5] + D[m][6] + D[m][7] + D[m][8] + D[m][9] + D[m][10] + D[m][11])/12*1.75;      // Let's average and 'fudge' the noise limiter.
  70.       Serial.print(E[m]);
  71.       Serial.print(", ");
  72.   }
  73. }
  74.  
  75. void loop() {
  76.   fhtsound();
  77.   FastLED.show();
  78. }  
  79.  
  80.  
  81. void fhtsound() {
  82.   hueinc++;                                                   // A cute little hue incrementer.
  83.   GetFHT();                                                   // Let's take FHT_N samples and crunch 'em.
  84. //  for (int i = 0 ; i < FHT_N/2 ; i++)                       // You can process up to FHT_N/2 of them. Hmm, Nyquist.
  85. //    B[i] = fht_log_out[i];
  86.  
  87.   for (int i= 0; i < NUM_LEDS; i++) {                         // Run through the LED array.
  88. //        Serial.print(fht_oct_out[2*i+2]);
  89. //        Serial.print(", ");
  90.  
  91. //  int tmp = qsuba(fht_oct_out[2*i+2], E[i]);                // Get the sample and subtract the 'quiet' normalized values, but don't go < 0.
  92.   int tmp = qsuba(fht_log_out[2*i+2], E[i]);                // Get the sample and subtract the 'quiet' normalized values, but don't go < 0.
  93.  
  94.     if (tmp > (leds[i].r + leds[i].g + leds[i].b))            // Refresh an LED only when the intensity is low
  95.         leds[i] = CHSV(tmp*micmult+hueinc, 255, tmp*micmult);           // Note how we really cranked up the tmp value to get BRIGHT LED's. Also increment the hue for fun.
  96.     leds[i].nscale8(224);                                     // Let's fade the whole thing over time as well.
  97.   }
  98. //          Serial.println();
  99. } // fhtsound()
  100.  
  101.  
  102.  
  103. void GetFHT() {
  104. //  Serial.println("GetFHT");
  105.   cli();
  106.   for (int i = 0 ; i < FHT_N ; i++) {
  107.     fht_input[i] = analogRead(inputPin) - 512;
  108. //    Serial.print(fht_input[i]);
  109. //    Serial.print(", ");
  110.   }
  111.   sei();
  112.   Serial.println();
  113.     // save 256 samples
  114.   fht_window();                                               // Window the data for better frequency response.
  115.   fht_reorder();                                              // Reorder the data before doing the fht.
  116.   fht_run();                                                  // Process the data in the fht.
  117. //  fht_mag_octave();                                              // Take the output of the fht and put in an array called fht_log_out[FHT_N/2]
  118.   fht_mag_log();
  119. } // GetFHT()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement