Advertisement
atuline

fht_log updated without beat detection added.

Aug 11th, 2015
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.94 KB | None | 0 0
  1.  
  2. /* Work just starting for: Fast Hartley Transform with beat detection for FastLED.
  3.  
  4. By: Andrew Tuline
  5.  
  6. Date: Aug, 2015
  7.  
  8.  
  9. This is an implementation of the FHT library with FastLED 3.1
  10.  
  11. FHT is available at http://wiki.openmusiclabs.com/wiki/ArduinoFHT
  12.  
  13.  
  14.  
  15. Note: If you are using a microphone powered by the 3.3V signal, such as the Sparkfun MEMS microphone, then connect 3.3V to the AREF pin.
  16.  
  17. */
  18.  
  19.  
  20. #define qsubd(x, b) ((x>b)?wavebright:0)                     // A digital unsigned subtraction macro. if result <0, then => 0. Otherwise, take on fixed value.
  21. #define qsuba(x, b) ((x>b)?x-b:0)                            // Unsigned subtraction macro. if result <0, then => 0.
  22.  
  23. #define wavebright 128                                        // qsubd result will be this value if subtraction is >0.
  24.  
  25. #include "FastLED.h"                                          // FastLED library. Preferably the latest copy of FastLED 2.1.
  26.  
  27. // Fixed definitions cannot change on the fly.
  28. #define LED_DT 12                                             // Data pin to connect to the strip.
  29. #define LED_CK 11                                             // Clock pin, if using 4 pin strips.
  30. #define COLOR_ORDER BGR                                       // Are they RGB, GRB or what??
  31. #define LED_TYPE APA102                                       // What kind of strip are you using?
  32. #define NUM_LEDS 20                                           // Number of LED's.
  33.  
  34. // Initialize changeable global variables.
  35. uint8_t max_bright = 128;                                     // Overall brightness definition. It can be changed on the fly.
  36.  
  37. struct CRGB leds[NUM_LEDS];                                   // Initialize our LED array.
  38.  
  39. // Palette definitions
  40. CRGBPalette16 currentPalette;
  41. CRGBPalette16 targetPalette;
  42. TBlendType    currentBlending;
  43.  
  44.  
  45. #define LOG_OUT 1
  46.  
  47.  
  48. #define FHT_N 256                                             // Set to 256 point fht.
  49. #define inputPin A5
  50.  
  51. #include <FHT.h>                                              // FHT library
  52.  
  53. uint8_t hueinc;                                               // A hue increment value to make it rotate a bit.
  54. uint8_t micmult = 10;                                         // Make it louder and easier to see.
  55. uint8_t noiseval = 32;                                        // Increase this to reduce sensitivity.
  56.  
  57.  
  58. // Function that printf and related will use to print
  59. int serial_putchar(char c, FILE* f) {
  60.     if (c == '\n') serial_putchar('\r', f);
  61.     return !Serial.write(c);
  62. }
  63.  
  64. FILE serial_stdout;
  65.  
  66.  
  67.  
  68. void setup() {
  69.   analogReference(EXTERNAL);                                  // Connect 3.3V to AREF pin for any microphones using 3.3V
  70.   Serial.begin(57600);                                        // use the serial port
  71. //  LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);  // Use this for WS2812B LED_TYPE
  72.   LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS);  // Use this for WS2801 or APA102 LED_TYPE
  73.  
  74.   FastLED.setBrightness(max_bright);
  75.   set_max_power_in_volts_and_milliamps(5, 500);               // This is used by the power management functionality and is currently set at 5V, 500mA.
  76.  
  77.  
  78.   currentPalette  = CRGBPalette16(CRGB::Black);
  79.   targetPalette   = RainbowColors_p;                            // Used for smooth transitioning.
  80.   currentBlending = LINEARBLEND;
  81.  
  82.    // Set up stdout
  83.   fdev_setup_stream(&serial_stdout, serial_putchar, NULL, _FDEV_SETUP_WRITE);
  84.   stdout = &serial_stdout;
  85.  
  86. } // setup()
  87.  
  88.  
  89.  
  90. void loop() {
  91.   ChangePaletteAndSettingsPeriodically();
  92.  
  93.   EVERY_N_MILLISECONDS(100) {
  94.     uint8_t maxChanges = 24;
  95.     nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges);   // AWESOME palette blending capability.
  96.   }
  97.  
  98.   EVERY_N_MILLISECONDS(50) {
  99.     fhtsound();
  100.   }
  101.   show_at_max_brightness_for_power();                         // Run the FastLED.show() at full loop speed.
  102. //  Serial.println(LEDS.getFPS());                              // Display frames per second on the serial monitor.  
  103.  
  104. } // loop()
  105.  
  106.  
  107. void fhtsound() {
  108.   GetFHT();                                                   // Let's take FHT_N samples and crunch 'em.
  109. //  for (int i = 0 ; i < FHT_N/2 ; i++)                       // You can process up to FHT_N/2 of them. Hmm, Nyquist.
  110. //    B[i] = fht_log_out[i];
  111.  
  112.  
  113.   for (int i= 0; i < NUM_LEDS; i++) {                         // Run through the LED array.
  114.     int tmp = qsuba(fht_log_out[2*i+2], noiseval);           // Get the sample and subtract the 'quiet' normalized values, but don't go < 0.
  115.     if (tmp > (leds[i].r + leds[i].g + leds[i].b))            // Refresh an LED only when the intensity is low
  116.         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.
  117. //        leds[i] = ColorFromPalette(currentPalette, tmp*micmult, tmp*micmult, currentBlending);   // index, brightness
  118.  
  119.     leds[i].nscale8(224);                                     // Let's fade the whole thing over time as well.
  120.   }
  121. } // fhtsound()
  122.  
  123.  
  124.  
  125. void GetFHT() {
  126. //  Serial.println("GetFHT");
  127.   cli();
  128.   for (int i = 0 ; i < FHT_N ; i++) {
  129.     fht_input[i] = analogRead(inputPin) - 512;
  130. //    Serial.print(fht_input[i]);
  131. //    Serial.print(", ");
  132.   }
  133.   sei();
  134. //  Serial.println();
  135.     // save 256 samples
  136.   fht_window();                                               // Window the data for better frequency response.
  137.   fht_reorder();                                              // Reorder the data before doing the fht.
  138.   fht_run();                                                  // Process the data in the fht.
  139. //  fht_mag_octave();                                         // Take the output of the fht and put in an array called fht_log_out[FHT_N/2]
  140.   fht_mag_log();                                              // Let's use logarithmic bins.
  141. } // GetFHT()
  142.  
  143.  
  144.  
  145. void ChangePaletteAndSettingsPeriodically() {
  146.   uint8_t secondHand = ((millis() / 1000) / 1) % 80;
  147.   static uint8_t lastSecond = 99;
  148.   if (lastSecond != secondHand) {
  149.     lastSecond = secondHand;
  150.     switch(secondHand) {
  151.       case  0: targetPalette = RainbowColors_p; break;
  152.       case 10: SetupRandomPalette(); break;
  153.       case 20: targetPalette = PartyColors_p; break;
  154.       case 30: targetPalette = ForestColors_p; break;
  155.       case 40: targetPalette = CloudColors_p; break;
  156.       case 50: targetPalette = LavaColors_p; break;
  157.       case 60: targetPalette = OceanColors_p; break;
  158.       case 70: SetupRandomPalette(); break;
  159.       case 80: break;
  160.     }
  161.   }
  162. } // ChangePaletteAndSettingsPeriodically()
  163.  
  164.  
  165. void SetupRandomPalette() {
  166.   targetPalette = CRGBPalette16(CHSV( random8(), 255, 32), CHSV(random8(), 255, 255), CHSV(random8(), 128, 255), CHSV(random8(), 255, 255));
  167. }
  168.  
  169. void SetupSimilarPalette() {
  170.   int tmphue = random8();
  171.   targetPalette = CRGBPalette16(CHSV(tmphue-10, 255, 32), CHSV(tmphue, 255, 255), CHSV(tmphue-5, 128, 255), CHSV(tmphue+5, 255, 255));
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement