Program Resource

Resource libraries for programmers and developers

There are some case that arduino and/or connected devices doesn’t work right after powering up. Putting some delay does not work, and everything starts working after pressing reset button on Arduino.

So, below is simple trick to restart Arduino once after powering up. Simply write 1 or 0 to EEPROM after each boot.

#include <avr/wdt.h>
#include <EEPROM.h>

//Reset Arduino
void software_reset() {
	wdt_disable();
	wdt_enable(WDTO_15MS);
	while (1) {}
}

byte Load_Bootstate() {
	int state;
	state = EEPROM.read(0);
	return state;
}

void Set_Bootstate(int state) {
	EEPROM.write(0, state);
}

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

	//reboot after 500ms
	if (Load_Bootstate() == 0) { //If data is 0, reboot
		Set_Bootstate(1); //Write 1 to EEPROM. Boot normally next time.
		delay(500);
		Serial.println("Rebooting");
		software_reset();
	}
	Set_Bootstate(0); //Write 0 to memory.

	Serial.println("Boot!");
}

void loop() {

}
Print Friendly, PDF & Email

This post is also available in: Japanese

Leave a Reply

Your email address will not be published. Required fields are marked *


*

CAPTCHA