1

Dear Stack Exchanger's,

I want to reset my Arduino and system in every 24h for preventing frozen software and also other connectivity stuffs.

I wrote a program which connects a digital pin to reset pin of Arduino and I want to reset with that way.

For example my code :

unsigned long myTime; // Millis() function time value 
int timeExpected = 5000; // I want to reset it in every 5 secs for testing.
const int RESET_PIN = 2; // Reset pin's connection

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN,OUTPUT);
}
void loop() {
  digitalWrite(LED_BUILTIN,HIGH);
    myTime = millis();
    Serial.println(myTime); // prints time since program started
     delay(1000);

   if(myTime % timeExpected == 0) {
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
     Serial.println("RESET");
    //digitalWrite(RESET_PIN, LOW);(I just test with LED_BUILTIN Low function)
    }

  }

I have tried the watchdog timer but I cannot do it for 24 hours. If you have another solution for reset in 24h please tell me and send me the related article/book/link etc. I will check and implement into my code.

UPDATE: I have done this but it does not reset at very 5s . Code :

#include <avr/wdt.h>
unsigned long myTime;
int timeExpected = 5000; 


void setup() {
  Serial.begin(9600);
Serial.println("Setup started :");
// make a delay before enable WDT
// this delay help to complete all initial tasks
delay(2000);
wdt_enable(WDTO_2S);
}


void loop() {
    myTime = millis();
    delay(1000);
    Serial.println(myTime); // prints time since program started
   if(myTime == timeExpected) {
     Serial.println("RESET");
    wdt_reset();
    }
2
  • 1
    You can't really use unstable software to reset itself - if it freezes, how can it execute the reset code? You'll need to either: 1) debug the software (you should do this anyway); 2) use the built-in watchdog timer to reset the arduino if your software doesn't reset the watchdog; or 3) use an external hardware timer to reset the Arduino at intervals. Solution 3) should be unnecessary if you do 1) and 2).
    – JRobert
    Jul 26 at 11:57
  • 2
    wdt_reset() resets the watchdog's timer to avoid reset of the MCU. read my answer again
    – Juraj
    Jul 26 at 12:18
2

You can use the watchdog timer, but you have to change the way you measure the elapsed time. Try this code:

#include <avr/wdt.h>

unsigned long myTime; // Millis() function time value
unsigned long elapsedTime;
unsigned long timeExpected = 5000; // I want to reset it in every 5 secs for testing.
const int RESET_PIN = 2; // Reset pin's connection

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  myTime = millis();
  Serial.println("INIT");
  wdt_enable(WDTO_2S); //Enable wdt every two seconds

}
void loop() {

  //************RESET HANDLER********************//
  elapsedTime = millis() - myTime;
  if (elapsedTime > timeExpected) {
    Serial.print("RESET");
    while (1); //Intentionally halt the program, so the watchdog performs the reset    
  }
  //**********************************************
  //****************REST OF THE PROGRAM***********
  wdt_reset(); //Ensure one call to this function at least one time in two seconds or less
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.println(elapsedTime); // prints time since program started
  delay(1000);           
}

Please note that if you have multiple delays in your program, you have to ensure the call to wdt_reset() in order to prevent unwanted resets. Personally I try not to use delays and would recommend avoid delays as much as possible. You may check out this tutorial if you want to see how to avoid delays. If you achieve avoiding delays you can call wdt_reset() just one time at the begining of the loop.

1

You can do an almost immediate full reset with watchdog by activating it 'on place'.

wdt_enable(WDTO_15MS); // resets the MCU after 15 milliseconds
while (true);

You can use millis() for timing the reset as any other timed function.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.