i'm a new Arduino user and was having trouble using debouncing methods (with millis() timers) all day. it seems like the proper thing to do when using a push button toggle switch according to the book i'm reading and many online forms.
however, i just figured out a way to completely remove debouncing timer methods from my code and replace it with a simple buttonCount variable instead. basically, the state changes when the button's state has changed twice (push and release). it works perfect and i didn't need to resort to using timers or fear debouncing. so i assume, anyway. is my code below debounce proof? should i still include bedounce methods in the sketch rather than relying on my buttonCount variable?
//Constants
const int LED = 13;
const int BUTTON = 7;
const int DELAY = 25;
//Variables
int reading = LOW;
int previousReading = LOW;
int buttonCount = 0;
boolean state = false;
//Initialize
void setup()
{
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
}
//Execute
void loop()
{
reading = digitalRead(BUTTON);
if (reading != previousReading)
{
previousReading = reading;
buttonCount++;
}
if (buttonCount == 2)
{
state = !state;
buttonCount = 0;
}
switch (state)
{
case (true): strobe();
break;
case (false): digitalWrite(LED, LOW);
}
}
void strobe()
{
digitalWrite(LED, HIGH);
delay(DELAY);
digitalWrite(LED, LOW);
delay(DELAY);
}