If statement conditional timer

Hey im not quite sure how I would even start this. I need my if statment to run when a pin goes low but if the pin gose high within 15s to cancel the whole statment.

if (accps == LOW && Pwr2 == HIGH) {
AutoOFF();
}

What can i add to this so it will wait 15s for a accps high signal and then not finnish the if statment. Thanks in advance for the help

1 Like

Use the millis() function (measures milliseconds since your program began to run) as follows:

if (accps == LOW) timeNow = millis();

timeLater = timeNow + 15000; //15 seconds later

while (millisec() < timeLater)
{
if ((accps == LOW) && (Pwr2 == HIGH)) AutoOFF();
}

Thanks for the help but im having an issue incorporating your inputs into my sketch. Im getting an error time now not decladed.

EvoX:
Thanks for the help but im having an issue incorporating your inputs into my sketch. Im getting an error time now not decladed.

You need to post your program code if you want advice about it.

...R

jrdoner:
Use the millis() function (measures milliseconds since your program began to run) as follows:

if (accps == LOW) timeNow = millis();

timeLater = timeNow + 15000; //15 seconds later

while (millisec() < timeLater)
{
if ((accps == LOW) && (Pwr2 == HIGH)) AutoOFF();
}

That while (millis() < later) is not going to work past the rollover. Sure it may be more than a month away but sure enough someone is going to use it long enough to find out the hard way.

The way that always works is to save start time and interval then

void loop()
{
.......
.......
if ( millis() - start time < interval ) // not a loop because that blocks loop() and all that's in it.
{
// run until time is up code here
}
.........
..........
}

If you don't block loop() then you can have other things running, code pieces that act independent.

This lets you write code for your sensors to be continually read and debug that little piece finished done then write another little piece that takes the sensor data and makes decisions (debug alone, much easier than along with everything else) and finally output code that runs on data from the decision (aka orocess) code.
That's 3 pieces. Input. Process. Output. Each one can be replaced without needing to change the others.
Learn this, do this, and you will keep more hair.

Hey sir, thanks for the help. I read through the millis() introduction page on gammon.com but still a little confused. Here is what i have so far. I'm sure something is missing because this just seams to easy. With this code if the accps signal goes HIGH I don't think it will stop the statement.

unsigned long WaitAutoOff = millis();


void setup() {
  // put your setup code here, to run once:

}

void loop() {

  if (accps == LOW && Pwr2 == HIGH) millis() - WaitAutoOff >= 15000
  {
    AutoOFF();
  }
}

More like this to

unsigned long WaitAutoOff = millis();

byte accps, Pwr2; // pin reads? no pin numbers, no modes set


void setup() {
  // put your setup code here, to run once:

  Pwr2 = HIGH;

}

void loop() {

  if ( millis() - WaitAutoOff >= 15000UL ) // only check accps AND Pwr2 after 15 seconds
  {
    if (( accps == LOW ) && ( Pwr2 == HIGH ))
    {
      AutoOFF();
    }
  }
}

Hey guys, I need some help.trying to get my if statment to run when a pin goes low but if the pin gose high within 15s to cancel the whole statment. I read through the millis() introduction page on gammon.com but still a little confused. Here is what i have so far. I'm sure something is missing because this just seams to easy. With this code if the accps signal goes HIGH I don't think it will stop the statement.

unsigned long WaitAutoOff = millis();


void setup() {
  // put your setup code here, to run once:

}

void loop() {

  if ((accps == LOW && millis() - WaitAutoOff >= 15000) && Pwr2 == HIGH))
{
    AutoOFF();
  }
}

I don't undersant what do you mean with:

EvoX:
(...)if the pin gose high within 15s to cancel the whole statment(...)

You can't cancel a statement "within". What you can do is "abort" if 15s after start the pin goes HIGH again. But, for that you must explain what you are doing inside the AutoOFF(). Do this function let you abort the operation?
Other thing that you can do is wait 15s to see if the pin don't goes HIGH. But you only start the operation after 15s and not at the time you press the button (or whatever).

I see what your saying i could do something basic like this

void loop() {

  if (accps == LOW && Pwr2 == HIGH)
  {
    Delay (15000)
    }
   Else if ( accps == LOW)
   {
    AutoOFF();
  }
}

Auto off is just the funtion to turn everything off.accps is the acc power signal from my truck. When i turn the key off i want everything to power off but i want to wait 15s to see if the car is turned back on first.

Auto off is just the funtion to turn everything off.accps is the acc power signal from my truck. When i turn the key off i want everything to power off but i want to wait 15s to see if the car is turned back on first.

So if you knew at what time the key was turned off you would know when to actually turn it off because you do know how long you want to wait. During that wait time you can keep checking the key to see if it goes back on. Then abort actually turning off.

millis() returns the time, like looking at a watch. Unlike a watch, it tells the time by counting ticks. 1000 ticks per second. It starts counting when it is booted and an unsigned long variable can hold almost 50 days worth of ticks.

Have you tried looking at any of the samples in the ide?

File -> Examples -> 02Digital -> There's good stuff in here.

Why are you wasting people's time with two Threads about the same issue. Other Thread here

...R

@EvoX, do not cross-post. Threads merged.

EvoX:
I see what your saying i could do something basic like this

void loop() {

if (accps == LOW && Pwr2 == HIGH)
  {
    Delay (15000)
    }
  Else if ( accps == LOW)
  {
    AutoOFF();
  }
}




Auto off is just the funtion to turn everything off.accps is the acc power signal from my truck. When i turn the key off i want everything to power off but i want to wait 15s to see if the car is turned back on first.

Less than 10 minutes after I give you an answer, you start another thread?