Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "FastLED.h" // FastLED library.
- // Fixed definitions cannot change on the fly.
- #define LED_DT 6 // Data pin to connect to the strip.
- #define COLOR_ORDER GRB // It's GRB for WS2812B
- #define LED_TYPE WS2812 // What kind of strip are you using (WS2801, WS2812B or APA102)?
- #define NUM_LEDS 50 // Number of LED's.
- // Initialize changeable global variables.
- uint8_t max_bright = 255; // Overall brightness definition. It can be changed on the fly.
- //Left part of the suit
- #define LED_PIN_left 3
- #define NUM_LEDS_left 64
- #define NUM_LEDS_left_arm 22
- #define NUM_LEDS_left_body 19
- #define NUM_LEDS_left_leg 23
- CRGB leds_left[NUM_LEDS_left];
- //Right part of the suit
- #define LED_PIN_right 6
- #define NUM_LEDS_right 64
- #define NUM_LEDS_right_arm 21
- #define NUM_LEDS_right_body 19
- #define NUM_LEDS_right_leg 24
- CRGB leds_right[NUM_LEDS_right];
- #define MIC_PIN 0 // Analog port for microphone
- #define DC_OFFSET 0 // DC offset in mic signal - if unusure, leave 0
- #define NOISE 0 // Noise/hum/interference in mic signal and increased value until it went quiet
- #define SAMPLES 120 // Length of buffer for dynamic level adjustment
- #define TOP (NUM_LEDS + 2) // Allow dot to go slightly off scale
- byte
- volCount = 0; // Frame counter for storing past volume data
- int
- vol[SAMPLES], // Collection of prior volume samples
- lvl = 10, // Current "dampened" audio level
- minLvlAvg = 0, // For dynamic adjustment of graph low & high
- maxLvlAvg = 412;
- //Temp
- uint8_t gHue = 0; // rotating "base color" used by many of the patterns
- void setup() {
- // This is only needed on 5V Arduinos (Uno, Leonardo, etc.).
- // Connect 3.3V to mic AND TO AREF ON ARDUINO and enable this
- // line. Audio samples are 'cleaner' at 3.3V.
- // COMMENT OUT THIS LINE FOR 3.3V ARDUINOS (FLORA, ETC.):
- //analogReference(EXTERNAL);
- delay(1000); // Power-up safety delay or something like that.
- Serial.begin(57600);
- LEDS.addLeds<LED_TYPE, 5, COLOR_ORDER>(leds_left, NUM_LEDS_left); // Use this for WS2812B
- LEDS.addLeds<LED_TYPE, 6, COLOR_ORDER>(leds_right, NUM_LEDS_right); // Use this for WS2812B
- // LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2801 or APA102
- FastLED.setBrightness(max_bright);
- set_max_power_in_volts_and_milliamps(5, 500); // FastLED 2.1 Power management set at 5V, 500mA
- Serial.println("STARTING");
- } // setup()
- void loop() {
- soundbracelet();
- //FastLED.show();
- //show_at_max_brightness_for_power(); // Power managed FastLED display
- } // loop()
- void soundbracelet() {
- Serial.println("");
- uint8_t i;
- uint16_t minLvl, maxLvl;
- int n, height;
- n = analogRead(MIC_PIN); // Raw reading from mic
- //Serial.print("start met n = ");
- //Serial.print(n);
- n = abs(n - 512 - DC_OFFSET); // Center on zero
- //Serial.print("\t after abs = ");
- //Serial.print(n);
- n = (n <= NOISE) ? 0 : (n - NOISE); // Remove noise/hum
- //Serial.print("\t after noise/hum = ");
- //Serial.print(n);
- lvl = ((lvl * 7) + n) >> 3; // "Dampened" reading (else looks twitchy)
- //Serial.print("\t lvl = ");
- //Serial.print(lvl);
- // Calculate bar height based on dynamic min/max levels (fixed point):
- height = TOP * (lvl - minLvlAvg) / (long)(maxLvlAvg - minLvlAvg);
- Serial.print("\t height = ");
- Serial.print(height);
- // Fill 2 strips
- for (i=0; i<=height; i++) {
- leds_left[i] = CHSV(gHue, 200, 255);
- }
- //for (i=0; i<=height; i++) {
- // leds_right[i] = CHSV(gHue, 200, 255);
- //}
- if(height >= 50){
- fill_solid(leds_right, NUM_LEDS_right, CRGB::Green);
- }
- vol[volCount] = n; // Save sample for dynamic leveling
- if (++volCount >= SAMPLES) volCount = 0; // Advance/rollover sample counter
- // Get volume range of prior frames
- minLvl = maxLvl = vol[0];
- for (i=1; i<SAMPLES; i++) {
- if (vol[i] < minLvl) minLvl = vol[i];
- else if (vol[i] > maxLvl) maxLvl = vol[i];
- }
- // minLvl and maxLvl indicate the volume range over prior frames, used
- // for vertically scaling the output graph (so it looks interesting
- // regardless of volume level). If they're too close together though
- // (e.g. at very low volume levels) the graph becomes super coarse
- // and 'jumpy'...so keep some minimum distance between them (this
- // also lets the graph go to zero when no sound is playing):
- if((maxLvl - minLvl) < TOP) maxLvl = minLvl + TOP;
- minLvlAvg = (minLvlAvg * 63 + minLvl) >> 6; // Dampen min/max levels
- maxLvlAvg = (maxLvlAvg * 63 + maxLvl) >> 6; // (fake rolling average)
- FastLED.show();
- EVERY_N_MILLISECONDS(20)
- {
- //gHue++;
- fadeToBlackBy(leds_left, NUM_LEDS, 145);
- fadeToBlackBy(leds_right, NUM_LEDS, 45);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment