Go Down

Topic: Mini loop for a short period of time. (Read 372 times) previous topic - next topic

mrjonny2

Hi,
What I am trying to achieve is that I want my device to wait for 10 minutes at the end of my loop, however, here is the catch, I need to reset the watchdog timer every 8 seconds,  How would I go about doing this?
I have been trying to use the BlinkWithoutDelay Example with no success, please help.
Thanks

MarkT

Show us the code you have so far - easier to see where you are going wrong then...
[ I won't respond to messages, use the forum please ]

mrjonny2

Code: [Select]
void loop()
{
  Serial.println("hello");
    while( intervaltimer() != 1)
    {
    continue;
    }
    Serial.println("finished loop");
}

  unsigned long currentMillis = millis();
 
unsigned long intervaltimer(){
  if(currentMillis - previousMillis  interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   
    wdt_reset();

    return 1;

  }
  else
  {
    return 0;
  }
}

PeterH

The blink-without-delay approach is what you need, but you're trying to time two things at once so you need to have two timers running together. Something like this should do the trick (untested):
Code: [Select]

const unsigned long TotalDelay = 600000;
const unsigned long ResetInterval = 8000;

unsigned long delayStartTime = millis();
unsigned long lastResetTime = 0;

while(millis() - delayStartTime < TotalDelay) // loop until TotalDelay has elapsed
{
   if(millis() - lastResetTime >= ResetInterval) // every ResetInterval reset the watchdog
   {
       wdt_reset();
       lastResetTime = millis();
   }
}
I only provide help via the forum - please do not contact me for private consultancy.

mrjonny2

Thanks man that works like a charm.
I am very jealous of your ability to just write code like that without testing

Go Up