Pages: [1]   Go Down
Author Topic: Mini loop for a short period of time.  (Read 328 times)
0 Members and 1 Guest are viewing this topic.
London, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 190
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Shannon Member
****
Karma: 214
Posts: 12392
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Show us the code you have so far - easier to see where you are going wrong then...
Logged

[ I won't respond to messages, use the forum please ]

London, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 190
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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;
  }
 }
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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();
    }
}
Logged

I only provide help via the forum - please do not contact me for private consultancy.

London, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 190
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: