user77752: I'm running into issue properly reading a button connected to a digital I/O pin on my Adafruit Feather HUZZAH ESP8266 device.
I've implemented the debouncing script exactly as the Arduino provided example shows, but if I add a print statement to my setup loop:
void setup() {
  Serial.begin(230400);
  Serial.println("Starting");
  pinMode(buttonPin, INPUT_PULLUP);
}

I trigger a reading of the switch on every reset cycle. Commenting out the print statement will avoid the problem so it doesn't appear to be related to the debouncing script.
In addition, I can replicate the problem on any digital I/O pin even when there is no pin connected. Once I get past the setup function, everything works as intended. Here's my whole setup code (again, copied from the example):
const int buttonPin = 13;
int buttonState;             
int lastButtonState = LOW;   

unsigned long lastDebounceTime = 0;  
unsigned long debounceDelay = 50;    

void setup() {
  Serial.begin(230400);
  Serial.println("Starting");
  pinMode(buttonPin, INPUT_PULLUP);
  lastDebounceTime = millis(); 

}

void loop() {
  int reading = digitalRead(buttonPin);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      Serial.println("A");
    }
  }

  lastButtonState = reading;
}


And here's the output of my serial monitor (with no delays on reset):
Starting
A

I can avoid this by just ignoring the first button switch but it seems odd that this is happening and I can't find any explanations for what's going on.

user77752: Initializing my buttonState solved my problem:
int buttonState = LOW;    


user68310: You need to initialise buttonState to the current signal state and initialise lastDebounceTime to the current time, something like this:
int buttonState = digitalRead(buttonPin);
unsigned long lastDebounceTime = millis();

Compare with this very simple debouncer I put on GitHub.

77752: <p>I'm running into issue properly reading a button connected to a digital I/O pin on my Adafruit Feather HUZZAH ESP8266 device.</p>
<p>I've implemented the debouncing script exactly as the Arduino provided example shows, but if I add a print statement to my setup loop:</p>
<pre><code>void setup() {
  Serial.begin(230400);
  Serial.println(&quot;Starting&quot;);
  pinMode(buttonPin, INPUT_PULLUP);
}
</code></pre>
<p>I trigger a reading of the switch on every reset cycle. Commenting out the print statement will avoid the problem so it doesn't appear to be related to the debouncing script.</p>
<p>In addition, I can replicate the problem on <em>any</em> digital I/O pin even when there is no pin connected. Once I get past the setup function, everything works as intended. Here's my whole setup code (again, copied from the example):</p>
<pre><code>const int buttonPin = 13;
int buttonState;             
int lastButtonState = LOW;   

unsigned long lastDebounceTime = 0;  
unsigned long debounceDelay = 50;    

void setup() {
  Serial.begin(230400);
  Serial.println(&quot;Starting&quot;);
  pinMode(buttonPin, INPUT_PULLUP);
  lastDebounceTime = millis(); 

}

void loop() {
  int reading = digitalRead(buttonPin);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) &gt; debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      Serial.println(&quot;A&quot;);
    }
  }

  lastButtonState = reading;
}

</code></pre>
<p>And here's the output of my serial monitor (with no delays on reset):</p>
<pre><code>Starting
A
</code></pre>
<p>I can avoid this by just ignoring the first button switch but it seems odd that this is happening and I can't find any explanations for what's going on.</p>
 77752: <p>Initializing my buttonState solved my problem:</p>
<pre><code>int buttonState = LOW;    
</code></pre>
 68310: <p>You need to initialise <code>buttonState</code> to the current signal state and initialise <code>lastDebounceTime</code> to the current time, something like this:</p>
<pre class="lang-cpp prettyprint-override"><code>int buttonState = digitalRead(buttonPin);
unsigned long lastDebounceTime = millis();
</code></pre>
<p>Compare with this very simple <a href="http://github.com/TimMathias/Arduino-Debouncer" rel="nofollow noreferrer">debouncer</a> I put on GitHub.</p>
