0

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++;
}
asked Nov 23, 2018 at 2:42
2
  • how do you power it when disconnected from USB? Commented Nov 23, 2018 at 10:14
  • 12V dc power supply plugged in to the mains Commented Nov 23, 2018 at 20:51

1 Answer 1

-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);
}
answered Nov 25, 2018 at 1:11
1
  • he has a Mega with 4 hardware Serials Commented Nov 25, 2018 at 9:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.