Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Work just starting for: Fast Hartley Transform with beat detection for FastLED.
- By: Andrew Tuline
- Date: Aug, 2015
- This is an implementation of the FHT library with FastLED 3.1
- FHT is available at http://wiki.openmusiclabs.com/wiki/ArduinoFHT
- 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.
- */
- #define qsubd(x, b) ((x>b)?wavebright:0) // A digital unsigned subtraction macro. if result <0, then => 0. Otherwise, take on fixed value.
- #define qsuba(x, b) ((x>b)?x-b:0) // Unsigned subtraction macro. if result <0, then => 0.
- #define wavebright 128 // qsubd result will be this value if subtraction is >0.
- #include "FastLED.h" // FastLED library. Preferably the latest copy of FastLED 2.1.
- // Fixed definitions cannot change on the fly.
- #define LED_DT 12 // Data pin to connect to the strip.
- #define LED_CK 11 // Clock pin, if using 4 pin strips.
- #define COLOR_ORDER BGR // Are they RGB, GRB or what??
- #define LED_TYPE APA102 // What kind of strip are you using?
- #define NUM_LEDS 20 // Number of LED's.
- // Initialize changeable global variables.
- uint8_t max_bright = 128; // Overall brightness definition. It can be changed on the fly.
- struct CRGB leds[NUM_LEDS]; // Initialize our LED array.
- // Palette definitions
- CRGBPalette16 currentPalette;
- CRGBPalette16 targetPalette;
- TBlendType currentBlending;
- #define LOG_OUT 1
- #define FHT_N 256 // Set to 256 point fht.
- #define inputPin A5
- #include <FHT.h> // FHT library
- uint8_t hueinc; // A hue increment value to make it rotate a bit.
- uint8_t micmult = 10; // Make it louder and easier to see.
- uint8_t noiseval = 32; // Increase this to reduce sensitivity.
- // Function that printf and related will use to print
- int serial_putchar(char c, FILE* f) {
- if (c == '\n') serial_putchar('\r', f);
- return !Serial.write(c);
- }
- FILE serial_stdout;
- void setup() {
- analogReference(EXTERNAL); // Connect 3.3V to AREF pin for any microphones using 3.3V
- Serial.begin(57600); // use the serial port
- // LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2812B LED_TYPE
- LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2801 or APA102 LED_TYPE
- FastLED.setBrightness(max_bright);
- set_max_power_in_volts_and_milliamps(5, 500); // This is used by the power management functionality and is currently set at 5V, 500mA.
- currentPalette = CRGBPalette16(CRGB::Black);
- targetPalette = RainbowColors_p; // Used for smooth transitioning.
- currentBlending = LINEARBLEND;
- // Set up stdout
- fdev_setup_stream(&serial_stdout, serial_putchar, NULL, _FDEV_SETUP_WRITE);
- stdout = &serial_stdout;
- } // setup()
- void loop() {
- ChangePaletteAndSettingsPeriodically();
- EVERY_N_MILLISECONDS(100) {
- uint8_t maxChanges = 24;
- nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges); // AWESOME palette blending capability.
- }
- EVERY_N_MILLISECONDS(50) {
- fhtsound();
- }
- show_at_max_brightness_for_power(); // Run the FastLED.show() at full loop speed.
- // Serial.println(LEDS.getFPS()); // Display frames per second on the serial monitor.
- } // loop()
- void fhtsound() {
- GetFHT(); // Let's take FHT_N samples and crunch 'em.
- // for (int i = 0 ; i < FHT_N/2 ; i++) // You can process up to FHT_N/2 of them. Hmm, Nyquist.
- // B[i] = fht_log_out[i];
- for (int i= 0; i < NUM_LEDS; i++) { // Run through the LED array.
- int tmp = qsuba(fht_log_out[2*i+2], noiseval); // Get the sample and subtract the 'quiet' normalized values, but don't go < 0.
- if (tmp > (leds[i].r + leds[i].g + leds[i].b)) // Refresh an LED only when the intensity is low
- 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.
- // leds[i] = ColorFromPalette(currentPalette, tmp*micmult, tmp*micmult, currentBlending); // index, brightness
- leds[i].nscale8(224); // Let's fade the whole thing over time as well.
- }
- } // fhtsound()
- void GetFHT() {
- // Serial.println("GetFHT");
- cli();
- for (int i = 0 ; i < FHT_N ; i++) {
- fht_input[i] = analogRead(inputPin) - 512;
- // Serial.print(fht_input[i]);
- // Serial.print(", ");
- }
- sei();
- // Serial.println();
- // save 256 samples
- fht_window(); // Window the data for better frequency response.
- fht_reorder(); // Reorder the data before doing the fht.
- fht_run(); // Process the data in the fht.
- // fht_mag_octave(); // Take the output of the fht and put in an array called fht_log_out[FHT_N/2]
- fht_mag_log(); // Let's use logarithmic bins.
- } // GetFHT()
- void ChangePaletteAndSettingsPeriodically() {
- uint8_t secondHand = ((millis() / 1000) / 1) % 80;
- static uint8_t lastSecond = 99;
- if (lastSecond != secondHand) {
- lastSecond = secondHand;
- switch(secondHand) {
- case 0: targetPalette = RainbowColors_p; break;
- case 10: SetupRandomPalette(); break;
- case 20: targetPalette = PartyColors_p; break;
- case 30: targetPalette = ForestColors_p; break;
- case 40: targetPalette = CloudColors_p; break;
- case 50: targetPalette = LavaColors_p; break;
- case 60: targetPalette = OceanColors_p; break;
- case 70: SetupRandomPalette(); break;
- case 80: break;
- }
- }
- } // ChangePaletteAndSettingsPeriodically()
- void SetupRandomPalette() {
- targetPalette = CRGBPalette16(CHSV( random8(), 255, 32), CHSV(random8(), 255, 255), CHSV(random8(), 128, 255), CHSV(random8(), 255, 255));
- }
- void SetupSimilarPalette() {
- int tmphue = random8();
- targetPalette = CRGBPalette16(CHSV(tmphue-10, 255, 32), CHSV(tmphue, 255, 255), CHSV(tmphue-5, 128, 255), CHSV(tmphue+5, 255, 255));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement