Start stop with delay

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

Ok, what do you need help with?
Paul

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

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

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.

Probably, the following one of Fig-1:

sw1led1
Figure-1:

You youngsters have a crystal ball.

:nerd_face:

sw1led1

We really need to get a LED symbol. :wink:

Is this all your program needs to do?

These two tutorials should help:

StateChangeDetection
BlinkWithoutDelay

1. Hardware setup (Fig-1) using Button, Led, Pot1, and UNO
sw1led1
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)

#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)

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

<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

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?

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

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

//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()


“ 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?

They can remove this section if need be:

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

The OP's requirement and problem are statd above.

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.

// 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++.

1 Like

Seems a 555 timer module would do this quite well. :sunglasses:

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?

They can remove this section if need be:

    //*************
    //when the LED is ON, was the switch pushed ?
. . .
1 Like