When board powered up led turn on for 1min and meanwhile when button pressed turn off it immediately

Hi guys,
I want to write a program when board powered up turn on a led for a one minute and Meanwhile button press (push button) immediately turn off even before automaticallyturn off. How could I do that? I’ve heard something about millis() but I don’t know how to implement that.
Thanks

How can edit this..

#define LED_PIN 11
#define BUTTON_PIN 2


byte lastButtonState = LOW;
byte ledState = LOW;

unsigned long debounceDuration = 10; // millis
unsigned long lastTimeButtonStateChanged = 0;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  if (millis() - lastTimeButtonStateChanged > debounceDuration) {
    byte buttonState = digitalRead(BUTTON_PIN);
    if (buttonState != lastButtonState) {
      lastTimeButtonStateChanged = millis();
      lastButtonState = buttonState;
      if (buttonState == HIGH) {
        ledState = (ledState == HIGH) ? LOW: HIGH;
        digitalWrite(LED_PIN, ledState);
        
      }
    }
  }
}

Have a look at the blink without delay example in the IDE.
Post an attempt at your code with this in .

Example

1 Like

Hi, @shan784
Welcome to the forum.

What Arduino controller do you have?
Have you programmed your controller before?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

1 Like

Millis() is a timer that increments every millisecond and can be used to check progress in a sketch …so….
Turn your led on in setup and at the same time make some “unsigned long” variable , say NOW equal to millis.

In loop check to see if millis is then greater than NOW + 60000 , “if” it is turn the led off .
Also in loop ( next line …) check your button input ( button state ?) , “if” that has changed turn the led off .

I’ve deliberately not written the code here , that’s for you to try . It is always best to write your own code than adapt something you don’t understand

1 Like
  • a button switch is typically wired between the pin and ground, the pin configured as INPUT_PULLUP so that the pin is normally pulled HIGH and when the button is pressed the pin is pulled LOW

  • i usually initialize the button state in setup by reading the pin

  • i just add a 10 msec delay when a button is pressed to debounce

  • turn LED off if button pressed

  • need to turn the LED on in setup

  • should capture a timestamp in setup

  • loop() can use millis() to see if a minute has passed before turning the LED off

  • presumably reset board to start over

1 Like

The example-code that you posted uses the function millis() for debouncing the button.
This is something different that "waiting for a button-press to switch of an LED"

For the task "waiting for a button-press to switch of an LED" debouncing is not nescessary.

As a pseudo-code

in a arduino-board is powered up the function setup() is executed once

So inside function setup() you code:

switch LED ON

if power is supplied to the arduino-board the function loop() is doing what its name says:

looping infinitely.

So inside loop() you have to do two things
checking if the button is pressed
checking how much time has gone by since powerup

if button is pressed: switch off LED

if one minute has passed by: switch LED off.

The established users just link to this very unsatisfying BWD-example.

Here is a short tutorial that is easier to understand as the "standard" Blink-without-delay-example. (I'd like to call it "blink without explain" )

So please start reading the example-code and then ask specific questions if you do not understand something

best regards Stefan

1 Like

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