From an Android phone I am using the "Serial Bluetooth Terminal" app to communicate to an Arduino Mega 2560 with a Bluetooth Low Energy HM10 (Jaycar XC-4382) module connected via Serial2.
If the physical USB serial cable (Serial) is connected to Arduino the very simple sketch below works fine and runs continuously. When the cable is disconnected mid sketch or the power is cycle without the cable connected the sketch will run for about 5 seconds and then stop.
How can I prevent the sketch from timing out when the serial cable is disconnected?
int counter = 0;
void setup()
{
Serial2.begin(9600);
}
void loop()
{
Serial2.print(counter); Serial2.println();
delay(1000);
counter++;
}