Back to Parent

int PIRsensor = D0;              // choose the input pin (for PIR sensor)
int buzzPin = D3;
int button = D4;                // button ouput
int ledPin = D5;                //led OUTPUT
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;
int triggered = false;                   // variable for reading the pin status
int calibrateTime = 60000;      // wait for the thingy to calibrate

void setup()
{
  Serial.begin( 9600 );
  pinMode( buzzPin, OUTPUT );    //declare buzzer as output
  pinMode(ledPin, OUTPUT);       //declare led as output
  pinMode(PIRsensor, INPUT);     // declare sensor as input
  pinMode(button, INPUT_PULLUP);
}
void loop()
{
  // if the sensor is calibrated
  if ( calibrated() )
  {
  // get the data from the sensor
    readTheSensor();
    // report it out, if the state has changed
    reportTheData();
  }
}

void readTheSensor() {
  val = digitalRead(PIRsensor);
  Serial.print( "PIR IS " );
  Serial.println( val );
}

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

void reportTheData() {
  // if the sensor reads high
  // or there is now motion
  if (val == HIGH)
  {
     if (pirState == LOW)
     {
       triggered = true;
       Particle.publish("designingiot/s15/motion");
       pirState = HIGH;
      setLED( pirState );
     }

   }

int buttonValue = digitalRead(button);
   if( buttonValue == LOW ){
     triggered = false;
}

   if (  triggered == true ) {
     tone(buzzPin, 400);
     delay(300);
     noTone(buzzPin);
     delay(300);
     tone(buzzPin, 300);
     delay(300);
   }

   else {
     if (pirState == HIGH) {
      // we have just turned of
      // Update the current state
      pirState = LOW;
      setLED( pirState );
    }
  }
}
void setLED( int state )
{
  digitalWrite( ledPin, state );
}
Click to Expand

Content Rating

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

0