Back to Parent

/*
 * Project homehack
 * Title: Smart Bed Underglow
 * Author:Xitong Deng
 * Date: 01/28/2018
 */

int inputPin = A0;          //choose the input pin (for PIR sensor) [analog pin]
int ledPin = D1;            //LED Pin
int pirState = LOW;         //assume no motion detected when we start
int val = 0;                //variable for reading the pin status.

void setup() {
  Serial.begin(9600);
  Serial.print("On");
  pinMode(ledPin, OUTPUT);  //declare LED light as output
  pinMode(inputPin, INPUT); //declare sensor as input
}

void loop() {
    //make sure the loop is working
    Serial.print("Calibrated");
    //get the data from the sensor
    readTheSensor();
    //report it out, if the state has changed
    reportTheData();

}

void readTheSensor(){
  val = analogRead(inputPin);
  Serial.println(val);
  delay(500); //I can try to delete it!
}

void reportTheData(){
    //--> when the sensor tells that motion is there
    if (val > 1000){
      if (pirState == LOW){
        Particle.publish("motion-detected"); //announce this change by publishing an event
        pirState = HIGH;
        setLED(pirState);
      }
    }
    else{
    //--> when the sensor tells that motion is no longer there
    //--> we need to turn the state into LOW and turn off the light
      if (pirState == HIGH){
        pirState = LOW;
        setLED(pirState);
      }
    }
}

void setLED (int state){ //turn on/off the led light according to 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