Recognize short press or long press for analog buttons

Hi all!

I want use analog keypad for my project. I write this code to check buttons is pressed for short time or long time. I read analog pin and when change form 0 to another number( button is pressed) run a timer. when the button is release, check timer(for what the elapsed time) and button but...
I'm confused because short press not recognized

void key()
{
  byte button;

  //get the latest button pressed, also the buttonJustPressed, buttonJustReleased flags
  button = ReadButtons();
  // if the button state changes to pressed, remember the start time
  if (button != 0 && previous == 0 && (millis() - firstTime) > 200) {
    pressed_button = button;
    firstTime = millis();
  }

  millis_held = (millis() - firstTime);

  // This if statement is a basic debouncing tool, the button must be pushed for at least
  // 20milliseconds in a row for it to be considered as a push.
  if (millis_held > 20) {

    // check if the button was released since we last checked
    if (button == 0 && previous != 0) {

      if (millis_held < 1000 && pressed_button == 1) {
        lcd.clear();
        lcd.print("short right");
        pressed_button = 0;
        delay (1000);
      }

      if (millis_held < 1000 && pressed_button == 2) {
        lcd.clear();
        lcd.print("short up");
        pressed_button = 0;
        delay (1000);
      }

      if (millis_held < 1000 && pressed_button == 3) {
        lcd.clear();
        lcd.print("short down");
        pressed_button = 0;
        delay (1000);
      }

      if (millis_held < 1000 && pressed_button == 4) {
        lcd.clear();
        lcd.print("short left");
        pressed_button = 0;
        delay (1000);
      }

      if (millis_held < 1000 && pressed_button == 5) {
        lcd.clear();
        lcd.print("short select");
        pressed_button = 0;
        delay (1000);
      }


      if (millis_held >= 1000 && pressed_button == 1) {
        lcd.clear();
        lcd.print("long right");
        pressed_button = 0;
        delay (1000);
      }
      if (millis_held >= 1000 && pressed_button == 2) {
        lcd.clear();
        lcd.print("long up");
        pressed_button = 0;
        delay (1000);
      }
      if (millis_held >= 1000 && pressed_button == 3) {
        lcd.clear();
        lcd.print("long down");
        pressed_button = 0;
        delay (1000);
      }
      if (millis_held >= 1000 && pressed_button == 4) {
        lcd.clear();
        lcd.print("long left");
        pressed_button = 0;
        delay (1000);
      }
      if (millis_held >= 1000 && pressed_button == 5) {
        lcd.clear();
        lcd.print("long select");
        pressed_button = 0;
        delay (1000);
      }
    }
  }

  previous = button;
  prev_secs_held = secs_held;
}

I wonder if the concept in the code in this link would be of use

At the very least get all the delay()s out of your code. Have a look at how millis() is used to manage timing without blocking in several things at a time

...R

I wouldn't try to compare an analog read to zero, try < 4.

You don't really "run a timer", you record the time and compare against it later.

The way to think about this is every separate task you have to manage maintains its own state,
you make sure variables represent the state and update as needed - in particular when a certain
amount of time has elapsed since the state was entered, you can trigger another state change.

Here each button is a task, states include idle, being_pressed and depending on the time the
button is released you have detected a short or long press - just record the value of millis() as
the state goes from idle to being_pressed, and consult that when it changes back - you can add
software debouncing by adding more states.

The analog button library I wrote for my LCD shield buttons may give you ideas on how this can be done. The library is at the code repository link in the signature block below.

You need to conditionally execute:

millis_held = (millis() - firstTime);

As it is, you execute it every time the code loops.
You need to determine if a button is pressed before
doing this thousands of times, unconditionally.
Dwight