Arduino MEGA2560 code stops after power down and up

Hi,

I am running a typical serial print code, and if I pull out the USB connection and then plug it back in (power up and down the board), the it won't start printing again. I have to reload the code every time I power down. How can I get around this? Is this normal?

I reloaded the bootloader on again, and it didn't fix it.

Never mind, I figured out that I had to close and reopen the serial monitor. But another question, closing and opening the serial monitor seems to make my sketch reset. Is this normal? Is there a way to avoid that and just keep the sketch running all the time?

Are you leaving the serial monitor open when you unplug and re-connect the USB cable?
If so, this behaviour is normal and you don't need to upload the code again. You only need to close and re-open the serial monitor.
You figured this bit out.

Yes, opening the serial monitor resets the Arduino. There's a "RST EN" link on the board that you can cut to stop this behaviour, but then you need to manually press RST during uploading, or fit a jumper where you cut the link.

Thanks! So I am trying to add the Arduino mega 2560 to a system that has an on/off button, and I want the Arduino to keep running from its previous point after the power is turned back on. If I hold down the reset button during upload, with this allow me to cut the power and turn it back on and the sketch to just continue where it left off?

Meg93931:
Thanks! So I am trying to add the Arduino mega 2560 to a system that has an on/off button, and I want the Arduino to keep running from its previous point after the power is turned back on.

You're not being very clear. Do you mean you want to cut the power to the Arduino itself, then have it run from where it left off when it's power is reconnected? Or, while the Arduino is running, you just want to cut the power to the external device, then re-power it and have the Arduino sketch continue. And if the latter, do you want the Arduino to pause while the power to the external device is off?
What is this system that you mention?

If I hold down the reset button during upload, with this allow me to cut the power and turn it back on and the sketch to just continue where it left off?

Again, you're not explaining the situation very well.
Cut what power? Do you mean initiate an upload, press the RST button as it's happening and at the exact same time cut the power to the external device, or the Arduino, or both?

Words are free. Please describe the exact situation accurately, using as many as you need.

Sorry. I will try to explain better.

Say the Arduino was running a mini LED screen and then the Arduino was unplugged from the computer and so the Arduino had no power and the LED screen had no power. And then you plug in the Arduino again, so they both get power again. The Arduino stops printing to the screen. It only starts again if I upload the code to the Arduino again, or if I open the serial monitor on the computer. How do I get the Arduino to keep printing to the screen on power up?

Meg93931:
Sorry. I will try to explain better.

Say the Arduino was running a mini LED screen and then the Arduino was unplugged from the computer and so the Arduino had no power and the LED screen had no power. And then you plug in the Arduino again, so they both get power again. The Arduino stops printing to the screen. It only starts again if I upload the code to the Arduino again, or if I open the serial monitor on the computer. How do I get the Arduino to keep printing to the screen on power up?

After disconnecting the power, the Arduino will never continue from where it left off when the power was cut. It will always start from the beginning, the 'setup()' function.

But it sounds like this isn't really what you want to know.

So on power-up, nothing is printed to the LED display, despite the fact that everything was fine immediately after uploading the code?

Effectively, powering up both should do the same thing that happens at the end of an upload.
When you power up again, instead of re-uploading the code or opening the serial monitor, try pressing the Arduino's RST button then releasing it and see if the Arduino prints to the display as it's supposed to.
Let me know what happens.

Oh, thanks for explaining. I think that I wanted the Arduino to do something that is not possible. I will just press the reset button from now on. Thanks :slight_smile:

Meg93931:
Oh, thanks for explaining. I think that I wanted the Arduino to do something that is not possible.

Oh, right. If you saved a variable or variables in EEPROM, with status info, you could then read that info in 'setup()' and act on it, possibly. EEPROM storage is 'non-volatile', so does not disappear when power is cut.

I will just press the reset button from now on. Thanks :slight_smile:

Regarding the fact that there's no printing to the display on power-up until you press RST, upload or open the serial monitor, that indicates that you might need a short delay in 'setup()', to give the display time to initialise before you start sending print commands. Edit: You could try about 500mS, then if that works, keep reducing the delay and testing until you find the minimum delay that's required, then just increase it by a comfortable margin to ensure reliable operation. It definitely should be possible to get the Arduino to print to the display on power-up without having to press RST.

Is this an OLED display, or another type of LED display with an on-board controller?