Back to Parent

// the pin we're reading from
int mic_pin = A0;
const int sampleWindow = 50;
int numPins = 2;
int ledPins[ 2 ] = { D0, D1 };
int piezoPin = D2;
// know if the lights are on or off
bool light_state = false;
// for our sound detection
// store the noise level / reading from the electret
int noise_level = 1000;
// empty house noise level
int noise_threshold = 500;
// when we last heard a noise
long last_loud_noise_at = -1;
int  turn_lights_off_after = 10 * 1000;


// create an array for the notes in the melody:
//C4,G3,G3,A3,G3,0,B3,C4
int melody[] = {1908,2551,2551,2273,2551,0,2024,1908};

// create an array for the duration of notes.
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4,8,8,4,4,4,4,4 };


void setup(){
 // start serial connection
 Serial.begin(9600);
 for( int i = 0 ; i < numPins; i++ ){
   pinMode(ledPins[ i ], OUTPUT);
 }
 light_state = true ;
 for( int i = 0 ; i < numPins; i++ ){
   int led = ledPins[ i ];
   digitalWrite( led, HIGH );
 }

 pinMode(piezoPin, OUTPUT);


 // Share the value as a noise level
 // reading through the cloud
 Particle.variable( "noise", &noise_level, INT );
}
void loop()
{
 noise_level = sampleNoise( );
 Serial.print( "Noise Level:" );
 Serial.println( noise_level );
 checkNoiseLevel();
 checkifShouldTurnLightsOff();
 displayLEDs();

 Serial.print( "Noise At:" );
 Serial.println( last_loud_noise_at );
 delay( 100 );
}
void checkNoiseLevel(){
  if( noise_level > noise_threshold ){
    // this means the house is occupied
    last_loud_noise_at = -1;
    //light_state = true;
  }else{
    if( last_loud_noise_at == -1 )
      last_loud_noise_at = millis();
  }
}
void checkifShouldTurnLightsOff(){
  if( last_loud_noise_at == -1 || light_state == false ){
    // do nothing;
    return;
  }
  if( last_loud_noise_at + turn_lights_off_after < millis() ){

    digitalWrite(D2, HIGH);
    delay(100);
    digitalWrite(D2, LOW);
    // add a beep here
    playNotes();

    light_state = false;


  }
}
void displayLEDs(){
 for( int i = 0 ; i < numPins; i++ ){
   int led = ledPins[ i ];
   if( light_state == true ){
     digitalWrite( led, HIGH );
   }else{
     digitalWrite( led, LOW );

   }
 }
}
int sampleNoise()
{
 unsigned long startMillis = millis(); // Start of sample window
 int highest_sample = 0;
 int lowest_sample = 1000;
 // 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, 1000, 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;
}




void playNotes()
{
    // iterate over the notes of the melody:
    for (int thisNote = 0; thisNote < 8; thisNote++) {

      // to calculate the note duration, take one second
      // divided by the note type.
      //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
      int noteDuration = 1000/noteDurations[thisNote];
      tone(piezoPin, melody[thisNote],noteDuration);

      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well:
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);
      // stop the tone playing:
      noTone(piezoPin);
    }
}
Click to Expand

Content Rating

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

0