Reseting timer counter

Hi All,

Am trying to reset the counter to turn off the backlight of an LCD. I use this bit of code to start it in the loop

backLightStart = millis();
  if (backLightStart - previousTime >= backLightOff)
  {
    previousTime = backLightStart;
    lcd.noBacklight();
  }

it works too. How do i reset the timer here when exiting a function for example ?

it tried backLightStart = millis() and previousTime = 0 neither worked.

Not sure what you mean, but updating previousTime with backLightStart (or with millis) will keep backlight on.
Leo..

anishkgt:
Hi All,

Am trying to reset the counter to turn off the backlight of an LCD. I use this bit of code to start it in the loop

backLightStart = millis();

if (backLightStart - previousTime >= backLightOff)
  {
    previousTime = backLightStart;
    lcd.noBacklight();
  }




it works too. How do i reset the timer here when exiting a function for example ?

it tried backLightStart = millis() and previousTime = 0 neither worked.

I hope that your timers are unsigned long variables? They must be unsigned types.

Do you want something that works not over and over but only when started?

  if ( backLightOffTime > 0 )
  {
    if ( millis() - backLightOffStartTime >= backLightOffTime ) 
    {
      lcd.noBacklight();
      backLightOffTime = 0; // turns the timer off
    }
  }

This needs other code to start it every time, it is a 1-shot.

backLightOffStartTime needs to be set to millis() by the starter code
backLightOffTime needs to be set to how long before turn off by the same code

Perhaps the code that does those is triggered by a button, only happens when pressed.

I changed the name of previousTime because that is more suitable to a blinking timer.

It hasn't been very long since I figured out millis() timing, so I'm little hesitant to answer because all who have replied so far know WAY more than I. but here is how I have done it.

previousTime is what actually controls when the timer starts so I changed the names to reflect that.

To start the timer from another function do this:

void loop()
{
  currentTime = millis();
  if (currentTime - backLightStart >= backLightOff)
  {
    lcd.noBacklight();
  }
}

void anotherFunction()
{
  backLightStart = currentTime; // reset the timer
}

Just make sure it isn't called every loop or it will just keep resetting and never expire.

.

How do you get anotherFunction() to run?

void setup()
{
// put things you only want to run once here
}

void loop()
{
// put things you only want to run over and over here, when loop() finishes, loop() runs again.
}

Only functions inside of setup() and loop() (and interrupts and event code) run.

Try making a version that starts with led13 on and see how you make it turn off after 3 seconds.
By the time you have it working you will have learned a good lesson, skillz for you!
Don't use interrupt, you should not need it for something like this, poor lesson then.

He didn't specify, he just said that he wanted to reset the timer from another function that I assume he is calling in the code that he didn't show us.

That post was for you and it started with a question.
If you don't know then maybe you should find out, it would be to your benefit.

This thread has only snippets so far as I can see.

void loop()
{
  currentTime = millis();
  if (currentTime - backLightStart >= backLightOff)
  {
    lcd.noBacklight();
  }
  
  if (reason to call anotherFunction)
  {
    anotherFunction();
  }
}

void anotherFunction()
{
  backLightStart = currentTime; // reset timer
}

Or

void loop()
{
  currentTime = millis();
  if (currentTime - backLightStart >= backLightOff)
  {
    lcd.noBacklight();
  }
  
  anotherFunction();
}

void anotherFunction()
{
  if (reason to start timer)
  {
    backLightStart = currentTime; // reset timer
  }  
}

To be more clear I should add that the line that starts the timer needs to be within a conditional such that it is only called when needed.

K?

You mean because of the typo of accidentally naming the function "otherFunction" instead of "anotherFunction"? fixed

I think that you should try and make a working sketch that turns led13 ON in setup and OFF after some time without turning back ON except by user action, like send a serial char since that requires no extra hardware.

Once you have something that works you will have learned some things.

Well now you can see why I was hesitant. This crowd is as unforgiving as the code itself is :slight_smile:

So I came in and fired up the computer and did my homework. And sure enough I had forgotten to keep track of when the timer was running :roll_eyes:

@anishkgt Sorry I jacked your thread. but hopefully it helped.... eventually :o

char commandReceived;
bool timerRunning = false;  // Flag to track the timer

