Hi everyone, I am trying to use Bluetooth to control the Arduino DUE via my PC, but the test results are so weird.
The bluetooth I used is JY-MCU (HC-06) slaver, and connections are all correct, TXD - RX, RXD- TX, VCC- 5V OR 3.5V, GND-GND. The type of data I sent from PC is ASCII. I bought three Bluetooth(number:1,2,3).
Test result:
Only when Bluetooth 2 is connected to TX2 and RX2, it can receive most data. I send 1 with a 1000ms loop for 100 times, there are about 14 wrong results shown in the monitor. If I try to used other Bluetooth and connections, they just cannot work! For example, when I connect Bluetooth 1 to TX2 and RX2, it receives the wrong data mostly.
I cannot figure out why this problem happened. I also tried some solutions, like pull up the resister of TX pin, but the it did not work. Can someone help me with this problem? Thank you in advance.
My BT code:
String comdata = "";
double flag1=0;
int temp;
void setup() {
Serial.begin(9600);
Serial2.begin(9600);
pinMode(16,INPUT_PULLUP);
I am not familiar with your Bluetooth parts, but in the past, if I've had partially incorrect data on a serial link, it usually was due to a timing problem. I.e., the clocks of the sending and receiving devices were a little off from each other.
use software serial library , you can use two serial as you mentioned.Thats reason you might getting data from one module not from other
#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(10,11);
// software serial #2: TX = digital pin 8, RX = digital pin 9
// on the Mega, use other pins instead, since 8 and 9 don't work on the Mega
SoftwareSerial portTwo(8,9);
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// Start each software serial port
portOne.begin(9600);
portTwo.begin(9600);
}
void loop()
{
// By default, the last intialized port is listening.
// when you want to listen on a port, explicitly select it:
portOne.listen();
Serial.println("Data from port one:");
// while there is data coming in, read it
// and send to the hardware serial port:
while (portOne.available() > 0) {
char inByte = portOne.read();
Serial.write(inByte);
}
// blank line to separate data from the two ports:
Serial.println();
// Now listen on the second port
portTwo.listen();
// while there is data coming in, read it
// and send to the hardware serial port:
Serial.println("Data from port two:");
while (portTwo.available() > 0) {
char inByte = portTwo.read();
Serial.write(inByte);
}
// blank line to separate data from the two ports:
Serial.println();
}