# Turning LED ON using a pushbutton and millis()

**URL:** <https://forum.arduino.cc/t/turning-led-on-using-a-pushbutton-and-millis/509052>\
**Category:** Programming\
**Created:** [February 18, 2018, 11:29am UTC](https://forum.arduino.cc/t/turning-led-on-using-a-pushbutton-and-millis/509052 "2018-02-18T11:29:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![codezero121](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@codezero121](https://forum.arduino.cc/u/codezero121)\
**Post date:** [February 18, 2018, 11:29am UTC](https://forum.arduino.cc/t/turning-led-on-using-a-pushbutton-and-millis/509052/1 "2018-02-18T11:29:11Z")

</div>

Hello everyone. I'm new to Arduino and I would like to ask for your help in creating a program that will turn on the LED for 5 seconds after a pushbutton is pressed using millis() instead of delay(). I could have just used delay but adding another pushbutton and LED with the same function makes the program not work as intended because only one of them get to turn on after pressing their respective pushbuttons. Thank you for reading and hoping for your kind help.

---

<div class="post-metadata">

**Author:** ![GreyArea](https://avatars.discourse-cdn.com/v4/letter/g/ecccb3/32.png) [@GreyArea](https://forum.arduino.cc/u/GreyArea)\
**Post date:** [February 18, 2018, 11:39am UTC](https://forum.arduino.cc/t/turning-led-on-using-a-pushbutton-and-millis/509052/2 "2018-02-18T11:39:21Z")

</div>

When you get the read signal for the push button, set a variable

```cpp
Buttonpushed=millis()

```

And turn your led on. Then all you need is to check

```cpp
if (millis()-buttonpushed >=5000){

```

And turn it off (hint...check it’s actually on first...)

---

<div class="post-metadata">

**Author:** ![codezero121](https://avatars.discourse-cdn.com/v4/letter/c/f4b2a3/32.png) [@codezero121](https://forum.arduino.cc/u/codezero121)\
**Post date:** [February 18, 2018, 12:25pm UTC](https://forum.arduino.cc/t/turning-led-on-using-a-pushbutton-and-millis/509052/3 "2018-02-18T12:25:08Z")

</div>

Thank you very much! I managed to do it using your hints.

---

<div class="post-metadata">

**Author:** ![LesserMole](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@LesserMole](https://forum.arduino.cc/u/LesserMole)\
**Post date:** [February 18, 2018, 1:37pm UTC](https://forum.arduino.cc/t/turning-led-on-using-a-pushbutton-and-millis/509052/4 "2018-02-18T13:37:15Z")

</div>

In the spirit of sharing, maybe you could post your final code to help the next soul with a similar need.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [February 18, 2018, 3:08pm UTC](https://forum.arduino.cc/t/turning-led-on-using-a-pushbutton-and-millis/509052/5 "2018-02-18T15:08:56Z")

</div>

> And turn it off (hint...check it's actually on first...)

Whilst it is neater to check whether it is turned on first neither the program or the LED will care if you turn it off every time through loop() once the period has elapsed.

What would be nice would be to detect when the button becomes pressed rather than when it is pressed. That way the time period would start immediately a button press was detected even if the button is held down.

---

<div class="post-metadata">

**Author:** ![GreyArea](https://avatars.discourse-cdn.com/v4/letter/g/ecccb3/32.png) [@GreyArea](https://forum.arduino.cc/u/GreyArea)\
**Post date:** [February 21, 2018, 2:19am UTC](https://forum.arduino.cc/t/turning-led-on-using-a-pushbutton-and-millis/509052/6 "2018-02-21T02:19:02Z")

</div>

I use latching buttons quite a lot. They don’t always latch so button and LED (or whatever) can get out of sync. Confusing when th latching switch is also illuminated, so looks like something is on when it’s not...or vice verse. It’s why I always put a check in.

---

<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:** [May 5, 2021, 11:01pm UTC](https://forum.arduino.cc/t/turning-led-on-using-a-pushbutton-and-millis/509052/7 "2021-05-05T23:01:06Z")

</div>

Closed on May 5, 2021, 11:01pm UTC