unsigned long currentTime;
unsigned long backLightStart;
unsigned long backLightOff = 1000;

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

void loop()
{
  currentTime = millis();
  if (timerRunning == true && currentTime - backLightStart >= backLightOff)
  {
    Serial.println ("backlight turned off");
    timerRunning = false;
  }

  if (Serial.available() > 0)
  {
    commandReceived = Serial.read();
  }

  anotherFunction();
}

void anotherFunction()
{
  if (commandReceived == 'a' && timerRunning == false)
  {
    backLightStart = currentTime; // Make sure this is only called once when you want to start timer
    timerRunning = true;

    commandReceived = 0;  // reset command
    Serial.println ("Start timer");
  }
}

Just to clarify,
I wish to have the backlight off when idle (that is not in put from button or a rotary encoder ) and switch it on when pressing a button and reset the counter. secondly when inside a function keep it on and reset counter to 0 again so that it will switch off after 1minute.

currently the backlight after first boot will switch of the backlight after 60seconds and switch on when any button is pressed. I need to do the same to the rotary encoder and reset it when exiting a function.

Hutkikz:
Well now you can see why I was hesitant. This crowd is as unforgiving as the code itself is :slight_smile:

You should not be so sensitive to opinions and very receptive to facts. It is the compiler that you must first please and then reality (it compiled but doesn't work.. means you got work to do, get up and put on you big-coder pants) and last of all is OPINION. The compiler can refuse to compile your mess, reality may show your ideas wrong but opinion stops nothing tech from working.... still if you want to get help, deal with the opinions just to get the facts and insist on facts.

So I came in and fired up the computer and did my homework. And sure enough I had forgotten to keep track of when the timer was running :roll_eyes:

I don't know anybody who gets it all right before making something that works except after they've done pretty much the same thing before. Practice might not make perfect but can be moved toward over time for same-old-same-old, and the techniques and lessons learned can usually be used on new things.

@anishkgt Sorry I jacked your thread. but hopefully it helped.... eventually :o

Just IMO you have been close to topic and redeemed yourself. You got up and did something that shows a lesson, sheds some light on fact. Compare to before?

Might I suggest a small optimization and a performance indicator?
That loop() you have will run incredibly fast, anotherFunction() only has to run the VERY rare times when a char is read.

char commandReceived;
bool timerRunning = false;  // Flag to track the timer

unsigned long currentTime;
unsigned long backLightStart;
unsigned long backLightOff = 1000;

unsigned long loopCounter;
unsigned long loopCounterStartMs;


void setup()
{
  Serial.begin (9600); // best to set this up high out of habit, 9600 is slow at emptying the serial output buffer.
}

void loop()
{

  // performance indicator code, yah it is a load on the cycles but how else to know?
  loopCounter++; 
  if ( millis() - loopCounterStartMs >= 1000UL )
  {
    loopCounterStartMs = millis();
    Serial.println(  loopCounter );
    loopCounter = 0;
  }
  // end of performance indicator code

  currentTime = millis();
  if (timerRunning == true && currentTime - backLightStart >= backLightOff) 
  {
    Serial.println ("backlight turned off");
    timerRunning = false;
  }

  if (Serial.available() > 0)
  {
    commandReceived = Serial.read();
    anotherFunction();  // only runs when a char is read, the loop() counter shows how often it runs otherwise.
  }
}

void anotherFunction()
{
  if (commandReceived == 'a' && timerRunning == false)
  {
    backLightStart = currentTime; // Make sure this is only called once when you want to start timer
    timerRunning = true;

    commandReceived = 0;  // reset command
    Serial.println ("Start timer");
  }
}

anishkgt:
Just to clarify,
I wish to have the backlight off when idle (that is not in put from button or a rotary encoder ) and switch it on when pressing a button and reset the counter. secondly when inside a function keep it on and reset counter to 0 again so that it will switch off after 1minute.

currently the backlight after first boot will switch of the backlight after 60seconds and switch on when any button is pressed. I need to do the same to the rotary encoder and reset it when exiting a function.

Ahhh, a watch dog routine could do this well.

You have your timer with startTimingMs and turnOffIntervalMs always running.

Then whenever your sketch gets input, it sets startTimingMs = millis();
Now your button is every input you do that for -- maybe even add a button just to force the backlight on.

Howzat?