Delay-Hold button

Hi, I'm a starter for Arduino and it's quite fun for now. I'm now doing a delay-hold button where the mechanism is when I push and hold the button for 10 second, LED will lights up and as soon I release the button, it will soon terminate the light without any delay. I've been doing the code but sadly, it does not terminate the light and it takes 10 seconds back to dim off. Please help. Thanks
sketch_may21g.ino (595 Bytes)


int YOUR_RESET_BUTTON_PIN = 2; 

void setup(){
  
  pinMode(YOUR_RESET_BUTTON_PIN, INPUT);
  pinMode(13, OUTPUT);

}

void loop(){

  while(digitalRead(YOUR_RESET_BUTTON_PIN) == HIGH){
    delay(10000);
    if(digitalRead(YOUR_RESET_BUTTON_PIN) == LOW){
      digitalWrite(13, LOW);
      break;
    }
    else if(digitalRead(YOUR_RESET_BUTTON_PIN) == HIGH){
      digitalWrite(13, HIGH);
    }
  }
}

Hello
do not use the delay() function. Use a timer() function based on the IDE example BWOD, it is always better.

hi, thanks for the reply. I try to use it yesterday. But it seems that many example on this forum that I have found use codes that needs to have a cycle (push and release) at the input button (for the system to be running), while for this project, the cycle ends (the button is released from hold condition) means the system running is terminated afterwards.

If you can provide any example for the hold condition, I would be grateful. again, thanks for the reply

The problem here is that, once the button has been pressed, you then need your sketch to start doing 2 things at once: 1. checking to see if the button has been released and 2. checking if 10 seconds has passed.

Using delay() "blocks" the code from doing anything else, even checking if the button is released. So you can't use delay() for timing the 10 second wait. Instead, you must use millis(). Plus, if the button is released in less than 10 seconds, you don't want to continue waiting for the remainder of the 10s. Using delay() prevents you doing that also.

Using millis() is a bit like using a stop-watch, except for 2 things: 1. it "ticks" 1000 times per second and 2. you can't reset it to zero, so if you want to use it to time 10s, you have to make a note of the time when the button was pressed, and then keep comparing the current time back to that time to figure out when 10s has passed.

Hello
I could do this, but you will never learn how to make a proper solution in a sketch for yourself, isn´t it?

alrighty, I'll try to code first, thanks for the reply

I'll try to code first yeah, later I'll post about it, thanks

using your analogy, I've learn about millis(); function today. Thank you to you two guys. Here's my code.

#define PUSHED HIGH

byte lastState;
unsigned long startMillis;
const byte myPin = 2;
int val;

void setup()
{
  pinMode(myPin, INPUT);
  pinMode(13, OUTPUT);
  val = 0;
}

void loop()
{

  byte thisState = digitalRead(myPin);

  if (lastState != thisState)
  {
    lastState = thisState;
    startMillis = millis();
  }
  else
  val = 0;

  if (lastState == PUSHED && millis() - startMillis >= 2000UL)
  val = 1;

  digitalWrite(13, val);

}
1 Like

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