Code only works when Serial Monitor opens/reset button is pressed

@Paul_B
@Idahowalker

Any thoughts on the dependency of the serial monitor? This is what took my suspicious away from it being a power issue.

Anyway, another observation I noticed is that the onboard LED for pin 13 blinks twice when when I open up the serial monitor. Does this signify anything?

*As an update to this, if I open and then close the serial monitor, the program still works as expected. But if I unplug the usb or power down the supplies and then try to power up and use the program I need to reopen the serial monitor before the program will work again. I'm expecting that the board needs to be reset in some fashion or have some sort of connection triggered by the opening of the serial monitor.

Another observation when testing the blink program is that the program takes ~10 seconds to function properly. Either the light will stay on for longer than the delay time or it will flutter before falling into the prescribed flashing pattern.

The Arduino MEGA will automatically reset when you open the serial monitor.
This is because it has a built-in automatic reset circuit that uses the DTR signal to start the boot loader for sketch transfer.

In other words, I think your problem can be unified into the fact that "work fine after an external reset but do not execute correctly at the power-on reset".

Precisely the issue, the board is on a shield which doesn't give access to the reset button, but I grounded a wire that I connected to the reset pin and it worked after that.

Yeah, it's the same as pressing the reset button.
Also, the act of "opening the serial monitor" does that with Mega's internal circuitry.

Probably not a problem, but I would like to check the boot loader and fuse settings.
Do you know about ICSP?
Also, is the board temporarily removable and do you have another Arduino board?

I'm not familiar with ICSP. And yeah I have another clone Mega and another regular mega as well as other uno/nanos.

Use spare Mega and Uno.
Connect the two as follows.

Once properly connected, connect only Uno to your PC's USB.
Then open the IDE and set the board to Arduino Uno.
[Tools -> Board -> Arduino AVR boards -> Arduino Uno]

From the sketch sample, open ArduinoISP.
[File -> Examples -> 11. ArduinoISP -> ArduinoISP]
Click the Upload button to program this sample sketch into Uno as is.
Note: Select the correct COM port to which Uno is connected.

Switch the IDE board to Mega.
[Tools -> Board -> Arduino AVR boards -> Arduino Mega or Mega 2560]
Note: Uno is still connected to the USB! Does not change the connection! IDE menu only!

Change the programmer to Arduino as ISP.
[Tools -> Programmer -> Arduino as ISP]
Note: It's not "Arduino ISP", please note that the names are similar!

Let's burn the bootloader.
[Tools -> Burn Bootloader]

Remove Uno from your PC's USB and unplug all connecting wires from Uno and Mega.
Connect Mega to your PC via USB and write a sketch of your project.
Install it in the project and check the operation.

If you encounter any errors or other problems during the process, please post an error message.

EDIT:
There was a good instruction video.
https://www.youtube.com/watch?v=X5achE10rCI

@chrisknightley

I'm getting this error message:

Arduino: 1.8.13 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

Error while burning bootloader.

avrdude: Yikes!  Invalid device signature.

         Double check connections and try again, or use -F to override

         this check.





This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Full Error Message Below:

Arduino: 1.8.13 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude -CC:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf -v -patmega2560 -cstk500v1 -PCOM6 -b19200 -e -Ulock:w:0x3F:m -Uefuse:w:0xFD:m -Uhfuse:w:0xD8:m -Ulfuse:w:0xFF:m 



avrdude: Version 6.3-20190619

         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/

         Copyright (c) 2007-2014 Joerg Wunsch



         System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"



         Using Port                    : COM6

         Using Programmer              : stk500v1

         Overriding Baud Rate          : 19200

         AVR Part                      : ATmega2560

         Chip Erase delay              : 9000 us

         PAGEL                         : PD7

         BS2                           : PA0

         RESET disposition             : dedicated

         RETRY pulse                   : SCK

         serial program mode           : yes

         parallel program mode         : yes

         Timeout                       : 200

         StabDelay                     : 100

         CmdexeDelay                   : 25

         SyncLoops                     : 32

         ByteDelay                     : 0

         PollIndex                     : 3

         PollValue                     : 0x53

         Memory Detail                 :



                                  Block Poll               Page                       Polled

           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack

           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------

           eeprom        65    10     8    0 no       4096    8      0  9000  9000 0x00 0x00

           flash         65    10   256    0 yes    262144  256   1024  4500  4500 0x00 0x00

           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00

           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00

           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00

           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00

           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00

           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00



         Programmer Type : STK500

         Description     : Atmel STK500 Version 1.x firmware

         Hardware Version: 2

         Firmware Version: 1.18

         Topcard         : Unknown

         Vtarget         : 0.0 V

         Varef           : 0.0 V

         Oscillator      : Off

         SCK period      : 0.1 us



avrdude: AVR device initialized and ready to accept instructions



Reading | ################################################## | 100% 0.02s



avrdude: Device signature = 0x000000 (retrying)



Reading | ################################################## | 100% 0.02s



avrdude: Device signature = 0x000000 (retrying)



Reading | ################################################## | 100% 0.02s



avrdude: Device signature = 0x000000

avrdude: Yikes!  Invalid device signature.

         Double check connections and try again, or use -F to override

         this check.





avrdude done.  Thank you.



Error while burning bootloader.


The this error message has been omitted by the IDE.
Can you post the full message of the error by doing the following IDE settings?

[File -> Preferences]

image

Just got it to work by using a different Uno.

But the same issue still arises. Does not work upon powering up but will work after a reset.

Looks like programmer (in this case your Uno) is can't communicate with Mega.
Please re-check the wiring again.

And I'm so sorry, my bed is approaching...

Good test!

Use other Arduinos have similar problems?
So it may not be a problem, with that Mega bootloader.
May need to investigate again.

I've changed out the board to a clone mega that I had and I'm still having the same issue.

Okay, It is possible to isolate the problem more.

Sorry, I'm going to sleep today. :zzz:

Other posters may also give you more hints on how to solve the problem.
I has basically sloppy head... :joy:

@UKHeliBob Any input here?