Off delay timer

i want to make simple circuit when i press the push button relay output work for 5 min then turn off could any one help me

1 Like

Hello davidrafek

Post your sketch, well formated, with well-tempered comments and in so called code tags "</>" and schematic to see how we can help.

Welcome to the forum

Is it the circuit or the sketch that you need help with ?

i want help with the sketch

What have you written so far? Post the code in code tags and let us know where you are stuck and we will try to help.

Which MCU are you using?

this is the code but it doesnt work propelly

const byte buttonPin = 2; // momentary button pin
const byte ledPin13=13; // LED (built-in on Uno)

unsigned long buttonPushedMillis; // when button was released
unsigned long ledTurnedOnAt; // when led was turned on
unsigned long turnOnDelay = 0; // wait to turn on LED (keep at 0 for this project)
unsigned long turnOffDelay = 5000; // Off-delay time (ms)
bool ledReady = false; // flag for when button is let go
bool ledState = false; // for LED is on or not.

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

long time = 0;         // the last time the output pin was toggled
long debounce = 1;   // the debounce time, increase if the output flickers

 
void setup() {
 pinMode(buttonPin, INPUT);
 pinMode(ledPin13, OUTPUT);
 digitalWrite(ledPin13, LOW);
}
 
void loop() {
 // get the time at the start of this loop()
 unsigned long currentMillis = millis(); 
 reading = digitalRead(buttonPin);
 
 // check the button
 if (digitalRead(buttonPin) == HIGH) {
  // update the time when button was pushed
  buttonPushedMillis = currentMillis;
  ledReady = true;
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  previous = reading;
 }
  
 // make sure this code isn't checked until after button has been let go
 if (ledReady) {
   //this is typical millis code here:
   if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
     // okay, enough time has passed since the button was let go.
     digitalWrite(ledPin13, HIGH);
     // setup our next "state"
     ledState = true;
     // save when the LED turned on
     ledTurnedOnAt = currentMillis;
     // wait for next button press
     ledReady = false;
   }
 }
  
 // see if we are watching for the time to turn off LED
 if (ledState) {
   // okay, led on, check for now long
   if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
     ledState = false;
     digitalWrite(ledPin13, LOW);
   }
 }
}

can be simpler


const byte PinButton = A1;
const byte PinLed    = LED_BUILTIN;

unsigned long msecPeriod;
unsigned long msecLst;

byte butLst;

enum { Off = LOW, On = HIGH };

// -----------------------------------------------------------------------------
void
loop (void)
{
    unsigned long msec = millis ();
    if (msecPeriod && msec - msecLst >= msecPeriod)  {
        digitalWrite (PinLed, LOW);
        msecPeriod = 0;
    }

    byte but = digitalRead (PinButton);
    if (butLst != but)  {
        butLst = but;
        delay (20);

        digitalWrite (PinLed, HIGH);
        msecPeriod = 3000;
    }
}

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

    pinMode (PinLed,    OUTPUT);
    pinMode (PinButton, INPUT_PULLUP);
    butLst = digitalRead (PinButton);
}

Needs to be pinMode(buttonPin, INPUT_PULLUP);

Needs to be if (digitalRead(buttonPin) == LOW)

Also connect your button to GND instead of +5V.

If you find this the solution, hit the solution box.

Just typing up my post when you did yours. :laughing:

@theeccentricgenius , you do not know how the OP has wired te button; there might very well be a pull-down resistor in which case your statements don't solve the problem.

1 Like

i want to skip any change any push button until time elapsed

based on my code, ignore button presses until msecPeriod becomes zero when the timer expires

code not working

i want to skip any change any push button until time elapsed

what code doesn't work?

const byte PinButton = A1;
const byte PinLed = LED_BUILTIN;

unsigned long msecPeriod;
unsigned long msecLst;

byte butLst;

enum { Off = LOW, On = HIGH };

// -----------------------------------------------------------------------------
void
loop (void)
{
unsigned long msec = millis ();
if (msecPeriod && msec - msecLst >= msecPeriod) {
digitalWrite (PinLed, LOW);
msecPeriod = 0;
}

byte but = digitalRead (PinButton);
if (butLst != but)  {
    butLst = but;
    delay (20);

    digitalWrite (PinLed, HIGH);
    msecPeriod = 3000;
}

}

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

pinMode (PinLed,    OUTPUT);
pinMode (PinButton, INPUT_PULLUP);
butLst = digitalRead (PinButton);

}

did you try

if (butLst != but && ! msecPeriod)  {

the problem that code works only for one time

msecLst wasn't being set along with msecPeriod

const byte PinButton = A1;
const byte PinLed    = LED_BUILTIN;

unsigned long msecPeriod;
unsigned long msecLst;

byte butLst;

enum { Off = LOW, On = HIGH };

// -----------------------------------------------------------------------------
void
loop (void)
{
    unsigned long msec = millis ();
    if (msecPeriod && msec - msecLst >= msecPeriod)  {
        Serial.println ("timer expired");
        digitalWrite (PinLed, LOW);
        msecPeriod = 0;
    }

    byte but = digitalRead (PinButton);
    if (butLst != but)  {
        butLst = but;
        delay (20);

        if (LOW == but && ! msecPeriod)  {
            Serial.println ("but press");
            digitalWrite (PinLed, HIGH);
            msecPeriod = 3000;
            msecLst    = msec;
        }
    }
}

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

    pinMode (PinLed,    OUTPUT);
    pinMode (PinButton, INPUT_PULLUP);
    butLst = digitalRead (PinButton);
}

i have code for lamp works for 10 sec after push button is released i want to ignore any change in the button until lamp is off

const byte buttonPin = 2; // momentary button pin
const byte ledPin13=13; // LED (built-in on Uno)

unsigned long buttonPushedMillis; // when button was released
unsigned long ledTurnedOnAt; // when led was turned on
unsigned long turnOnDelay = 0; // wait to turn on LED (keep at 0 for this project)
unsigned long turnOffDelay = 10000; // Off-delay time (ms)
bool ledReady = false; // flag for when button is let go
bool ledState = false; // for LED is on or not.

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

long time = 0;         // the last time the output pin was toggled
long debounce = 1;   // the debounce time, increase if the output flickers

 
void setup() {
 pinMode(buttonPin, INPUT_PULLUP);
 pinMode(ledPin13, OUTPUT);
 digitalWrite(ledPin13, LOW);
}
 
void loop() {
 // get the time at the start of this loop()
 unsigned long currentMillis = millis(); 
 reading = digitalRead(buttonPin);
 
 // check the button
 if (digitalRead(buttonPin) == LOW) {
  // update the time when button was pushed
  buttonPushedMillis = currentMillis;
  ledReady = true;
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  previous = reading;
 }
  
 // make sure this code isn't checked until after button has been let go
 if (ledReady) {
   //this is typical millis code here:
   if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
     // okay, enough time has passed since the button was let go.
     digitalWrite(ledPin13, HIGH);
     // setup our next "state"
     ledState = true;
     // save when the LED turned on
     ledTurnedOnAt = currentMillis;
     // wait for next button press
     ledReady = false;
   }
 }
  
 // see if we are watching for the time to turn off LED
 if (ledState) {
   // okay, led on, check for now long
   if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
     ledState = false;
     digitalWrite(ledPin13, LOW);
   }
 }
}