Back to Parent

o// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_COUNT 16
#define PIXEL_PIN D2
#define PIXEL_TYPE WS2812B

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

/****************************************
Example Sound Level Sketch for the 
Adafruit Microphone Amplifier
****************************************/

int loudNoise = 1;

const int sampleWindow = 50; // Sample window width in mS (50 mS = 20Hz)
unsigned int sample;

double temperature = 0;

void setup() 
{
   Serial.begin(9600);
   strip.begin();
   strip.show();
   Particle.publish( "weather" );
   Particle.subscribe("hook-response/weather", myHandler, MY_DEVICES);
}

void myHandler(const char *event, const char *data) {
  // Handle the integration response
  
  temperature = (double)(String( data ).toFloat());
  
}
          



void loop() 
{
   unsigned long startMillis= millis();  // Start of sample window
   unsigned int peakToPeak = 0;   // peak-to-peak level

   unsigned int signalMax = 0;
   unsigned int signalMin = 4095;

   // collect data for 50 mS
   while (millis() - startMillis < sampleWindow)
   {
      sample = analogRead(0);
      if (sample < 4095)  // toss out spurious readings
      {
         if (sample > signalMax)
         {
            signalMax = sample;  // save just the max levels
         }
         else if (sample < signalMin)
         {
            signalMin = sample;  // save just the min levels
         }
      }
   }
   peakToPeak = signalMax - signalMin;  // max - min = peak-peak amplitude
   double volts = (peakToPeak * 5.0) / 4095;  // convert to volts
   
   if( volts > 0.07 ){

       if( loudNoise < 1 ){
           Particle.publish( "weather" );
       }

       loudNoise = 100;
       
   }
   
   if( loudNoise > 0){
        //int color = map( loudNoise, 0, 100, 0, 255);
        int t = (int)(temperature * 100);
        
        int color = map( loudNoise, 0, 10000, 0, 255);
        
        // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
        for(int i = 0 ; i < strip.numPixels(); i++ ){
            strip.setPixelColor(i, strip.Color(t,0,255-t)); // Blue.
        }

       strip.show(); // This sends the updated pixel color to the hardware.
       loudNoise = loudNoise - 1;
   }else{
        for(int i = 0 ; i < strip.numPixels(); i++ ){
            strip.setPixelColor(i, strip.Color(0,0,0)); // Red.
        }
        strip.show(); // This sends the updated pixel color to the hardware.

   }
   
   

   delay( 50 );

// //code to turn on the light
// if (volts > .03)
// {
//     // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
//     strip.setPixelColor(0, strip.Color(0,0,255)); // Blue.

//     strip.show(); // This sends the updated pixel color to the hardware.
// }
// else
// {
//      strip.setPixelColor(0, strip.Color(255,0,0)); // Red.

//     strip.show(); // This sends the updated pixel color to the hardware.
// }
   Serial.println(loudNoise);
   Serial.println(volts);
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0