Back to Parent

/*
 * PIR sensor tester
 */

int ledPin = D0;                // choose the pin for the LED
int inputPin = D1;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // assuming no motion detected
int val = 0;                    // variable for reading the pin status
  

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input

  Serial.begin(9600);
}

void loop()
{
  

  val = digitalRead(inputPin);  // read input value
  digitalWrite(ledPin, HIGH);  // turn LED ON
  Serial.println(val);
  if (val == HIGH)
    {            // check if the input is HIGH
        if (pirState == LOW)
            {
            // we have just turned on
              Serial.println("Motion detected!");

              Particle.publish ("motion-detector","motion detected! Intruder!", PRIVATE);
            // We only want to print on the output change, not state
              pirState = HIGH;
            }
    }

  else {
        if (pirState == HIGH)
            {
            
              pirState = LOW;
            }
        }

        digitalWrite(ledPin, pirState ); // turn LED OFF
        

}
Click to Expand

Content Rating

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

0