I am trying to use a while statement in void loop() to wait for a state change in a GPIO input. However, even though it compiles without error, I get stack overflows when it runs on my ESP8266 board, as seen in the serial monitor window. The device starts, gives the error message then reboots itself. I am posting the simple code and the error message:
- Tutorial page: Arduino - Door Sensor | Arduino Tutorial
*/
const int DOOR_SENSOR_PIN = 4; // Arduino pin connected to door sensor's pin
int doorState;
void setup() {
Serial.begin(115200); // initialize serial
pinMode(DOOR_SENSOR_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode
}
void loop() {
doorState = digitalRead(DOOR_SENSOR_PIN); // read state
while (digitalRead(DOOR_SENSOR_PIN)) { }
if (doorState == HIGH) {
Serial.println("The door is open");
} else {
Serial.println("The door is closed");
}
}
And here is the output I get:
Soft WDT reset
ctx: cont
sp: 3ffffda0 end: 3fffffd0 offset: 01b0
stack>>>
3fffff50: 40202400 3ffefa14 3ffef9e4 4020235e
3fffff60: 0001c200 0000001c 00000003 3ffef9fc
3fffff70: feefeffe feefeffe 3ffee85c 3ffee898
3fffff80: 3fffdad0 00000001 3ffee85c 402028f8
3fffff90: 0001c200 0000001c 00000000 feefeffe
3fffffa0: feefeffe 00000000 3ffee890 4020270b
3fffffb0: feefeffe feefeffe 3ffee890 40202c7c
3fffffc0: feefeffe feefeffe 3ffe85d8 401000e5
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,7)
ets Jan 8 2013,rst cause:4, boot mode:(1,7)
wdt reset
Can someone help this newbie with this? Thanks!