Button: short press vs long press, best way to do it ?

Hi all,

I wondering what is the best way to manage short press vs long press on a button.

I'm using a single button for two function: reset the values with a short press and reset/changing mode with a long press.

Here is the code I've done by myself:

//RESET BUTTON
  if (bt_reset.isPressed()) //test button
  {
    delay(200); //no bounce delay
    if (!bt_reset.stateChanged()) //if button still pressed
    {
      delay(300); //waiting a little more for long press
      if (!bt_reset.stateChanged()) //if button still pressed
      {
        is_rotor=!is_rotor; //changing mode
        EEPROM.write(1, is_rotor); //Eeprom save
      }
    }
    for (int i = 0; i < 3 ; i++) //reset values
    {
      max_pressure_PSI[i] = 0; // set all measures to 0
      max_pressure_bars[i] = 0; // set all measures to 0
      rpm[i] = 0; //set all rpm to 0
    }
    max_pressure_OK = false; //reset bolean
    rotor_face = 0; //reset rotor face
  }

I have a small issue with this: when still pressing the button continuously the boolean is_rotor is changing every 0.5 seconds.
Is it a good way to check short vs long?
How to avoid the value for changing every 0.5 seconds ?

Thank you :slight_smile:

Try using the following code.
Set your key press duration as per your requirement and set your reset pin accordingly.

long prev = 0;
int buttonState = 0;  // 0 = not pressed   --- 1 = long pressed --- 2 short pressed
int YOUR_RESET_BUTTON_PIN = 13; 
int DURATION_IN_MILLIS = 1000; 
void setup{
  pinMode(YOUR_RESET_BUTTON_PIN, INPUT);
}

void loop(){
  buttonState = 0;
  if(digitalRead(YOUR_RESET_BUTTON_PIN)){
    prev = millis();
    buttonState = 1;
    while((millis()-prev)<=DURATION_IN_MILLIS){
      if(!(digitalRead(YOUR_RESET_BUTTON_PIN))){
        buttonState = 2;
        break;
      }
    }
  }
  
  if(!buttonState){
    // TODO nothing is pressed
  }else if(buttonState == 1){
    // TODO button is pressed long
  }else if(buttonSTate ==2){
    //TODO button is pressed short
  }
  
}

Also, you need to add a small capacitor for hardware debounce.That way, you'll get the best results.
I didn't test it but it would work for you

I have made a similar sketch.

const byte buttonPin = 2;     
const byte ledPin =  11;

byte buttonState = 0;
byte  lastReading = 0;
unsigned long onTime=0;
int count = 0;

void setup() 
{ 
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);  
}

void loop()
{ 
  buttonState = digitalRead(buttonPin);

  // Quick presses
  if (buttonState == HIGH && lastReading == LOW) 
  {
    onTime = millis();
    count++;
  }

  //button being held for half second 
  if (buttonState == HIGH && lastReading == HIGH) 
  { 
    if ((millis() - onTime) > 500 ) // half second hold time
    {
      delay(200); // this delay is just for "count"
      count++;
      lastReading = LOW;
    } 
  }
  Serial.println(count);
  lastReading = buttonState;
}

If you want to run different functions depending on the type of press, then take a look at this HERE

two differents way, two interesting sketchs.

But I have to say the Improved Button Library is perfect for my project.
Thank you :slight_smile: