Button debouncing, multiple functions, same buttons

Hello!
I'm trying to build a program for two TM1637 displays and an RTC; I have time / date setting functions, but the buttons work a bit hard, and I should fix this. For example:

void DisplaySetMinute();
{
  Display1.clear();
  if (digitalRead(P2) == LOW) {
    if (myMinute == 59) {
      myMinute = 0;
    } else {
      myMinute = myMinute + 1;
    }
  }
  if (digitalRead(P3) == LOW) {
    if (myMinute == 0) {
      myMinute = 59;
    } else {
      myMinute = myMinute - 1;
    }
  }
  Display1.showNumberDec((myMinute / 10) % 10, false, 1, 2);
  Display1.showNumberDec(myMinute % 10, false, 1, 3);
  delay(200);
}

void DisplaySetMinute();
{
  Display1.clear();
  if (millis() - lastDebounceTime > debounceDelay) { // 50 ms
    if (readingP2 != buttonState) {
      buttonState = readingP2;
      if (digitalRead(P2) == HIGH) {
        if (myMinute == 59) {
          myMinute = 0;
        } else {
          myMinute = myMinute + 1;
        }
      }
    }
  }

  lastButtonState = readingP2;
  if (millis() - lastDebounceTime > debounceDelay) {
    if (readingP3 != buttonState) {
      buttonState = readingP3;
      if (digitalRead(P3) == HIGH) {
        if (myMinute == 0) {
          myMinute = 59;
        } else {
          myMinute = myMinute - 1;
        }
      }
    }
  }
  lastButtonState = readingP3;
  Display1.showNumberDec((myMinute / 10) % 10, false, 1, 2);
  Display1.showNumberDec(myMinute % 10, false, 1, 3);
  //delay(200);
}

In my mind, I said that I have to do debouncing in each function, but with the same buttons I think that the states are interspersed, because I can't set anything on the display anymore.
How do you suggest to accomplish debouncing in this case?
sketch_oct10a.ino (5.4 KB)

Don’t post snippets (Snippets R Us!)

Don't get me wrong, I write the program as a hobby, and what I put in the program, from various sources of info, may not seem easy to read, but it is quite rudimentary.
You can find the entire sketch above in the post.

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