Actually the library works really well, but it seems that my code has an issue.
My goal is:
When I press and release a button the arduiono should print in serial "button pressed"
When I press and hold a button, the arduino should print in serial "button hold", when I release it after that, the arduino should send the message "button released".
My current code has the problem that it sends a "button pressed" message prior to "button hold" when I hold a button pressed from the beginning.
The code works well if I just press and release the button.
Any idea how I must change my code, so that my goal above is reached?
Many thanks in advance!
PaulS:
Not a clue, as you failed to post ALL of it.
What you want to do is trivial without using a library at all.
I only posted the relevant part of the code. What is missing is the library inclusion, which should be obvious and the hardware pin assignment which can vary and is not really important for the code behaviour.
I used the library because it has debouncing feature that I have not to code from scratch again in the sketch.
Pls feel free to post your trivial code without library use. Thx.
cesarius:
I only posted the relevant part of the code.
That's kind of okay but you should make it compilable! The rule is, only complete code so everybody can easily test it. You want help so you should put in the effort, not us
I must confess I did not read all rules. I am sorry.
This is the code for the button part of my project(the whole sketch is too long and is really not relevant for my issue).
I compiled the code and uploaded it to my Arduino and it reproduces my problem.
As stated before it uses this library: Link
I preferred the library because it keeps the sketch clean. If you push and hold the button there will be a "Button pressed" message before "Button is hold". I need to omit that "Button pressed" message if and only if the button was pressed AND hold. I hope I explained it well. I think the problem is in my algorithm not the library itself.
If you push and hold the button there will be a "Button pressed" message before "Button is hold". I need to omit that "Button pressed" message if and only if the button was pressed AND hold. I hope I explained it well.
You did describe what you want to do.
In order for the switch to be held, it must be pressed. The library determines that the switch has become pressed. When the switch does not become released within some time frame, the library notices that the switch is held.
Since your code is responsible for all the messages printed, you need to print them differently.
What you are doing now is asking the library if the switch has become pressed. If the library says yes, YOU print that the switch is pressed.
Then, you ask if the switch is held. If the library says yes, you print another message.
If you want different messages printed, YOU must change when you ask the library for data or when you print messages or both.
Sorry, that's indeed what I meant Forgot to add the else to the last part as well when I moved it around. But good to see you thought of that yourself now See it as a final test