Hold the push button for 5 seconds and then the led glows.

How to code for following condition :

Hold the push button for 5 seconds and then the led glows.

Kindly suggest with code.

Will something like this do?

void loop()  {
  static bool bButtonPressed = false;
  static unsigned long lButtonWait;

  // Check for switch press - switchStatus goes high if pressed
  int switchStatus = ReadSwitch();                // Could be just checking a pin

  if ( switchStatus && !bButtonPressed )  {
    lButtonWait = millis();
    bButtonPressed = true;
  }
  else  {
    if ( switchStatus && bButtonPressed )  {
      if ( (millis() - lButtonWait) >= 1200)  {   // Do what is require if button held for 1.2 sec      

        // Turn om LED if that is what is required
        
        bButtonPressed = false;
        while (ReadSwitch() > 0);                 // Wait for button release
      }
    } else {
      if ( bButtonPressed )  {

        // Do somthing if button is released before 1.2 seconds
        bButtonPressed = false;
        
      }
    }
  }
  
}

Willem

Will something like this do?

Probably not as it does not even compile

Probably not as it does not even compile

You mean I have to provide a complete working sketch?

I provided an outline of the loop function which should do what the OP asked (if I correctly understand the question), I thought that should be enough?

If you add a setup() and ReadSwitch() it does compile - it won't do anything since I have not included anything to do. I thought the OP can do that. I do not even know what board or processor the OP has.

Willem

You mean I have to provide a complete working sketch?

You are not obliged to provide anything, but a program that compiles is generally more helpful in my experience

You are not obliged to provide anything, but a program that compiles is generally more helpful in my experience

I cannot argue with that. ;)

OK. If the OP wants me to, I can add the rest to provide a complete sketch.

@Dnyaan Do you need more? What type of button do you want to use? Do you need it to do anything on a short press?

Willem

Any input pin will do as an example as will active HIGH or LOW for the button and the LED as long as the principle is sound. It is certainly not necessary to write the program to exactly meet the OPs hardware requirements unless you feel like doing it but to do so you would, of course, need more details

Thank you everyone for the help.. Appreciated!

The following is a simple way to check if a button is pressed continuously for a certain length of time

void loop() {
   buttonState = digitalRead(buttonPin);
   if (buttonState == HIGH) {   // assumes LOW when pressed and HIGH when not-pressed
       lastTimeButtonWasHigh = millis();
   }

   if (millis() - lastTimeButtonWasHigh >= requiredTime) {
       // input has been LOW throughout the required time
       // do whatever needs to be done
   }
}

...R

@Robin2

Nice one if you only require the long press. Could be adapted to also do a "click" press.

Most of the time I need the normal "click" press to do a certain thing but also a long hold to do some other more infrequent function.

Willem.

Willem43:
Most of the time I need the normal “click” press to do a certain thing but also a long hold to do some other more infrequent function.

If you have a different requirement from the OP then it would be a good idea to start your own Thread.

This link has code for different button-clicks

…R

No requirements, just a remark.

I have everything working just fine.

Willem.

[EDIT] Just checked that link of yours. I really like the code written by Jeff Saltzman. My debounce is rather rudimental, This solves that problem - will definitely make use of it.

Thanks.