Back to Parent

int inputPin = D4;              // choose the input pin (for PIR sensor)
int ledPin = D1;                // LED Pin
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int switchPin = D2;             // switch wired to D2

int calibrateTime = 10000;      // calibrate
int cat_pos = 0;
void setup()
{
  pinMode( ledPin, OUTPUT );
  pinMode( switchPin , INPUT_PULLUP ); // sets pin as input
  pinMode(inputPin, INPUT_PULLDOWN );     // declare sensor as input
  pinMode( ledPin , OUTPUT ); // sets pin as output
  Particle.publish("I sensed","Cat is inside first");
  Serial.begin( 9600 ); // start the serial monitor
}

void loop()
{
  // if the sensor is calÄbrated
  if ( calibrated() )
  {
  // get the data from the sensor
    readTheSensor();

  }
  if( calibrated()  ){
    // report it out, if the state has changed
    reportTheData();

  }
  Serial.print( "PIR value is : " );
  Serial.println( val );
  Serial.print( "PIR State is : " );
  Serial.println( pirState );
  Serial.println(millis());

  if (val == HIGH){
    cat_pos = cat_pos + 1;

  }
  if (cat_pos%2 == 1){
    Particle.publish("I sensed","Cat is outside now");
    delay(5000);
  }

  if (cat_pos%2 == 0){
    Particle.publish("I sensed","Cat is inside now");
    delay(5000);
  }
  delay( 500 );
}


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

}

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

void reportTheData() {
  Serial.println( "REPORT THE DATA" );


  setLED( val );

}

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