Arduino delay on timer

Hi, I am new to Arduino and I have been trying to create a sketch where I have to push and hold down a button and after a 10 second delay an output comes on and stays on until the button is released and it turns off without delay. I have been playing around with the debounce sketch but it toggles on alternate presses of the button and has a delay on release of the button which I don't want. I wonder if someone could help an old newbie.

Code in code tags, image of your project, schematic?

I am using the debounce sketch from the Arduino examples library.

Hello and good morning
Your sketch needs a button-handler incl. debouncing and a timer function based on the BLINKWITHOUTDELAY example of IDE.
Have a nice day and enjoy coding in C++.

p.s. Take some time for a search for this typical school assignment inside the forum.

@claude1959, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

===

Debouncing will always result in delays but you don't need it with the long 10 second wait that you have. You need a millis based approach. Below should work.

void loop()
{
  // start time when button became pressed.
  static uint32_t startTime;
  // remember if button is pressed or not
  static bool buttonIsPressed = false;

  byte buttonState = digitalRead(buttonPin);
  if (buttonState == LOW)
  {
    buttonIsPressed = true;
    startTime = millis();
  }
  else
  {
    buttonIsPressed = false;
    digitalWrite(somePin, LOW);
  }

  if (buttonIsPressed == true)
  {
    if (millis() - startTime >= 10000UL)
    {
      digitalWrite(somePin, HIGH);
    }
  }
}

Connect button between pin and GND and use pinMode(buttonPin, INPUT_PULLUP) in setup().

// Edit
There will be a slight 'delay' due to bounce before the startTime becomes stable. If you don't want that, you have to look at state change detection and detect when the button becomes pressed.

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