[SOLVED] Resettable Timer by Event trigger with millis()

Hi there,

I’m trying to do some Resettable Timer by Event trigger something like this:

unsigned long currentMillis = 0;
const long SIX_SECOND= 6000;

void loop()
{

  currentMillis = millis();

if (eventTriggered && alreadyExecuting == false) {

    if ( millis() - currentMillis < SIX_SECOND)
    {
      alreadyExecuting = true;
      // Call function for 6 seconds
    } else {
      alreadyExecuting = false;
      // 6 seconds past (Stop execution)
    }

  }

}

Thank you for your help, ideas and time,

Kind regards,
Viktor

With your number of posts you should know better than to post a snippet of code
For instance

if ( millis() - currentMillis < SIX_SECOND)

millis() and currentMillis sound from the name as though they should have the same value but we cannot tell where the value of currentMillis is set and under what circumstances

Post a complete sketch and explain the problem that you are having

UKHeliBob:
With your number of posts you should know better than to post a snippet of code
Post a complete sketch and explain the problem that you are having

Dear UKHeliBob,

Yes, I know, but how I could post sketch of 7 tabs, my ears are already buzzing from it...

I could explain it in simple way, according to my posted snippet example.

I wish to make a 6 second timer, starting counting from event triggers.

After the 6 seconds pass, it would need to reset the timer for next cycle of event triggers occurrence.

Hope you can understand it better now.

Kind regards,
Viktor

Try explaining what you are up to in human. I won't chastise you for it.

-jim lee

in Loop

when an Event occurs

start a 6 second timer
 - Do something at this time
when those 6 seconds pass
 - Reset the timer for next (same) Event to occur

ok

This may not be the logic you actually want/need, but its what I got from reading your explanation. At least you can see how it works from this if its not quite right.

#include <timeObj.h>


timeObj myTimer(6000,false);   // 6 second timer. (In "off" state)


void serup(void) {  }

void loop(void) {

 if (event) {
    myTimer.start();   // Start the timer.
    doSomething();
 }
 if (myTimer.ding()) {   // Timer has expired.
    myTimer.reset();      // Reset the timer.
 }
}

LC_baseTools in library manager gives you this timerObj goodie. Along with other goodies.

-jim lee

I tried your library, but somehow it wont blink the Led, but it’s stops at 6 sec (Led goes Off).

boolean eventTriggered = false;
boolean alreadyExecuting = false;

const int ledPin =  7;
int ledState = LOW;
const long interval = 100;
unsigned long previousMillis = 0;

void loop()
{
currentMillis = millis();

  if ((eventTriggered) && (alreadyExecuting == false)) {
    myTimer.start();
    alreadyExecuting = true;
    blinkLed();
  }
  if (myTimer.ding()) {
    eventTriggered = false;
    alreadyExecuting = false;
    digitalWrite(ledPin, LOW);
    myTimer.reset();
  }
}

void blinkLed(void)
{
  if (currentMillis - previousMillis >= 100) {
    previousMillis = currentMillis;

    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
  }
}

and why is it so Sketch and Memory hungry library? (Using 10% on UNO)

beic:
Dear UKHeliBob,

Yes, I know, but how I could post sketch of 7 tabs, my ears are already buzzing from it...

This may be of interest MCVE

in Loop

when an Event occurs

start a 6 second timer
 - Do something at this time
when those 6 seconds pass
 - Reset the timer for next (same) Event to occur

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

I have used serial input for your event trigger. This code will blink the led for 6 seconds after sending '1' from the serial monitor.

const int ledPin =  13;
int ledState = LOW;
const long interval = 100;
unsigned long startTime = 0;
unsigned long currentMillis;
unsigned long previousMillis = 0;

boolean eventTriggered = false;
boolean alreadyExecuting = false;
unsigned long eventPeriod = 6000;//six second event

void setup()
{
  pinMode(13,OUTPUT);
  Serial.begin(115200);
}

void loop()
{
  if(Serial.available())
  {
    char eventTrigger = Serial.read();
    if (eventTrigger == '1')eventTriggered = true;
  }
currentMillis = millis();

  if ((eventTriggered) && (alreadyExecuting == false)) {
    startTime = millis();
    alreadyExecuting = true;
  }
  if(alreadyExecuting) {blinkLed();}
  
  if (alreadyExecuting && millis() - startTime >= eventPeriod)
  {
    eventTriggered = false;
    alreadyExecuting = false;
    digitalWrite(ledPin, LOW);
   
  }
}

void blinkLed(void)
{
  if (currentMillis - previousMillis >= 100) {
    previousMillis = currentMillis;

    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
    digitalWrite(ledPin, ledState);
  }
}
unsigned long startTime;
unsigned long currentTime;
unsigned long period = 6000;
const byte buttonPin = A3;
boolean timing = false;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop()
{
  currentTime = millis();
  if (digitalRead(buttonPin) == LOW && !timing)
  {
    startTime = currentTime;
    timing = true;
    Serial.println("start");
  }
  if (currentTime - startTime >= period && timing)
  {
    Serial.println("end");
    timing = false;
  }
}

cattledog:
This code will blink the led for 6 seconds after...

Exactly what I needed! +1 Thank you so much! :wink: (was struggling with it like maybe 10 hours or more today)

Also thank you UKHeliBob for your contribution too.

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