Yes but having the library already set for it (ie having functions that check if button is double clicked, long click etc) makes it easier to code (all you have to do is: if button has been clicked do this, if button has been doubleclicked do that, etc ) Unless I am still missing your point...
I know it is a month old, but I noticed the topic after a spammer bumped it and though I'd add a note that you might get a more readable flowchart if you ask ChatGPT to provide the Mermaid markup for the flowchart.
If I give it this prompt:
Give me the Mermaid markup for a flowchart of the following Arduino code. You must wrap the text in every node with quotes.
void ButtonControl::verifyButton(bool fast, bool doubleClick) {
_buttonTimes = 0;
_buttonClicks = 0;
_longClick = false;
while (digitalRead(_pin) == _signal) {
_buttonTimes++;
if (_buttonTimes >= _continuousTime && _continuous) break;
delay(1);
}
if (_buttonTimes > _debounceTime) {
_buttonClicks++;
if (fast) return;
if (_buttonTimes >= _longMinClickTime) {
_longClick = true;
} else {
bool loopClick = true;
while (loopClick) {
int timeNotPressedValue = 250;
int timeNotPressedCount = 0;
while (digitalRead(_pin) == !_signal) {
timeNotPressedCount++;
if (timeNotPressedCount >= timeNotPressedValue) {
loopClick = false;
break;
}
delay(1);
}
if (timeNotPressedCount < timeNotPressedValue) {
_buttonTimes = 0;
while (digitalRead(_pin) == _signal) {
_buttonTimes++;
delay(1);
}
if (_buttonTimes > 20) _buttonClicks++;
if (_buttonClicks == 2 && doubleClick) loopClick = false;
}
}
}
}
}
It (the "4o mini" model that is available without logging into an OpenAI account) responds with this Mermaid markup:
flowchart TD
A["Start"] --> B["Initialize variables: _buttonTimes = 0, _buttonClicks = 0, _longClick = false"]
B --> C["While (digitalRead(_pin) == _signal)"]
C --> D["Increment _buttonTimes"]
D --> E{"_buttonTimes >= _continuousTime && _continuous"}
E --> |"Yes"| F["Break loop"]
E --> |"No"| C
F --> G{"_buttonTimes > _debounceTime"}
G --> |"Yes"| H["Increment _buttonClicks"]
H --> I{"fast"}
I --> |"Yes"| J["Return"]
I --> |"No"| K{"_buttonTimes >= _longMinClickTime"}
K --> |"Yes"| L["Set _longClick = true"]
K --> |"No"| M["Set loopClick = true"]
L --> N["While (loopClick)"]
M --> N
N --> O["Initialize timeNotPressedValue = 250, timeNotPressedCount = 0"]
O --> P["While (digitalRead(_pin) == !_signal)"]
P --> Q["Increment timeNotPressedCount"]
Q --> R{"timeNotPressedCount >= timeNotPressedValue"}
R --> |"Yes"| S["Set loopClick = false"]
R --> |"No"| P
S --> T{"timeNotPressedCount < timeNotPressedValue"}
T --> |"Yes"| U["Set _buttonTimes = 0"]
T --> |"No"| N
U --> V["While (digitalRead(_pin) == _signal)"]
V --> W["Increment _buttonTimes"]
W --> X{"_buttonTimes > 20"}
X --> |"Yes"| Y["Increment _buttonClicks"]
X --> |"No"| N
Y --> Z{"_buttonClicks == 2 && doubleClick"}
Z --> |"Yes"| S
Z --> |"No"| N
You can view the rendered flowchart by pasting that into the Mermaid Live Editor (or any other Mermaid rendering tool or site such as GitHub that supports the markup):
I haven't personally found much value in ChatGPT (though I also haven't spent much time trying since I found it quite frustrating), but I have been impressed with its ability to generate Mermaid code
I support that a schematic assist the understanding of program logic.
Just to note that flowcharts are not the only type of schematic.
For example if your program is using a finite state machine, a state diagram is of better use.
You can have a look at the OneButton Library, it has a nice documentation
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
And imho this state diagram describes very well the program logic of that button library. Check it out.
Hi Guys thanks for your input. I am just on holiday at the moment and will look at this as soon as I am back. Cheers
Brilliant !! that's very useful! will certainly use this method when I will need some visual aid to understand the code. Thank you!
@noiasca Thanks a lot ! yes it definitively helps to have a visual reference ...
You are welcome.
If you are interested in flow charts (or other such types of diagrams), or will create them regularly, I think it is worth having a look at the Mermaid language for use in composing flow charts yourself (rather than only for use when getting ChatGPT to generate them). Mermaid has become the foremost flow chart markup language with the first class support on GitHub and other popular sites, tools, and frameworks. The design of the language is a bit clunky, but the code written by ChatGPT is not a good representation. When you write it yourself you can make the code significantly more readable than the code generated by ChatGPT.
Relay On/Off -- if the realy is an ouput device, then shoud the figure of the Flow Chart of post #48 be a parallelogram (Fig-1) instead of a rectangle?
Figure-1:
And for the retro look go to to your favorite antique tech store and get a flow chart template. Here is the one I used for years.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.