SHARE
TWEET

Untitled

a guest Dec 8th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Adafruit_NeoPixel.h>
  2.  
  3. /****************************************
  4. Example Sound Level Sketch for the
  5. Adafruit Microphone Amplifier
  6. ****************************************/
  7.  
  8. #define SAMPLES 5
  9. #define N_PIXELS  60  // Number of pixels in strand
  10. #define MIC_PIN   A1  // Microphone is attached to this analog pin
  11. #define LED_PIN    2  // NeoPixel LED strand is connected to this pin
  12.  
  13. const int sampleWindow = 20; // Sample window width in mS (50 mS = 20Hz)
  14. unsigned int sample;
  15.  
  16. int vol[SAMPLES];
  17. int volCount = 0;
  18. int total = 0;
  19. int avg = 0;
  20. int avg_int = 0;
  21.  
  22. Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800);
  23.  
  24.  
  25. void setup()
  26. {
  27.    Serial.begin(9600);
  28.    analogReference(EXTERNAL);
  29.    memset(vol, 0, sizeof(vol));
  30.    strip.begin();
  31. }
  32.  
  33.  
  34. void loop()
  35. {
  36.    unsigned long startMillis= millis();  // Start of sample window
  37.    unsigned int peakToPeak = 0;   // peak-to-peak level
  38.  
  39.    unsigned int signalMax = 0;
  40.    unsigned int signalMin = 1024;
  41.  
  42.    // collect data for 50 mS
  43.    while (millis() - startMillis < sampleWindow)
  44.    {
  45.       sample = analogRead(MIC_PIN);
  46.       if (sample < 1024)  // toss out spurious readings
  47.       {
  48.          if (sample > signalMax)
  49.          {
  50.             signalMax = sample;  // save just the max levels
  51.          }
  52.          else if (sample < signalMin)
  53.          {
  54.             signalMin = sample;  // save just the min levels
  55.          }
  56.       }
  57.    }
  58.    peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
  59. //   int volts = (peakToPeak * 3.3) / 1024;  // convert to volts
  60.  
  61.    total = total - vol[volCount];
  62.    vol[volCount] = peakToPeak;                      // Save sample for dynamic levelin
  63.    total = total + vol[volCount];
  64.    volCount += 1;
  65.    if(volCount >= SAMPLES) volCount = 0; // Advance/rollover sample counter
  66.    avg = total / SAMPLES;
  67.    avg_int = map(constrain(avg, 100, 900), 100, 900, 0, 255);
  68. //   Serial.print(avg);
  69. //   Serial.print(",");
  70.  
  71.     uint32_t color = strip.Color(0, 0, 0);
  72.    
  73.     if (avg_int < 32) {
  74.       color = strip.Color(255, 255, 255); // white  
  75.     } else if (avg_int < 64) {
  76.       color = strip.Color(0, 0, 102); // blue
  77.     } else if (avg_int < 96) {
  78.       color = strip.Color(76, 153, 0); // verde
  79.     } else if (avg_int < 128) {
  80.       color = strip.Color(0, 204, 204); // turcoaz
  81.     for (int i = 0; i < N_PIXELS; i++) {
  82.       strip.setPixelColor(i, color);
  83.     }
  84.     strip.show();
  85.    Serial.print(avg_int);
  86.     } else if (avg_int < 160) {
  87.       color = strip.Color(255, 0, 127);
  88.     } else if (avg_int < 192) {
  89.       color = strip.Color(204, 0, 0);
  90.     } else if (avg_int < 224) {
  91.       color = strip.Color(255, 0, 0);
  92.     } else {
  93.       color = strip.Color(255, 51, 255);
  94.     }
  95.    
  96.    Serial.println();
  97. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top