a RGB LED which help to understand which action has been executed
(for example if a long click has been taken into account)
3 examples are available : with one button, with 2 buttons and 2 buttons with an RGB light.
These examples display buttons events on serial :
- short press begins / short press released
- long press begins / long press released
- extra long press begins / extra long press released
- double press on one button
- 2 buttons pressed simultaneously
The LED example use an RGB led to show what happens to the user :
- single press = 3 green blinks
- double press = 3 OrangeRed blinks
- long press = red increasing light until 3 yellow blinks when it is taken into account
- dual press of 2 buttons = 3 blue blinks
So this third example require Fastled library and at least one RGB led compatible with Fastled (Neopixel, WS2801, WS2811, WS2812B...).
Fastled is a library easy to find in arduino IDE, Platformio or Github.
Led is connected on GPIO 2 by default.
By default examples use GPIO 13 for the one button example and 12 & 13 for the two buttons example.
Yes I know there are plenty of similar codes but as I will probably use it in my future projects I'm curious to have your opinion.
I know by experience that the criticisms are sometimes hard here so it is probably a good test and may be it will be useful for someone, who knows
thanks for sharing, always takes courage to show ones work.
one issue I see with your code is that it's not a class that you can easily integrate... seems you need to add the state machine function in your own code directly... not great for reuse and bug fixing - and no abstraction
from a code perspective, using a bool to store HIGH or LOW is the wrong C++ type and you could use an enum for this to make code more readable
// 0 : nothing , 1 = short press begins , 2 = short press released , 3 = long press begins
// 4 = long press released , 5 = extra long press begins , 6 = extra long press released , 7 = currently pushed
there are many other remarks possible on the code ranging from personal preference to memory impact optimisation but not sure that brings lots of value to discuss this here
The library I like the most is Button in easyRun. That library is memory conscious and you have various classes to choose from depending on the type of button you want . It also offers state machine capabilities.
The approach with a class helps you keep the code lean and simple, contrast this with your 1 button example