0

I'm trying to reset my program from software using the third method described here: Reset an Arduino Uno in code

The function is basically showing some data on an OLED display and afterwards should reset the processor

void setup() {
  MCUSR = 0;
}

void showCharacter(int char_number) {

  // [...]

  display.println(character_buffer);
  display.display();

  for (int j = 0; j < 3; j++) {
    enableLED(x, y);
    publishLEDs();
    delay(500);
    clearLEDs();
    publishLEDs();
    delay(500);
    Serial.println(j);
  }

  keyscan.sleep();

  // Reset system
  wdt_enable(WDTO_15MS); // turn on the WatchDog and don't stroke it.
  for (;;) {}
}

For whatever reason it makes a difference if the Serial.println() line is there. If the line is there, the code behaves as expected. The LED blinks three times, afterwards the Arduino is reset.

If I remove that line however, the LED continues to blink infinitely and the code does not reset. I really have no idea why that line has such a big difference, I wouldn't expect any difference to be honest. Anybody has an idea what's going on?

Edit:

I got a hint from a friend of mine that using the volatile keyword in the for loop could fix the problem. He vaguely remembered having a similar problem somehow related to compiler optimisation. And indeed, the following code works as expected:

for (volatile int j = 0; j < 3; j++) {
  enableLED(x, y);
  publishLEDs();
  delay(500);
  clearLEDs();
  publishLEDs();
  delay(500);
}

So let me rephrase my question a little bit - what is happening here, why is the volatile keyword needed?

  • Is that Serial initialized? I'm not sure we can answer this question, as there are no details about it. Why do you even need the restart? – KIIV Nov 24 '19 at 20:34
  • replace the Serial.println() with an equivalent delay – jsotola Nov 24 '19 at 20:37
  • How do you know, that the code does not reset? A continuos blink is not possible with this for loop. When we have a question like "it works with serial print, but not without", it is often just, that it could not be seen, if it works, since there is no serial print. Please show us a complete minimal example, that shows your problem. The code above is not complete. – chrisl Nov 24 '19 at 20:48
  • @KIIV, yes it is initialised and works. jsotola, there is a 500ms delay just before the Serial.println(), why should that make a difference? chrisi: after reset and init, the LED does not blink and also the OLED is reset. If the serial command is there, the devices resets, LED stops blinking and OLED shows correct content. If the command is not there, nothing of that happens and the LED keeps blinking (OLED content remains). I'll try to post more code tomorrow, but its somehow complex – waza-ari Nov 24 '19 at 21:04
  • I like the resetFunc style better. Looping and waiting for watchdog just seems clunky to me. – tavis Nov 24 '19 at 21:27

Your Answer

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

Browse other questions tagged or ask your own question.