Skip to content

using setDebounceTime() causes that isPressed() evaluates to true two times #10

Closed
@StefanL38

Description

@StefanL38

Here is a demo-code that demonstrates it
// start of macros dbg and dbgi
#define dbg(myFixedText, variableName)
Serial.print( F(#myFixedText " " #variableName"=") );
Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope

#define dbgi(myFixedText, variableName,timeInterval)
do {
static unsigned long intervalStartTime;
if ( millis() - intervalStartTime >= timeInterval ){
intervalStartTime = millis();
Serial.print( F(#myFixedText " " #variableName"=") );
Serial.println(variableName);
}
} while (false);
// usage: dbgi("2:my fixed text",myVariable,1000);
// myVariable can be any variable or expression that is defined in scope
// third parameter is the time in milliseconds that must pass by until the next time a
// Serial.print is executed
// end of macros dbg and dbgi

#include <Button.h>
#include <Stepper.h>

#define LOOP_STATE_STOPPED 0
#define LOOP_STATE_STARTED 1

#define STEPS 2038 // (28BYJ-48)

Stepper stepper(STEPS, 8, 10, 9, 11);

const int myButton_pin = 7;

Button myButton(myButton_pin); // pin 7;
int loopState = LOOP_STATE_STOPPED;

void setup() {
Serial.begin(115200);
Serial.println( F("Setup-Start") );

myButton.setDebounceTime(50); // set debounce time to 50 milliseconds
}

void loop() {

myButton.loop();

if (myButton.isPressed()) {
dbgi("myButton.isPressed() is TRUE!",loopState,1000);
if (loopState == LOOP_STATE_STOPPED) {
loopState = LOOP_STATE_STARTED;
dbg("loopState = LOOP_STATE_STARTED",loopState);
}
else { // if(loopState == LOOP_STATE_STARTED)
loopState = LOOP_STATE_STOPPED;
dbg("loopState = LOOP_STATE_STOPPED",loopState);
}
}

if (loopState == LOOP_STATE_STARTED) {
dbg("if (loopState == LOOP_STATE_STARTED) delay(1000)...",loopState);

delay(1000);
stepper.setSpeed(400);             // 8 rpm
stepper.step(1019);              // 1019 steps half (for testing)
loopState = LOOP_STATE_STOPPED;  //stop ste
dbg("if (loopState == LOOP_STATE_STARTED) loopState = LOOP_STATE_STOPPED",loopState);

digitalWrite(8, LOW);  //turn off stepper hold to reduce power usage.
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
dbg("end of if (loopState == LOOP_STATE_STARTED",loopState); 

}
dbgi("End of loop/n/n",loopState,1000);
}

What the heck is causing this??

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions