Initializing Arduino MEGA?

I've written a small command interpreter that accepts commands over serial for my Arduino MEGA, and it works great when I fire up the terminal console from within the IDE.

I've added a little welcome message in the setup routine and I'm greeted by that when I load up the serial monitor from the IDE.

I'm writing a program that then talks to the Arduino over the serial port, sending commands and reading the results, and that also works fine.

However I need to fire up the serial monitor from the IDE once everytime I've rebooted my machine, or the Arduino won't Initialize the program and start accepting commands.

So I guess I need to send something to the Arduino over the serial port to get it to initialize the program, but I'm not sure what.

Anyone know what I'm missing?

Put something in void setup after serial begin:

while (incomingByte =='z'){  // stay in this while loop until a 'z' is received (!= ??)
  if (Serial.available()){
  incomingByte = Serial.read();
  }
}

That's the thing though, I have a bunch of code in Setup()

A loop that sets up the pinmodes from 22-46 and ends with printing a welcome message so I know setup() has been run.

Every time I fire up the IDE terminal window after a short delay I'm greeted by the message from Setup(), so the terminal in the IDE must be sending something that resets the Arduino, which is what I'm looking to find out.

But if I shutdown my machine and restart it say the next day, if I then start sending something without first loading the Arduino IDE and loading up the terminal, the Arduino won't respond and won't run the program.

If I pull the USB cable and put it back in, then the program starts working without me having to do anything, and I can send commands from my app without problems.

But I can't have users needing to pull the USB cable or cut power every time they have had their PC shut down, so I need to figure out what the IDE terminal is doing to send a reset to the Arduino.

Why not cut the Reset Enable jumper so the Arduino only resets when the user presses the Reset button? Then once it starts, it just runs, and you don't have to worry about the serial monitor reseting the program.

That wouldn't really help, as the problem happens when power it cut and then turned on again later, as it is when you turn of your PC to which the Arduino is connected.

My program relies on a connection to a pc as the program processing the data to and from the Arduino is running on the PC, it will potentially run hundreds of timers and other date/time functions via a user accessible scripting language that's just to cumbersome (And large) to implement on the Arduino, and it needs to come back up after a power failure when the pc starts up again unattended.

What I'm specifically looking for is what exactly the IDE is sending to the Arduino in order to reset it.

If you add a message to Setup() that just Serial.println's in the setup() routine, you will see that message each time you close and reopen the IDE serial window, so the serial window in the IDE is resetting the Arduino. That's the functionality I'm looking for to implement in my C# app as it seems to be able to reinitialize the Arduino even when it becomes unresponsive after a reboot of the computer or power loss.

Just a follow up on this one in case anyone else needs this..

You simply need to set DTR (DataTerminalReady) low for about 500ms and the Arduino resets. (MEGA anyway, haven't tried others yet)

Works even when it has otherwise stopped responding to serial input for whatever reason.

Better than asm jmp 0 which leaves everything in a mess, and doesn't reset peripherals and registers.