Restart delay when press button

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

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

common-mistakes-with-code-and-posting-code

Welcome

Here is an example : t1024263.ino - Wokwi Arduino and ESP32 Simulator

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.

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

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);
}
}

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

thanks, very early, first day.

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);
  }
}

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.

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?

Blink without delay example in the IDE

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

Thanks, reading now mills explain

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

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

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)