Why does the LED flash twice with one push and release of the button?
#include <Servo.h> Servo Stop_servo; // create servo object to control a servo // Buttons with Pull-Ups are "backwards" // Some DEFINEs to make reading code easier #define PUSHED false #define NOT_PUSHED true #define WATCH_BUTTON true #define IGNORE_BUTTON false // Time to wait for bounce, in MICROsconds const int buttonWaitInterval = 6000; // Pins for LED and Button const int LEDpin = 5; const int BUTTONpin = 2; float Minpos = 0; // variable to store the servo position float Maxpos = 255; //greatest travel of servo // Used to track how long between "bounces" unsigned long previousMicros = 0; // Used to track state of button (high or low) boolean previousButtonState = NOT_PUSHED; // Variable reporting de-bounced state. boolean debouncedButtonState = NOT_PUSHED; // Tracks if we are waiting for a "bounce" event boolean bounceState = false; // Nothing surprising here void setup() { pinMode(LEDpin, OUTPUT); pinMode(BUTTONpin, INPUT_PULLUP); digitalWrite(LEDpin, LOW); Stop_servo.attach(3); // attaches the servo on pin 3 to the servo object Stop_servo.write(0); Serial.begin(9600); Serial.println(F("Serial Initialized")); } void loop() { // This needs to be called periodically to // update the timers and button status updateButton(); // This replaces: digitalRead(BUTTONpin); //digitalWrite(LEDpin, debouncedButtonState); if (debouncedButtonState == LOW) { digitalWrite(LEDpin, HIGH); delay(500); digitalWrite(LEDpin, LOW); delay(500); Stop_servo.write(255); delay(5000); Stop_servo.write(0); Serial.println(F("servo cycled")); } } // All of the magic happens here void updateButton() { // We are waiting for any activity on the button if (bounceState == WATCH_BUTTON) { // Get and store current button state boolean currentButtonState = digitalRead(BUTTONpin); // Check to see if a transition has occured (and only one) if (previousButtonState != currentButtonState) { // A transition was detected, ignore the others for a while bounceState = IGNORE_BUTTON; // Store current time (start the clock) previousMicros = micros(); } // Keep storing existing button state, if we're watching previousButtonState = currentButtonState; } // We are waiting for the buttonWaitInterval to elapse if (bounceState == IGNORE_BUTTON) { // Compare current value of micros to previously stored, enough time yet? unsigned long currentMicros = micros(); if ((unsigned long)(currentMicros - previousMicros) >= buttonWaitInterval) { // Store the state of the button/pin to debouncedButtonState, which "reports" // the correct value. This allows for the code to handle active high or low inputs debouncedButtonState = digitalRead(BUTTONpin); // Go back to watching the button again. bounceState = WATCH_BUTTON; } } }
I'm trying to use Baldengineer debounce code to read a button and take an action. Each press of the button causes the LED to flash twice. The serial output reflects the routine running twice.
I have failed in my code changes to get the result I hope. I have looked at Internet and have found no hints to help.