# Start stop with delay

**URL:** <https://forum.arduino.cc/t/start-stop-with-delay/908129>\
**Category:** General Guidance\
**Created:** [September 22, 2021, 6:43pm UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129 "2021-09-22T18:43:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lordsmit](https://avatars.discourse-cdn.com/v4/letter/l/a9a28c/32.png) [@lordsmit](https://forum.arduino.cc/u/lordsmit)\
**Post date:** [September 22, 2021, 6:43pm UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/1 "2021-09-22T18:43:31Z")

</div>

Hi, Im new here and am looking for some guidance on a project I'm trying out.  
I have a state changing push button to control an output (pin13).  
I have a POT for time delay.  
once the button is pressed pin 13 goes high, waits for the time delay then pin 13 goes low,  
the problem I'm having ;

when the button is pushed ( PIN 13 HIGH, waits for TIMEDELAY , PIN 13 LOW ) ,

while PIN 13 is HIGH if button is pressed send PIN 13 LOW, set TIMEDELAY 0

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [September 22, 2021, 6:45pm UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/2 "2021-09-22T18:45:46Z")

</div>

Ok, what do you need help with?  
Paul

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [September 22, 2021, 6:45pm UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/3 "2021-09-22T18:45:49Z")

</div>

Post your codes with code tags (\</\>).

---

<div class="post-metadata">

**Author:** ![Railroader](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/railroader/32/274550_2.png) [@Railroader](https://forum.arduino.cc/u/Railroader)\
**Post date:** [September 22, 2021, 6:46pm UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/4 "2021-09-22T18:46:41Z")

</div>

Show the code You've got. Use the code tag symbol, \</\>, when pasting.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 22, 2021, 6:53pm UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/5 "2021-09-22T18:53:30Z")

</div>

Show us a good schematic of your circuit.  
Show us a good image of your ‘actual’ wiring.  
Give links to components.

* * *

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the **complete sketch**.

Use the \</\> icon from the ‘reply menu’ to attach the copied sketch.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [September 22, 2021, 7:19pm UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/6 "2021-09-22T19:19:08Z")

</div>

> [@LarryD](#):
>
> Show us a good schematic of your circuit.

Probably, the following one of Fig-1:

![sw1led1](https://europe1.discourse-cdn.com/arduino/original/4X/0/7/9/0790ebf9a44b481aaf27ac4f3bdcc9218038e971.png)  
Figure-1:

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 22, 2021, 7:21pm UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/7 "2021-09-22T19:21:55Z")

</div>

You youngsters have a crystal ball.

🤓

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 22, 2021, 7:28pm UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/8 "2021-09-22T19:28:18Z")

</div>

![sw1led1](https://europe1.discourse-cdn.com/arduino/original/4X/0/7/9/0790ebf9a44b481aaf27ac4f3bdcc9218038e971.png)

We really need to get a LED symbol. 😉

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [September 22, 2021, 7:31pm UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/9 "2021-09-22T19:31:15Z")

</div>

> [@lordsmit](#):
>
> when the button is pushed ( PIN 13 HIGH, waits for TIMEDELAY , PIN 13 LOW ) ,
> 
> while PIN 13 is HIGH if button is pressed send PIN 13 LOW, set TIMEDELAY 0

Is this all your program needs to do?

These two tutorials should help:

[StateChangeDetection](https://forum.arduino.cc/index.php?topic=711767.msg4782013#msg4782013)  
[BlinkWithoutDelay](https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay)

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [September 23, 2021, 6:00am UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/10 "2021-09-23T06:00:31Z")

</div>

> [@lordsmit](#):
>
> when the button is pushed ( PIN 13 HIGH, waits for TIMEDELAY , PIN 13 LOW ) ,
> 
> while PIN 13 is HIGH if button is pressed send PIN 13 LOW, set TIMEDELAY 0

**1.** Hardware setup (Fig-1) using Button, Led, Pot1, and UNO  
 ![sw1led1](https://europe1.discourse-cdn.com/arduino/original/4X/0/7/9/0790ebf9a44b481aaf27ac4f3bdcc9218038e971.png)  
Figure-1:

**2.** You want that when Button of Fig-1 is pressed, the Led should turn ON. The On-period is determined by the voltage level at A0-pin. If you press Button again when the Led is still ON, the MCU/program will ignore the Button action.

**Sketch** (tested on UNO)

```arduino
#define Led 13

void setup()
{
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  pinMode(Led, OUTPUT);
  analogReference(DEFAULT);
}

void loop()
{
  unsigned int timeDelay = map(analogRead(A0), 0, 1023, 0, 5000);//(5000/5)*3.3 =3 sec
  bool n = digitalRead(2);
  if (n == LOW) //Button is closed
  {
    digitalWrite(Led, HIGH);
    delay(timeDelay);
    digitalWrite(Led, LOW);
  }
}

```

**3.** Note that a mechanical swicth like Button of Fig-1 makes a lot of make-and-break (called bouncing) before settling at the final closed position. In such case, we can apply **debouncing** on the Button using **Debounce.h** Library. Debouncing does not stop bouncing; rather, it refers to waiting of about 50 ms (as is set in the Debounce.h Library) until the Button settles at the final closed position.

**Sketch:** (tested on UNO)

```arduino
#include<Debounce.h>
Debounce Button(2);
#define Led 13

void setup()
{
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  pinMode(Led, OUTPUT);
  analogReference(DEFAULT);
}

void loop()
{
  unsigned int timeDelay = map(analogRead(A0), 0, 1023, 0, 5000);//(5000/5)*3.3 =3 sec
  bool n = !Button.read(); //redas Button when it is settled at closed position
  //Serial.println(n);
  if (n == LOW) //Button is closed
  {
    digitalWrite(Led, HIGH);
    delay(timeDelay);
    digitalWrite(Led, LOW);
  }
}

```

---

<div class="post-metadata">

**Author:** ![lordsmit](https://avatars.discourse-cdn.com/v4/letter/l/a9a28c/32.png) [@lordsmit](https://forum.arduino.cc/u/lordsmit)\
**Post date:** [September 23, 2021, 6:04am UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/11 "2021-09-23T06:04:51Z")

</div>

> [@GolamMostafa](#):
>
> ```arduino
> 
> void setup()
> {
> Serial.begin(9600);
> pinMode(2, INPUT_PULLUP);
> pinMode(Led, OUTPUT);
> analogReference(DEFAULT);
> }
> 
> ```

\<int ledState = 0;  
int ledPin = 13;  
int buttonPin = 2;  
int buttonStatenew;  
int buttonStateold = 1;  
int potPin = A0;  
int potVal;  
int offTime;

void setup()  
{  
pinMode (ledPin, OUTPUT);  
pinMode (buttonPin, INPUT);  
pinMode (potPin, INPUT);

}

void loop()  
{  
potVal = digitalRead(potPin); //read pot val  
offTime = map(potVal, 0, 1023, 1, 60); //set offtime to 1-60  
offTime = offTime \* 1000.; //set offTime to seconds  
buttonStatenew = digitalRead(buttonPin);

if (buttonStateold == 0 && buttonStatenew == 1)  
{  
if (ledState == 0) {  
digitalWrite(ledPin, HIGH);  
ledState = 1;  
delay (offTime); //wait for offTime  
digitalWrite(ledPin, LOW); //turn led off  
}  
else  
{ digitalWrite(ledPin, LOW);  
ledState = 0;  
}  
}  
buttonStateold = buttonStatenew;  
}  
if (digitalRead(button) == true) {  
status = !status;  
digitalWrite(led, status);  
} while (digitalRead(button) == true);  
delay(50);  
}

> 

this is what I have so far im not sure where to put the time delay so if i press the button again it stops and the led goes off

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [September 23, 2021, 6:46am UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/13 "2021-09-23T06:46:13Z")

</div>

What do you think about the following sketch which is a reduced version of your sketch of Post-11. Have you written these codes or are copied?

```cpp
int ledPin = 13;
bool buttonStatenew = 1; //we have internal pull-up
int buttonPin = 2;
int potPin = A0;
int potVal;
int offTime;

void setup()
{
  pinMode (ledPin, OUTPUT);
  pinMode (buttonPin, INPUT_PULLUP); //internal pull-up
}

void loop()
{
  potVal = analogRead(potPin); //read pot val
  offTime = map(potVal, 0, 1023, 1, 5000); //set offtime to 1 ms - 5000 ms
  buttonStatenew = digitalRead(buttonPin);

  if (buttonStatenew == 0) //button is now closed
  {
    digitalWrite(ledPin, HIGH);
    //--------------------------------------
    delay(offTime);
    //---------------------------------------
    digitalWrite(ledPin, LOW); //turn led off
  }
}

```

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [September 23, 2021, 7:01am UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/14 "2021-09-23T07:01:19Z")

</div>

Hello  
My recommendation to code a sketch for this task by using the BlinkWithOutDelay example of the IDE.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 23, 2021, 7:09am UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/15 "2021-09-23T07:09:06Z")

</div>

```cpp
//https://forum.arduino.cc/t/start-stop-with-delay/908129?

#define PUSHED LOW
#define notPUSHED HIGH

#define LEDon HIGH
#define LEDoff LOW

const byte potPin = A0;

const byte ledPin = 13; //Pin13---[220R]---[A->|-K]---GND
const byte buttonPin = 2; //+5V---[50k internal PU resistor]---[Pin2]---[switch]---GND

byte ledState;
byte buttonStatenew;
byte buttonStateold = notPUSHED;

unsigned int potVal;
unsigned int offTime;
unsigned int offTimeLocked;

unsigned long ledMillis;
unsigned long switchMillis;

// *********************************************************
void setup()
{
  Serial.begin(9600);

  pinMode (ledPin, OUTPUT);

  pinMode (buttonPin, INPUT_PULLUP);

} //END of setup()

// *********************************************************
void loop()
{
  // ******************************
  //is it time to check the switches ?
  if (millis() - switchMillis >= 50)
  {
    //restart the TIMER
    switchMillis = millis();

    checkSwitches();
  }

  // ******************************
  potVal = analogRead(potPin);

  offTime = map(potVal, 0, 1023, 1, 60); //set offtime to 1-60
  offTime = offTime * 1000; //set offTime to seconds

  // ******************************
  //when the LED is on, is it time to turn it OFF ?
  if (ledState == 1 && millis() - ledMillis >= offTimeLocked)
  {
    //disables TIMER checking
    ledState = 0;

    digitalWrite(ledPin, LEDoff); 
  }

  // ******************************
  // Other non blocking code goes here
  // ******************************

} //END of loop()

// *********************************************************
void checkSwitches()
{
  buttonStatenew = digitalRead(buttonPin);

  // **********************************
  //was there a change in switch state ?
  if (buttonStateold != buttonStatenew)
  {
    //update to the new state
    buttonStateold = buttonStatenew;

    // *************
    //when the LED is OFF, was the switch pushed ?
    if (ledState == LEDoff && buttonStatenew == PUSHED)
    {
      digitalWrite(ledPin, LEDon);

      //lock in the delay time
      offTimeLocked = offTime;
      Serial.print("Delay in ms will be = ");
      Serial.println(offTimeLocked);

      //enable TIMER checking
      ledState = LEDon;

      //restart the TIMER
      ledMillis = millis();
    }

    // *************
    //when the LED is ON, was the switch pushed ?
    else if (ledState == LEDon && buttonStatenew == PUSHED)
    {
      //cancels the LED timing and turns off the LED
      digitalWrite(ledPin, LEDoff);

      //disable TIMER checking
      ledState = LEDoff;
    }
  }

} //END of checkSwitches()

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 23, 2021, 7:35am UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/17 "2021-09-23T07:35:08Z")

</div>

“ The OP was asking for a system where the Led will turn on when Button is pressed, and the Led will remain ON until the adjustable tmeDelay is exhausted. The program will ignore the closure of the Button during this timeDelay.

~~Then~~~~ should ~~~~the~~~~ solution ~~~~be~~~~ not ~~~~straight~~~~ forward ~~~~few~~~~ lines ~~~~of~~~~ codes ~~~~?~~ ”

> [@lordsmit](#):
>
> when the button is pushed ( PIN 13 HIGH, waits for TIMEDELAY , PIN 13 LOW ) ,
> 
> while PIN 13 is HIGH if button is pressed send PIN 13 LOW, set TIMEDELAY 0

_They can remove this section if need be:_

```arduino
    // *************
    //when the LED is ON, was the switch pushed ?
. . .

```

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [September 23, 2021, 7:47am UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/18 "2021-09-23T07:47:01Z")

</div>

> [@lordsmit](#):
>
> once the button is pressed pin 13 goes high, waits for the time delay then pin 13 goes low,  
> the problem I'm having ;
> 
> when the button is pushed ( PIN 13 HIGH, waits for TIMEDELAY , PIN 13 LOW ) ,
> 
> while PIN 13 is HIGH if button is pressed send PIN 13 LOW, set TIMEDELAY 0

The OP's requirement and problem are statd above.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [September 23, 2021, 8:51am UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/19 "2021-09-23T08:51:00Z")

</div>

> [@LarryD](#):
>
> ~~Then~~~~ should ~~~~the~~~~ solution ~~~~be~~~~ not ~~~~straight~~~~ forward ~~~~few~~~~ lines ~~~~of~~~~ codes ~~~~?~~ ”

Yes.  
This is my proposal for a solution based on the BWOD example.  
Please check this out to your requierments. The sketch is UNO tested.

```arduino
// BLOCK COMMENT
// ATTENTION: This Sketch contains elements of C++.
// https://www.learncpp.com/cpp-tutorial/
// https://forum.arduino.cc/t/start-stop-with-delay/908129
#define ProjectName "Start stop with delay"
// CONSTANT DEFINITION
// you may need to change these constants to your hardware and needs.
constexpr byte Button_ {A0}; // portPin o---|button|---GND
constexpr byte Analoque {A3};
constexpr long minTime {500};
constexpr long maxTime {5000};
void setup() {
  Serial.begin(9600);
  Serial.println(F("."));
  Serial.print(F("File : ")), Serial.println( __FILE__ );
  Serial.print(F("Date : ")), Serial.println( __DATE__ );
  Serial.print(F("Project: ")), Serial.println(ProjectName);
  pinMode (LED_BUILTIN, OUTPUT);
  pinMode(Button_, INPUT_PULLUP);
}
void loop () {
  unsigned long currentTime = millis();
  static unsigned long blinkLed;
  static bool control_;
  if (!digitalRead(Button_)&& !digitalRead(LED_BUILTIN)) {
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.println(F("LED on"));
    control_ = true;
    blinkLed = currentTime;
  }
  if (currentTime - blinkLed >= (unsigned long) map(analogRead(Analoque), 0, 1023, minTime, maxTime) && control_) {
    digitalWrite(LED_BUILTIN, LOW);
    Serial.println(F("LED off"));
    control_ = false;
  }
}

```

Have a nice day and enjoy coding in C++.

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [September 23, 2021, 3:00pm UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/20 "2021-09-23T15:00:40Z")

</div>

Seems a 555 timer module would do this quite well. 🕶  
[![](https://europe1.discourse-cdn.com/arduino/original/4X/1/5/1/1518f3320cba75ce8f9fb2f04e837aa1c927a27c.jpeg)](https://www.aliexpress.com/item/1005003285811892.html)

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [September 23, 2021, 3:12pm UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/21 "2021-09-23T15:12:00Z")

</div>

You are right.  
Sometimes addtional buttons and LEDs suddendly arose.  
In this case an Arduino an a sketch written in C++ is more practical solution, isn´t it?

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 23, 2021, 4:40pm UTC](https://forum.arduino.cc/t/start-stop-with-delay/908129/22 "2021-09-23T16:40:59Z")

</div>

_They can remove this section if need be:_

```cpp
    // *************
    //when the LED is ON, was the switch pushed ?
. . .

```

[Next page](https://forum.arduino.cc/t/start-stop-with-delay/908129.md?page=2)
