Back to Parent

int inputPin = D0;              // choose the input pin (for PIR sensor)
int redLed = D1;
int greenLed = D3;                // LED Pin
int speakerPin = D2;            // Piezo pin
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status

int calibrateTime = 10000;      // wait for the thingy to calibrate

void setup()
{
  pinMode( redLed, OUTPUT );
  pinMode( greenLed, OUTPUT);
  pinMode( inputPin, INPUT);     // declare sensor as input
  pinMode(speakerPin, OUTPUT);
  Particle.publish("hardikpatelpir");

}

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(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
    // announce this change by publishing an event
    if (pirState == LOW) {
      //Particle.subscribe("hardikpatelpir2");
      // we have just turned on
      // Update the current state
      digitalWrite(greenLed, HIGH);
      digitalWrite(redLed, LOW );
      tone(speakerPin, 2000, 1000);
      delay(1000);
      digitalWrite(redLed, HIGH);
      digitalWrite(greenLed, LOW);
      pirState = HIGH;
      //setLED( pirState );
    }
  } else {
    if (pirState == HIGH) {
      // we have just turned of
      // Update the current state
      pirState = LOW;
      digitalWrite(greenLed, 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