Can we reset the on/off delay timer in arduino code

Hi,
I am working on my new code & i want to reset a delay timer with 1 in digital input
in mentioned below code

const int sPin1 = 8;
int sState1 = 0;     
const int sPin2 = 9;
int sState2 = 0;

const int b1 =  10;

void setup() {
  pinMode(10, OUTPUT);
  pinMode(8, INPUT); 
  pinMode(9, INPUT);
  }
void loop() {
  sState1 = digitalRead(sPin1);
  sState2 = digitalRead(sPin2);
    if (sState1 == 1)
  {digitalWrite(b1, 0);} 
  if (sState2 == 0)
  {digitalWrite(b1, 1);delay(100000000);}
     else
  if (sState1 == 0)
  {digitalWrite(b1, 1);} }

in this code i want to reset of delay(100000000) with sState1

Welcome to the forum

Once the delay(100000000) starts then nothing will happen for over 27 hours, including reading the inputs

Is that really what you want to do ?

What do you mean by "reset of delay"?

It is not possible to cancel delay(). For that reason, delay() is rarely used. Instead, people use other means of timing events, described in this tutorial on the Blink Without Delay program example.

I moved your topic to an appropriate forum category @kawal14feb.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

if you not able to solve your project with delay() then you should not using it

sounds like you want one button to start a timer and have a 2nd button that resets the timer before it expires

see how instead of using delay, millis() is used to see when the desired amount of time, msecPeriod has elapsed since the time when the button was pressed, msec0.

because millis() returns immediately, other things can be done while the timer code checks millis() each iteration of loop()

see how the timer is enabled by setting msecPeriod. the timer can easily be disabled by setting msecPeriod to zero because it's one of the 2 conditions (&&) to check the timer

const int sPin1 = 8;
const int sPin2 = 9;

#define OFF  LOW
#define ON   HIGH

unsigned long msecPeriod;
unsigned long msec0;

void loop ()
{
    unsigned long msec = millis ();

    // check timer
    if (msecPeriod && msec - msec0 >= msecPeriod)  {
        msecPeriod = 0;             // disable timer
        digitalWrite (LED_BUILTIN, OFF);
    }

    // check for start
    if (LOW == digitalRead (sPin2))  {
        msec0      = msec;
        msecPeriod = 5000;
        digitalWrite (LED_BUILTIN, ON);
    }

    // check to reset
    if (LOW == digitalRead (sPin1))  {
        msecPeriod = 0;             // disable timer
        digitalWrite (LED_BUILTIN, OFF);
    }
}

void setup ()
{
    Serial.begin (9600);
    pinMode (sPin1, INPUT);
    pinMode (sPin2, INPUT);

    pinMode (LED_BUILTIN, OUTPUT);
    digitalWrite (LED_BUILTIN, OFF);
}

And resets the state of b1.

1 Like

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