Example for Button "Back/Next" what to show on Display

Hi,
pls could anybody post a example about two hardwired buttons for the function "Back Display" and "Next Display"?

A while ago I wrote a sketch by myself. But I don't know if it was correct because when I press the button it counted not just +1 but many times. I tried to solve this with another if condition. I would like to know how a professional programmer does it. Many thanks.

Something like that:

void loop() {

if button "back" then $ChoosenSubroutine = -1;
if button "next" then $ChoosenSubroutine = +1;
if ($ChoosenSubroutine) == 1 {subroutine displayInfo1();};
if ($ChoosenSubroutine) == 1 {subroutine displayInfo2();};

}

That sounds like bounce. When you press a button, it bounces resulting in multiple connects/disconnects before settling in the connected state; it can also happen when you release the button.

Have a look at the debounce example that comes with the IDE.

These examples that are delivered with the IDE do teach

bad programming !

It does not teach good programming
Why?

These examples teach how to stuff everything into loop() and does not show how to write code based on functions.

The debounce-example provided by the Arduino-IDE-examples leaves the newcomer scratching his head:
"OK it debounces.... but how do I implement a debounced button into my code?
Writing the rest of my code into inside here??

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;   //<=== show my action be inserted here ?????
      }
    }
  }

my opinion: It is a big timesaver in the long run to invest some more time into explaining the basics of functions and to provide some a bit more advanced examples that make consequent use of functions.
best regards Stefan

do somehting like that

// Setup a new OneButton on pin A1.  
OneButton button(PIN_INPUT, true);

lesson 1:
Posting parts of code is useless. Like explained in the forum how to: If you post a short compileable example the chances are very high someone explains it exactly to your need.

lesson 2:
you got pointed to the debounce example - study the example and try to implement.
https://docs.arduino.cc/built-in-examples/digital/Debounce

lesson 3:
to make things easier in the future, you can use one of several "button" libraries.
I like the "OneButton" library for example.
But learning how to debounce a button "manually" - will raise your programming experience and you can better understand what "Button" libraries are doing in the background.

I assume he/she will step through the 3 lessons and finally write a library on its own.
I'm not a pro, but I have written a simple toolkit which contains debounce buttons.

It has a very simplified "Button" class, but as already explained, learning how to debounce will help you in future.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.