How to implement long button presses.

I'm trying to use two button to increase and decrease a variable in my code.

I'm looking at getting the variable to gradually increase/decrease by 1 while a button is held down, until about 7 iterations has been reached when the increase/decrease changes to 5 for each iteration. I've tried lots of different things but currently can't get it to work without the variable either not changing at all, or it suddenly changes in the hundreds.

The code I started with to change the value by 1 per button press was this:

const int IncreaseReference = 14;

 if (digitalRead(IncreaseReference) == HIGH) {
  ReferencePressure = (ReferencePressure + 1);    
  delay(20);      }

Any suggestions would be greatly appreciated!

  1. Start with a good denounce function
  2. Lose the delay(20) ... Bad programming form. Use variables and the millis() function to track elapsed time. Zillions if examples via Google


Here is a library I wrote, it includes a long-press example sketch.