evert-arias / EasyButton

Arduino library for debouncing momentary contact switches, detect press, release, long press and sequences with event definitions and callbacks.
https://easybtn.earias.me
MIT License
447 stars 62 forks source link

Delay in Reading #65

Open Abhesheksh opened 2 years ago

Abhesheksh commented 2 years ago

Here is my code below. I am using the Easybutton library for reading the button state. Problem is that in the loop function, even when I press my button for 2 seconds, it does not detect that and takes more time to detect, and even the detection is unreliable. I am sure there is a problem in my code, just can't figure it out. Please help.

Platform : NodeMCU IDE : Arduino

evert-arias commented 2 years ago

Will take a look

JFClaeys commented 2 years ago

I checked the submitted code and the very first thing that I noticed is the use of Delay(xx).
There is no need of it apart controlling the blinking of LEDs.

While delays can be usefull sometimes, they have the side effect of stopping the cpu until amount of time is reached.

Instead, try to implement a status driven type of code, where you each task required a previous one is coded with a flag (status) indicating (or not) to perform it. Your while(googleFails < googleFailsMax and yahooFails < yahooFailsMax){ loop, for exemple, is a compact code that could be explosed in 2-3 methods all in the same loop. So you would need only one button read, a toggling led (based on a count of tick since last executed) type of method) and small methods who would be called when necessary.

So yes, the problem is the code: Delays and too compact code

Abhesheksh commented 2 years ago

I checked the submitted code and the very first thing that I noticed is the use of Delay(xx). There is no need of it apart controlling the blinking of LEDs.

While delays can be usefull sometimes, they have the side effect of stopping the cpu until amount of time is reached.

Instead, try to implement a status driven type of code, where you each task required a previous one is coded with a flag (status) indicating (or not) to perform it. Your while(googleFails < googleFailsMax and yahooFails < yahooFailsMax){ loop, for exemple, is a compact code that could be explosed in 2-3 methods all in the same loop. So you would need only one button read, a toggling led (based on a count of tick since last executed) type of method) and small methods who would be called when necessary.

So yes, the problem is the code: Delays and too compact code

Thanks, removed the delay functions and code now runs without issue.