setting DTR

Hi,
I am using 2 micro arduino’s connected to 2 hc-05 communicating to each other.
When using them with Serial monitor open the code works fine. When plugging them and not opening serial monitor the communication doesn’t happen. I read about this issue and saw that it is related to that once the Serial monitor is turned on DTR goes HIGH and code will work properly.
Any recommended simple way to bypass this issue? please correct me if I made some mistakes.
Thanks,
Omri.

I Attached my code if necessary.

band_send.ino (1.09 KB)

band_receive.ino (1.9 KB)

This:

while (!Serial) ;

should be after the corresponding Serial.begin() ;

6v6gt:
This:

while (!Serial) ;

should be after the corresponding Serial.begin() ;

Thanks! done, but still doesn't solve the issue. any other thoughts?

Omri_Saporta:
Thanks! done, but still doesn't solve the issue. any other thoughts?

Did you fix both sides?

Montmorency:
Did you fix both sides?

Well, it seems that the problem was with waiting for Serial port to begin, even when putting the while condition after Serial.begin it waits for it.

Now works perfect thanks a lot!

6v6gt:
This:

while (!Serial) ;

should be after the corresponding Serial.begin() ;

Doesn't really matter:

void Serial_::begin(unsigned long /* baud_count */)
{
        peek_buffer = -1;
}

The constructor already does that.

Just an FYI, DTR is not used on the Micro. Unlike other boards like the Uno, Mega, and Nano, where DTR assert is used to pulse the reset pin to activate the bootloader for an upload (which also means the board resets whenever you open Serial Monitor), the boards with native USB functionality in their primary microcontroller like the Micro, Leonardo, MKR use a different system to trigger the reset before upload. A serial connection at 1200 baud is opened as a signal that an upload is starting.

For this reason, the microcontroller on the native USB boards does not reset when you open Serial Monitor. That can be problematic if there is serial output between the time the program starts running and you get Serial Monitor open. The solution is to add that while (!Serial) to setup(), so that the program will wait in an infinite loop until you open Serial Monitor.