Back to Parent

int inputPin = D0;              
int ledPin = D1;                
int buttonPin = D4;
int pirState = LOW;             // start no motion detected
int val = 0;                    // 

int calibrateTime = 10000;      // wait for calibration



void setup()
{
  pinMode( ledPin, OUTPUT );
  pinMode(inputPin, INPUT);     
  pinMode( buttonPin , INPUT_PULLUP);
}

void loop()
{
  int buttonState = digitalRead( buttonPin );
  if( buttonState == LOW )
  {
    setLED( HIGH );

    digitalWrite( ledPin, HIGH);
  }
  //  digitalWrite( ledPin, LOW);
  //}
  // if the sensor is calibrated
  if ( calibrated() )
  {
  // get the data from the sensor
    readTheSensor();

    // report it out, if the state has changed
    reportTheData();
    //Particle.publish("motion_detected");
  }
}

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

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

void reportTheData() {

int buttonState = digitalRead( buttonPin );


  if (val == HIGH) {

    // the current state is no motion
    // changed
    // announce this change by publishing an eent
    if (pirState == LOW) {
      // we have just turned on
      Particle.publish("motion");
      // Update the current state
      pirState = HIGH;
      setLED( pirState );
      delay(10000);
      Particle.publish("motion");

    }
  }

  else {
    if (pirState == HIGH) {
      // we have just turned of
      // Update the current state and flash for off
      pirState = LOW;
      setLED( LOW );
      delay(1000);
      setLED( HIGH );
      delay(1000);
      setLED( LOW );
      delay(1000);
      setLED( HIGH );
      delay(1000);
      setLED( LOW );
      delay(1000);
      setLED( HIGH );
      setLED( pirState );
      Particle.publish("motion_complete");
    }
  }
}

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