else if(digitalRead(buttonPin) == 0){ //Door Closed
digitalWrite(ledPin, LOW);
x = 1;
Serial.println(digitalRead(buttonPin)); //Prints 0
delay(100);
}
because you are not looking for the state change in the above else if statement.
but you are here:
if ((digitalRead(buttonPin) == 1) && (x == 1))
recommend that you use a non-blocking version like this (untested):
const int ledPin = D7;
const int buttonPin = D0;
const int debounceTime = 50;
void setup()
{
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop()
{
int pressed = checkButton(buttonPin);
switch(pressed)
{
case 1:
Serial.println("Door Closed");
digitalWrite(ledPin, LOW);
// send message here
break;
case 2:
Serial.println("Door Open");
digitalWrite(ledPin, HIGH);
// send message here
break;
default:
// nothing to do here
break;
}
}
int checkButton(int pin)
{
static unsigned long lastTime = 0;
static bool lastState = false;
bool state = digitalRead(pin);
if(state != lastState && millis() - lastTime > debounceTime)
{
return state? 2 : 1;
lastTime = millis();
}
return 0;
}