counting Magnetic door Sensor

Somewhere, you need to set lastButtonState to state.