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++;
}
-
how do you power it when disconnected from USB?Juraj– Juraj ♦2018年11月23日 10:14:29 +00:00Commented Nov 23, 2018 at 10:14
-
12V dc power supply plugged in to the mainsBrett– Brett2018年11月23日 20:51:25 +00:00Commented Nov 23, 2018 at 20:51
1 Answer 1
The workaround is to switch to using SoftwareSerial, see code below. Data is continuously sent via Bluetooth from Arduino Mega to Android with or without the usb cable connected.
#include <SoftwareSerial.h>
// Connections
// Arduino HM10
// MEGA
// ---- ----
// 3.3v VCC
// GND GND
// 12 RX
// 13 TX
SoftwareSerial bte(12,13); // RX, TX use (2,3) for UNO
int counter = 0;
void setup() {
// set the data rate for the SoftwareSerial port
bte.begin(9600);
bte.println(counter);
}
void loop() { // run over and over
bte.println(counter);
counter++;
delay(1000);
}
-
he has a Mega with 4 hardware Serials2018年11月25日 09:18:01 +00:00Commented Nov 25, 2018 at 9:18