Skip to content

Instantly share code, notes, and snippets.

@timpulver
Created June 10, 2014 11:42
Show Gist options
  • Save timpulver/f2c1e2524dedf05380b7 to your computer and use it in GitHub Desktop.
Save timpulver/f2c1e2524dedf05380b7 to your computer and use it in GitHub Desktop.
[Arduino, Watchdog] Soft-Reset Arduino using Watchdog
/*
* Watchdog example
*
* Send 'r' through serial to reboot the Arduino, when it's back up, "Boot..." should appear.
*/
// include watchdog, part of avr-gcc compiler
#include <avr/wdt.h>
void setup(){
wdt_enable(WDTO_2S); // we need to pet the dog at least every 2 seconds
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Boot...");
}
void loop(){
wdt_reset(); // let's pet the dog
while (Serial.available()) {
Serial.println("Got char");
char inChar = (char)Serial.read();
switch(inChar){
case 'r':
reboot();
break;
}
}
}
void reboot(){
while(1){} // let's stay here and ignore the dog
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment