Back to Parent

// the pin we're reading from
int mic_pin = A0;
// store the noise level / reading from the electret
int noise_level = 0;
const int sampleWindow = 50;
int notifyLed = D5;
int switchPin = D1;
int statusLed = D0;

void setup(){
  // start serial connection
  pinMode(notifyLed, OUTPUT);
  pinMode(switchPin, INPUT_PULLUP);
  pinMode(statusLed, OUTPUT);


  Serial.begin(9600);
  // Share the value as a noise level
  // reading through the cloud
  Particle.variable( "noise", &noise_level, INT );

}
void loop()
{
  noise_level = sampleNoise( );
  int buttonState = digitalRead( switchPin );
  Serial.println( noise_level );
  delay( 100 );

  if (buttonState == LOW){
    digitalWrite(statusLed, LOW);
  }
  else {
    digitalWrite(statusLed, HIGH);
    if (noise_level > 5500 ){
       Particle.publish("wash_status", "done");
       digitalWrite(notifyLed, HIGH);
    }
    else {
      digitalWrite(notifyLed, LOW);
        }
  }


}




int sampleNoise( )
{
  unsigned long startMillis = millis(); // Start of sample window
  int highest_sample = 0;
  int lowest_sample = 2000;
  // collect data for 50 mS
  while (millis() - startMillis < sampleWindow)
  {
    int sample = analogRead( mic_pin );
    // invert the range, and convert it to a percent
    sample = map( sample, 0, 4095, 6000, 0 );
    // now see if the sample is the lowest;
    if ( sample > highest_sample ){
    highest_sample = sample ;
    }
    if ( sample < lowest_sample ){
    lowest_sample = sample;
    }
  }
  int peakToPeak = highest_sample - lowest_sample;
  return peakToPeak;
}
Click to Expand

Content Rating

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

0