Back to Parent

#include "captouch.h"
#define CAPTOUCH_DEBUG

// Audio

//Connections:
// - 5 V to mic amp +
// - GND to mic amp -
// - Analog pin to microphone output
int mic_pin = A0;
// store the noise level
// from the microphone
int noise_level = 0;
//PIR sensor
int inputPin = D0;
int pirState = LOW;             // we start, assuming no motion detected
int val = LOW;                    // variable for reading the pin status
int calibrateTime = 10000;      // wait for the thingy to calibrate
//LED to indicate system triggered
int ledPin = D7;

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

// Noise level to trigger the system
const int alarmTreshhold = 250;

// To track if the system has been trigglered in the same day
bool triggeredToday = false;

//timer after alarm triggered
int timeElapsed = 0;

//Pizeo Variables
int speakerPin = D1;

// 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 };

//Time to reset the system
int resetHour = 5;
int resetMinute = 59;

void setup()
{

  Particle.variable( "noise", noise_level );
  Particle.variable( "triggered", triggeredToday );
  Particle.variable( "val", val);
  Serial.begin(9600);
  pinMode( inputPin, INPUT_PULLDOWN );
  pinMode( ledPin, OUTPUT );
  //Piezo
  pinMode( speakerPin, OUTPUT );
  Time.zone( -5 );

}

void loop(){
  //reset system in the morning
  if (resetHour == Time.hour() and resetMinute == Time.minute() ){
  triggeredToday = false;
  }
  noise_level = sampleNoise( );
	Serial.print("sensorValue ");
  Serial.println( noise_level );

//System triggered by the phoen alarm, start countddown for further actions
  if (triggerSystem () == true ) {

    digitalWrite (ledPin, HIGH);
    timeElapsed = millis();
    while (PIRmain () == false){
      Particle.process();
      if( timeElapsed + 5000 < millis() ){ //adjust timer to your own setting
        playNotes(); //system alarm
      }
      if( timeElapsed + 10000< millis() ){ //adjust timer to your own setting
        Particle.publish("Wake up!","It's time!"); //publish event for IFTTT message
        timeElapsed = millis()-5000; //reset timer for another 5s (adjust to your own timing)
      }
    }
    digitalWrite (ledPin, 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;
}

//To check if system is triggered by the high alarm sound
bool triggerSystem()
{
  if (triggeredToday == false and noise_level > alarmTreshhold){
      triggeredToday = true;
      Particle.publish("alarm");
      return true;
    }
  else {
    return false;
  }
}

//PIR Functions
bool PIRmain () {
  // if the sensor is calibrated
  if ( calibrated() )
  {
  // get the data from the sensor
  readTheSensor();
  // report it out, if the state has changed
  reportTheData();
  }
  if (pirState == HIGH){
    return true;
  }
  return false;
}

void readTheSensor() {
  val = digitalRead(inputPin);
}

bool calibrated() {
  return millis() - calibrateTime > 0;
}

void reportTheData() {

  // if the sensor reads high
  // or there is now motion
  if (val == HIGH) {

    // the current state is no motion
    // i.e. it's just changed
    if (pirState == LOW) {
      // we have just turned on
      // Update the current state
      pirState = HIGH;
      //setLED( pirState );
    }
  } else {
    if (pirState == HIGH) {
      // we have just turned of
      // Update the current state
      pirState = LOW;
      //setLED( pirState );
    }
  }
}

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(speakerPin, 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(speakerPin);
    }
}
Click to Expand

Content Rating

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

0