My goal is to use Arduino to establish a connection between a PC and an Android device using the bluetooth HC-05 module.
I am using a USB connection between a PC and Arduino (Serial Monitor) and SoftwareSerial to connect to the HC-05.
My problem is that the connection works well with BT on the PC, but does not work properly in a different way. When sending from PC to BT, all sent characters are received by the BT device only when I close the serial monitor on the PC or when I reset Arduino.
I ruled out the problem with the BT module or Android application, because if in Arduino I implement ECHO code (write on Android and send to Android), everything will be fine.
With the Arduino code below, the expected behavior is: Arduino reset -> Hello word sent, Serial monitor open-> nothing happens, the character written on the serial monitor-> the character received on BT, the character written on BT-> the character received on Serial Monitor, The serial monitor is closed -> nothing happens.
Real behavior: Arduino reset -> Hello word sent, Serial monitor open-> 2 Hello, the word BT and 1 ("goodnight") on the PC, the character written on the serial monitor, β nothing, the character written on BT-> received on Serial Monitor, the serial monitor is closed -> the previous recorded character in the serial monitor received + Hello Word.
How can I fix this problem?
code:
#include <SoftwareSerial.h> SoftwareSerial mySerial(2, 3); // RX, TX int a=0; char c; char d; void setup() { Serial.begin(9600); Serial.println("Goodnight moon!"); mySerial.begin(9600); mySerial.println("Hello, world?"); } void loop() { delay(10); if (Serial.available()) { c=Serial.read(); delay(10); Serial.write(c); } delay(10); if (mySerial.available()) { d=mySerial.read(); delay(10); mySerial.write(d); } }