Nice. I have tested your sketch.
To reduce spam and since it is clearly going to do the state report, I removed
if (buttonState == LOW) {
Serial.println("I am pressed at the moment");
} else {
Serial.println("I am not pressed at the moment");
}
There are a few things I would change, but I suggest to you that we can leave those what are somewhat subtleties to a later time and possibly never.
But some low fruit. I've copied three lines and recommend you leave LOW and HIGH behind and embrace the bool variables for what they are. So here
bool buttonState = HIGH;
buttonState = digitalRead(buttonPin);
if (buttonState == LOW && lastButtonState == HIGH) {
instead
// start with
bool buttonState = false; // true is switch is pressed
// add a manifest constant
# define PRESSED LOW // switches are pulled up normally
// get the true/false condition of the button
buttonState = digitalRead(buttonPin) == PRESSED;
// use boolean values directly
if (buttonState && !lastButtonState) { // ... see the
Here's where a weakness of mine enters, I'm not so good at naming variables. buttonIsPressed and buttonWasPressed read better in this casting, but I really don't care for them. So you do you, I'll leave it as it is and just "know" buttonState true means it read as down when you did.
Good move reading the button once!
Now I will say you have a grip on the first step of any loop(), which is to gather all inputs. Nothing but reading the button(s), and where you now print, setting flags. A flag is simple a variable that takes on tow (usually) meanings, so
bool inProgrammingMode = false; // Dedicated flag
never mind! You already know this.
The next section of code should bifurcate on inProgrammingMode, and within each section the flags set for the buttons will be useful for doing one thing or another.
And here's where some flags should be cleared (reset to false). So if a button reading set the saw-short-press release flag, and you were in programming mode, you would, e.g., increment the index of the current parameter and then... clear the flag to keep say you've handled it.
I could go on; my sense of it is that you are catching my hang of it and can probably make some kinds of progress. I hate to say it, but if this makes insufficient sense, I have found feeding entire posts (and threads!) to chatGPT with a request for it to make a different kind of sense, you will get a better written version of what I am saying.
I'm not in the lab, but I will pay attention and if you wanna be sure, feel free to @alto777 when you post and I will get a notification when I tune in.
Meanwhile, take a short press victory lap.
Oh, speaking of long and short, now that you have reliable reporting, I suggest to you to tune down the excruciating 2000 millseconds hold time to qualify as long. A number, oh say 777 will be crisper yet long enough so getting off the button to mean short won't be hard at all. Ppl naturally stay on a button a hundred milliseconds or so, long only has to be a healthy bit longer than the fattest fingered button presser.
For what passes as fun in my empty life I worked a bit with this thread using chatGPT. A single sentence only of which I share here, I thought it was put very well:
Before you decide what a button should do, you need to be sure you know exactly what the button did.
HTH
a7