Check for a certain period of time

Hi

I have an Arduino Uno R3, and I wanted to know if I could program what I am about to describe, and if yes, how would that be possible?

I want the timer of the Arduino to get triggered by a certain event. Then the timer should keep going for a, say, 15 minute period. However, if another event occurs (let us say some value returned false), it should be stopped.

Thanks so much.

Start with the [u]Blink Without Delay Example[/u]. This allows you to check the time while doing other things.

Then, set up an if-statement with a [u]logical or[/u] condition, where the timer stops when the time is up or if the other event occurs.

I want the timer of the Arduino to get triggered by a certain event.

You'll need to sit in a loop waiting for that event to happen. I'd probably create a "do nothing" [u]while() loop[/u] that loops while the event is "false".

Then when you break out of the loop, you can continue and start your timer...


Take these things one step at a time and "develop" your program. For example, maybe start with Blink Without Delay, and then add your do-nothing loop that waits for something to happen before starting the blink loop. Then when that works make the blink time longer (but not 15 minutes yet) and add the or condition. When you're pretty sure everything is working, increase the time to 15 minutes. (15 minutes is 900,000 milliseconds = 60 x 1000 x 15.)

The two most important conceptsin programming are conditional execution (if-statements) and loops (doing something over-and-over, usually until some condition is met). Once you understand these two concepts you can begin to develop programs.

@DVDdoug - Thanks, I'll keep that in mind.

float lastTimerTime = millis();
int StopItHere = 0;
void setup() {

}

void loop()
{

  if (StopItHere >= 1) {
    if ((millis() - lastTimerTime) > 900000) {
      //do you stuff here 1000 is 1 second
      // 1000 x 60 is one minute
      // 60000 x 15 minutes is 900000 milliseconds
      lastTimerTime = millis();  // resets timer
    }
  }
}

You need to record millis() when the first event happens and then keep checking if the 15 minutes has elapsed when the action will end. As well as which you can check for any other event that will end the action.

Note that millis() returns an unsigned long - not a float as @Nasa has suggested.

Something like this pseudo code

void loop()
    event1val = check event1
    event2val = check event1
    if event1val == true && actionStarted == false
       startMillis = millis()
       start action
       actionStarted = true

    if millis() - startMillis > interval
      stop action
      actionStarted = false

    if event2val == true
      stop action
      actionStarted = false

...R

Thanks again @Robin2 and @Nasa - I really appreciate it.