Back to Parent

const int ledPin = D0;
const int tiltSensor = A0;
const int switchpin = D2;


int sensorReading;      // variable to store the value read from the sensor pin
int ledState = LOW;       // variable used to store the last LED status, to toggle the light
int tiltState;            // the current reading from the input pin
int lastTiltState = HIGH; // the previous reading from the input pin
int switchstatus;

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
 pinMode(ledPin, OUTPUT);
 pinMode(tiltSensor, INPUT);

 Spark.variable("door", &sensorReading, INT);
 Spark.variable("switch", &switchstatus, INT);

}

void loop() {
  sensorReading = digitalRead(tiltSensor);
  switchstatus = digitalRead(switchpin);

  if (sensorReading != lastTiltState) {
    lastDebounceTime = millis();
  }
   if ((millis() - lastDebounceTime) > debounceDelay) {

    if (sensorReading != tiltState){
      tiltState = sensorReading;

      if (sensorReading == LOW & switchstatus == HIGH) {
        ledState = HIGH;
        digitalWrite(ledPin, ledState);
        delay(500);
        Spark.publish("Door","opened!",60,PRIVATE);
      }

      else if (sensorReading == HIGH){
        ledState = LOW;
        digitalWrite(ledPin, ledState);
      }
    }
   }
  lastTiltState = sensorReading;
  delay(100);
}
Click to Expand

Content Rating

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

0