# Restart delay when press button

**URL:** <https://forum.arduino.cc/t/restart-delay-when-press-button/1024263>\
**Category:** General Guidance\
**Created:** [August 21, 2022, 2:40pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263 "2022-08-21T14:40:51Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![arduleen0](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arduleen0](https://forum.arduino.cc/u/arduleen0)\
**Post date:** [August 21, 2022, 2:40pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/1 "2022-08-21T14:40:51Z")

</div>

```cpp
void loop(){
if (digitalRead(Button1) == LOW)
{
analogWrite(Led1, 50);
delay(10000);
analogWrite(Led1, 0);

```

Hallo,  
with this code, when push button Led powerOn than after 10 seconds power off

i need:  
if press button again, delay restart to 10 seconds

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [August 21, 2022, 2:58pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/2 "2022-08-21T14:58:03Z")

</div>

Please post the entire sketch, not a snippet of code.

[common-mistakes-with-code-and-posting-code](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966/1#common-mistakes-with-code-and-posting-code)

---

<div class="post-metadata">

**Author:** ![guix](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/guix/32/215371_2.png) [@guix](https://forum.arduino.cc/u/guix)\
**Post date:** [August 21, 2022, 3:20pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/3 "2022-08-21T15:20:44Z")

</div>

Welcome

Here is an example : [t1024263.ino - Wokwi Arduino and ESP32 Simulator](https://wokwi.com/projects/340615491281224276)

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [August 21, 2022, 3:20pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/4 "2022-08-21T15:20:54Z")

</div>

if you want to react to your button press while the led is on for 10 seconds, you can not use delay() since that blocks the processor from doing anything else while it is waiting. Look at the Blink Without Delay example in the IDE (File-\>examples-\>02.digital-\>BlinkWithoutDelay) to learn how to track elapsed time while still doing other things.

You probably also want to know **when** the button is pressed, not **if** the button is pressed.

---

<div class="post-metadata">

**Author:** ![pmagowan](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@pmagowan](https://forum.arduino.cc/u/pmagowan)\
**Post date:** [August 21, 2022, 3:24pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/5 "2022-08-21T15:24:22Z")

</div>

Welcome to the forum.

You are at an early stage in your arduino journey. You have fallen into the same trap that most beginners do and are using blocking code (delay). You need to think about and study:

millis timing (blink without delay example)  
state machines  
state change detection  
how the loop function works (as fast as possible)

Have fun

---

<div class="post-metadata">

**Author:** ![arduleen0](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arduleen0](https://forum.arduino.cc/u/arduleen0)\
**Post date:** [August 21, 2022, 3:25pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/6 "2022-08-21T15:25:38Z")

</div>

```cpp
int Button1 = 2;
int Led1 = 11;
//int ButtonState = 0;

void setup() {
  pinMode(Button1, INPUT_PULLUP);
  pinMode(Led1, OUTPUT); 
  //Serial.begin(9600);
}

void loop(){
if (digitalRead(Button1) == LOW)
{
analogWrite(Led1, 50);
delay(10000);
analogWrite(Led1, 0);
}
}

```

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [August 21, 2022, 3:31pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/7 "2022-08-21T15:31:41Z")

</div>

Looking on the bright side, at least your button is debounced. 🙂

---

<div class="post-metadata">

**Author:** ![arduleen0](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arduleen0](https://forum.arduino.cc/u/arduleen0)\
**Post date:** [August 21, 2022, 3:31pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/8 "2022-08-21T15:31:58Z")

</div>

thanks, very early, first day.

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [August 21, 2022, 3:33pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/9 "2022-08-21T15:33:03Z")

</div>

```cpp
const int Button1 = 2;
const int Led1 = 11;

int LastButtonState = HIGH;

boolean LEDOn = false;
unsigned long LEDOnStartTime = 0;

void setup()
{
  pinMode(Button1, INPUT_PULLUP);
  pinMode(Led1, OUTPUT);
}

void loop()
{
  int buttonState = digitalRead(Button1);
  if (buttonState != LastButtonState)
  {
    // Button state has changed
    LastButtonState = buttonState;
    if (buttonState == LOW)
    {
      // Button was just pressed
      analogWrite(Led1, 50);
      LEDOn = true;
      LEDOnStartTime = millis();
    }

  }

  // Time to turn off the LED?
  if (LEDOn && millis() - LEDOnStartTime >= 10000)
  {
    LEDOn = false;
    analogWrite(Led1, 0);
  }
}
```

---

<div class="post-metadata">

**Author:** ![pmagowan](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@pmagowan](https://forum.arduino.cc/u/pmagowan)\
**Post date:** [August 21, 2022, 3:40pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/10 "2022-08-21T15:40:01Z")

</div>

Probably the most important concept for you to grasp is the loop function. It is named appropriately. It loops round and round very fast, thousands of times a second if you code well. You are currently writing linear code rather than looping code. When using 'delay' you are telling the microcontroller to not do what it is designed to (super fast machine-speed looping) but to start doing things in a human timescale (very slooooooooowwwww for a machine). This is intuitive to us humans but entirely misses the point of a microcontroller. You need to get rid of blocking code and instead use millis (the microcontrollers clock) to check the time every loop and then work out if the required time has elapsed for you to do something such as turn on your LED. This is slightly more complicated than it first appears because the loop will just keep doing the same thing each time so you need to understand state machines. These are just a variable that you change to store what state you want your code to be in and depending on the state determines what code you run.

---

<div class="post-metadata">

**Author:** ![arduleen0](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arduleen0](https://forum.arduino.cc/u/arduleen0)\
**Post date:** [August 21, 2022, 3:43pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/11 "2022-08-21T15:43:01Z")

</div>

thanks, worked,  
next step for me is to understund this code, where i can found guide?

or please, can you add //some explain to the code?

---

<div class="post-metadata">

**Author:** ![pmagowan](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@pmagowan](https://forum.arduino.cc/u/pmagowan)\
**Post date:** [August 21, 2022, 3:47pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/12 "2022-08-21T15:47:33Z")

</div>

Blink without delay example in the IDE

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [August 21, 2022, 3:50pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/13 "2022-08-21T15:50:49Z")

</div>

> [@arduleen0](#):
>
> can you add //some explain to the code?

The code already contains comments. Have you tried to understand the code? What part do you not understand?

---

<div class="post-metadata">

**Author:** ![arduleen0](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arduleen0](https://forum.arduino.cc/u/arduleen0)\
**Post date:** [August 21, 2022, 4:02pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/14 "2022-08-21T16:02:24Z")

</div>

Thanks, reading now mills explain

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [August 21, 2022, 4:06pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/15 "2022-08-21T16:06:31Z")

</div>

There is nothing hidden or mysterious about millis(). It is just a continuously running clock. That is all.

---

<div class="post-metadata">

**Author:** ![pmagowan](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@pmagowan](https://forum.arduino.cc/u/pmagowan)\
**Post date:** [August 21, 2022, 4:50pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/16 "2022-08-21T16:50:50Z")

</div>

Millis timer:

To start timer check millis and record in a variable eg startTime(check clock and remember time).  
Decide on delay time and set in a variable eg delayTime is 10 seconds (how long you want the timer to run for)  
Each time through loop check the time elapsed and see if it is greater or equal to the delayTime (is time up) eg current time minus startTime is \>= delayTime

Look up if statements  
[if - Arduino Reference](https://www.arduino.cc/reference/en/language/structure/control-structure/if/)

State machine:  
set state in a global variable eg state is 0  
change state when a condition is met eg if timer is up state =1  
if state is 0 do something  
else if state is 1 do something else.

Look out for your variables being set in loop every cycle. You need to remember that code runs every loop unless it is protected by being part of another scope (outside the {} of loop or inside the {} of a block of code that is not going to run such as an if statement where condition is not met)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 17, 2023, 4:51pm UTC](https://forum.arduino.cc/t/restart-delay-when-press-button/1024263/17 "2023-02-17T16:51:41Z")

</div>

Closed on February 17, 2023, 4:51pm UTC

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