Guest User

Untitled

a guest
Feb 3rd, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.72 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 COLOR_ORDER GRB                                      // It's GRB for WS2812B
  6. #define LED_TYPE WS2812                                       // What kind of strip are you using (WS2801, WS2812B or APA102)?
  7. #define NUM_LEDS 50                                          // Number of LED's.
  8.  
  9. // Initialize changeable global variables.
  10. uint8_t max_bright = 255;                                     // Overall brightness definition. It can be changed on the fly.
  11.  
  12. //Left part of the suit
  13. #define LED_PIN_left        3
  14. #define NUM_LEDS_left       64
  15. #define NUM_LEDS_left_arm   22
  16. #define NUM_LEDS_left_body  19
  17. #define NUM_LEDS_left_leg   23
  18. CRGB leds_left[NUM_LEDS_left];
  19.  
  20. //Right part of the suit
  21. #define LED_PIN_right       6
  22. #define NUM_LEDS_right      64
  23. #define NUM_LEDS_right_arm  21
  24. #define NUM_LEDS_right_body 19
  25. #define NUM_LEDS_right_leg  24
  26. CRGB leds_right[NUM_LEDS_right];
  27.  
  28.  
  29. #define MIC_PIN 0                                            // Analog port for microphone
  30. #define DC_OFFSET  0                                         // DC offset in mic signal - if unusure, leave 0                                                        
  31. #define NOISE     0                                         // Noise/hum/interference in mic signal and increased value until it went quiet
  32. #define SAMPLES   120                                          // Length of buffer for dynamic level adjustment
  33. #define TOP (NUM_LEDS + 2)                                    // Allow dot to go slightly off scale
  34.  
  35. byte
  36.   volCount  = 0;                                              // Frame counter for storing past volume data
  37. int
  38.   vol[SAMPLES],                                               // Collection of prior volume samples
  39.   lvl       = 10,                                             // Current "dampened" audio level
  40.   minLvlAvg = 0,                                              // For dynamic adjustment of graph low & high
  41.   maxLvlAvg = 412;
  42.  
  43. //Temp
  44. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  45.  
  46. void setup() {
  47.  
  48.   // This is only needed on 5V Arduinos (Uno, Leonardo, etc.).
  49.   // Connect 3.3V to mic AND TO AREF ON ARDUINO and enable this
  50.   // line.  Audio samples are 'cleaner' at 3.3V.
  51.   // COMMENT OUT THIS LINE FOR 3.3V ARDUINOS (FLORA, ETC.):
  52.   //analogReference(EXTERNAL);
  53.  
  54.   delay(1000);                                                // Power-up safety delay or something like that.  
  55.   Serial.begin(57600);
  56.  
  57.   LEDS.addLeds<LED_TYPE, 5, COLOR_ORDER>(leds_left, NUM_LEDS_left);        // Use this for WS2812B
  58.   LEDS.addLeds<LED_TYPE, 6, COLOR_ORDER>(leds_right, NUM_LEDS_right);        // Use this for WS2812B
  59. //  LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS);  // Use this for WS2801 or APA102
  60.  
  61.   FastLED.setBrightness(max_bright);
  62.   set_max_power_in_volts_and_milliamps(5, 500);               // FastLED 2.1 Power management set at 5V, 500mA
  63.   Serial.println("STARTING");
  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.   if(height >= 50){
  115.     fill_solid(leds_right, NUM_LEDS_right, CRGB::Green);
  116.   }
  117.  
  118.   vol[volCount] = n;                                          // Save sample for dynamic leveling
  119.   if (++volCount >= SAMPLES) volCount = 0;                    // Advance/rollover sample counter
  120.  
  121.   // Get volume range of prior frames
  122.   minLvl = maxLvl = vol[0];
  123.   for (i=1; i<SAMPLES; i++) {
  124.     if (vol[i] < minLvl)      minLvl = vol[i];
  125.     else if (vol[i] > maxLvl) maxLvl = vol[i];
  126.   }
  127.  
  128.   // minLvl and maxLvl indicate the volume range over prior frames, used
  129.   // for vertically scaling the output graph (so it looks interesting
  130.   // regardless of volume level).  If they're too close together though
  131.   // (e.g. at very low volume levels) the graph becomes super coarse
  132.   // and 'jumpy'...so keep some minimum distance between them (this
  133.   // also lets the graph go to zero when no sound is playing):
  134.   if((maxLvl - minLvl) < TOP) maxLvl = minLvl + TOP;
  135.   minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6;                 // Dampen min/max levels
  136.   maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6;                 // (fake rolling average)
  137.  
  138.   FastLED.show();
  139.   EVERY_N_MILLISECONDS(20)
  140.   {
  141.     //gHue++;
  142.     fadeToBlackBy(leds_left, NUM_LEDS, 145);
  143.     fadeToBlackBy(leds_right, NUM_LEDS, 45);
  144.   }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment