Closed gjt211 closed 5 years ago
Hello
I have no time now to go into this, maybe someone else can help?
From: gjt211 Sent: Thursday, September 27, 2018 12:07 PM To: avandalen/avdweb_Switch Cc: Subscribed Subject: [avandalen/avdweb_Switch] Prevent initial callback problem (#8)
Thank you for this great library, it is working very well for me with one small exception.
The first time the button.poll(); is called it always runs the setPushedCallback() function. In the code snippet below the terminal always shows "Middle" when the program starts. Is there anyway to prevent this? My push button is connected from the pin to ground (0V) and using the internal pull-up.
Switch buttonMiddle = 7;
void setup() { Serial.begin(115200); buttonMiddle.setPushedCallback(&buttonMiddleCallbackFunction, nullptr); }
void loop() { buttonMiddle.poll(); }
void buttonMiddleCallbackFunction(void* s) { Serial.println("Middle"); } — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.
I will look into it when I find the time.
I can not reproduce the problem in an ESP8266. Nonetheless, I observed that you declare the object without using the constructor. That may map uninitialized memory leading to erratic behavior.
Switch buttonMiddle = 7;
Please, try using the constructor, like in the examples, and report back notifying if it works so that we can close the issue.
Switch buttonMiddle = Switch(7);
Hi @avandalen and @Martin-Laclaustra
I am using a SAMD21 which should really not make any difference. If I run the example code, it works fine. I changed my code to use the constructor as suggested (thanks I did not pick up on this). I copied the example code exactly (apart from changing the button pin) into my code it still shows the issue.
I will attempt to recreate/copy my code in stages until the problem occurs so I can determine where/when the problem begins.
I won't close this yet as I will post my results which may help others in the future.
Make sure that you are using the latest version of the library (the one that includes "singleClick" event). It was available in Arduino IDE as 1.2.0-rc (= singleClick branch) and I updated it yesterday, merging it to master and releasing it as 1.2.0 (with the same code).
Also, check that it is not a hardware problem by using an external pull-up (just to make sure that is not the issue).
I am also having this problem with an arduino uno. I think I've tracked it down to the fact that it takes my sketch almost 3/4 of a second within the setup function to initialize the display and networking hardware I'm using, so I dropped down to using just your SwitchCallbackExample sketch to see if it was my hardware or if I could reproduce it simply...
When I run your sketch as is, everything works as expected; if I add delay(500);
anywhere within the setup()
function, then I see
Toggle: turned on
Toggle: turned off
Button: single click
Sent to the serial console before I've pressed any buttons myself.
I think the easiest fix would be if there were some way for the program to reset the state of your flags and stored times -- sort of a "I know I haven't polled you in a while, so let's start over with fresh timings".
I'll try and take a look at your code tomorrow and see if I can untangle it enough to see either a simple fix or add the reset function myself, but I figured I'd add my thoughts and experiences in case it suggests something to you that can be implemented more quickly/easily.
Do not take the pain. Thanks to your detailed description and test I can guess what the problem is.
I will do some tests when I get the time, but probably the following will fix the problem:
Go to line:
https://github.com/avandalen/avdweb_Switch/blob/c1d9ab6ca31747ce2f875cacd6e132165e640545/avdweb_Switch.cpp#L116
and add just below:
poll()
Sorry I didn't get back to you sooner; that fixed it, thanks!
Thank you for this great library, it is working very well for me with one small exception.
The first time the
button.poll();
is called it always runs thesetPushedCallback()
function. In the code snippet below the terminal always shows "Middle" when the program starts. Is there anyway to prevent this? My push button is connected from the pin to ground (0V) and using the internal pull-up.