Guest User

Untitled

a guest
Feb 1st, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.60 KB | None | 0 0
  1. #include "FastLED.h"                                          // FastLED library.
  2.  
  3. // Fixed definitions cannot change on the fly.
  4. #define LED_DT 6                                            // Data pin to connect to the strip.
  5. #define LED_CK 11
  6. #define COLOR_ORDER GRB                                      // It's GRB for WS2812B
  7. #define LED_TYPE WS2812                                       // What kind of strip are you using (WS2801, WS2812B or APA102)?
  8. #define NUM_LEDS 64                                          // Number of LED's.
  9.  
  10. // Initialize changeable global variables.
  11. uint8_t max_bright = 255;                                     // Overall brightness definition. It can be changed on the fly.
  12.  
  13. //Left part of the suit
  14. #define LED_PIN_left        3
  15. #define NUM_LEDS_left       64
  16. #define NUM_LEDS_left_arm   22
  17. #define NUM_LEDS_left_body  19
  18. #define NUM_LEDS_left_leg   23
  19. CRGB leds_left[NUM_LEDS_left];
  20.  
  21. //Right part of the suit
  22. #define LED_PIN_right       6
  23. #define NUM_LEDS_right      64
  24. #define NUM_LEDS_right_arm  21
  25. #define NUM_LEDS_right_body 19
  26. #define NUM_LEDS_right_leg  24
  27. CRGB leds_right[NUM_LEDS_right];
  28.  
  29.  
  30. #define MIC_PIN 0                                            // Analog port for microphone
  31. #define DC_OFFSET  0                                         // DC offset in mic signal - if unusure, leave 0                                                        
  32. #define NOISE     0                                         // Noise/hum/interference in mic signal and increased value until it went quiet
  33. #define SAMPLES   120                                          // Length of buffer for dynamic level adjustment
  34. #define TOP (NUM_LEDS + 2)                                    // Allow dot to go slightly off scale
  35.  
  36. byte
  37.   volCount  = 0;                                              // Frame counter for storing past volume data
  38. int
  39.   vol[SAMPLES],                                               // Collection of prior volume samples
  40.   lvl       = 10,                                             // Current "dampened" audio level
  41.   minLvlAvg = 0,                                              // For dynamic adjustment of graph low & high
  42.   maxLvlAvg = 412;
  43.  
  44. //Temp
  45. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  46.  
  47. void setup() {
  48.  
  49.   // This is only needed on 5V Arduinos (Uno, Leonardo, etc.).
  50.   // Connect 3.3V to mic AND TO AREF ON ARDUINO and enable this
  51.   // line.  Audio samples are 'cleaner' at 3.3V.
  52.   // COMMENT OUT THIS LINE FOR 3.3V ARDUINOS (FLORA, ETC.):
  53.   //analogReference(EXTERNAL);
  54.  
  55.   delay(1000);                                                // Power-up safety delay or something like that.  
  56.   Serial.begin(57600);
  57.  
  58.   LEDS.addLeds<LED_TYPE, 5, COLOR_ORDER>(leds_left, NUM_LEDS_left);        // Use this for WS2812B
  59.   LEDS.addLeds<LED_TYPE, 6, COLOR_ORDER>(leds_right, NUM_LEDS_right);        // Use this for WS2812B
  60. //  LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS);  // Use this for WS2801 or APA102
  61.  
  62.   FastLED.setBrightness(max_bright);
  63.   set_max_power_in_volts_and_milliamps(5, 500);               // FastLED 2.1 Power management set at 5V, 500mA
  64. } // setup()
  65.  
  66.  
  67. void loop() {
  68.   soundbracelet();
  69.   FastLED.show();  
  70.   //show_at_max_brightness_for_power();                         // Power managed FastLED display
  71. } // loop()
  72.  
  73.  
  74. void soundbracelet() {
  75.  
  76.   Serial.println("");
  77.  
  78.   uint8_t  i;
  79.   uint16_t minLvl, maxLvl;
  80.   int      n, height;
  81.    
  82.   n = analogRead(MIC_PIN);                                    // Raw reading from mic
  83.   //Serial.print("start met n = ");
  84.   //Serial.print(n);
  85.   n = abs(n - 512 - DC_OFFSET);                               // Center on zero
  86.   //Serial.print("\t after abs = ");
  87.   //Serial.print(n);
  88.  
  89.  
  90.   n = (n <= NOISE) ? 0 : (n - NOISE);                         // Remove noise/hum
  91.  
  92.   //Serial.print("\t after noise/hum = ");
  93.   //Serial.print(n);
  94.  
  95.   lvl = ((lvl * 7) + n) >> 3;                                 // "Dampened" reading (else looks twitchy)
  96.  
  97.   //Serial.print("\t lvl = ");
  98.   //Serial.print(lvl);
  99.  
  100.   // Calculate bar height based on dynamic min/max levels (fixed point):
  101.   height = TOP * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);
  102.  
  103.   //Serial.print("\t height = ");
  104.   //Serial.print(height);
  105.  
  106.   // Fill 2 strips
  107.   for (i=0; i<=height; i++) {
  108.     leds_left[i] = CHSV(gHue, 200, 255);
  109.   }
  110.   for (i=0; i<=height; i++) {
  111.     leds_right[i] = CHSV(gHue, 200, 255);
  112.   }
  113.  
  114.   vol[volCount] = n;                                          // Save sample for dynamic leveling
  115.   if (++volCount >= SAMPLES) volCount = 0;                    // Advance/rollover sample counter
  116.  
  117.   // Get volume range of prior frames
  118.   minLvl = maxLvl = vol[0];
  119.   for (i=1; i<SAMPLES; i++) {
  120.     if (vol[i] < minLvl)      minLvl = vol[i];
  121.     else if (vol[i] > maxLvl) maxLvl = vol[i];
  122.   }
  123.  
  124.   // minLvl and maxLvl indicate the volume range over prior frames, used
  125.   // for vertically scaling the output graph (so it looks interesting
  126.   // regardless of volume level).  If they're too close together though
  127.   // (e.g. at very low volume levels) the graph becomes super coarse
  128.   // and 'jumpy'...so keep some minimum distance between them (this
  129.   // also lets the graph go to zero when no sound is playing):
  130.   if((maxLvl - minLvl) < TOP) maxLvl = minLvl + TOP;
  131.   minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6;                 // Dampen min/max levels
  132.   maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6;                 // (fake rolling average)
  133.  
  134.   EVERY_N_MILLISECONDS(20)
  135.   {
  136.     gHue++;
  137.     fadeToBlackBy(leds_left, NUM_LEDS, 145);
  138.     fadeToBlackBy(leds_right, NUM_LEDS, 145);
  139.   }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment