I'm trying to communicate with two bluetooth module (RN-42) on the Arduino Mega.
I studied about Serial communication in Arduino, and I used Serial1, Serial2 to get information at the same time from the modules.
At the setup period, baud rate will be set. The command "$$$" makes the bluetooth module go into the configuration mode. Until this, everything works pretty well.
The problem is, it seems nothing works inside the loop(). I think Serial1.print(inByte), Serial2.print(inByte) doesn't work properly. I checked the TX1, TX2 port whether it sends any data or not by using LED, and the LED never blinked.
Here's the code that I used.
void setup() {
// initialize both serial ports:
Serial.begin(9600);
Serial1.begin(115200);
Serial1.print("$$$");
delay(100);
Serial1.println("U,9600,N");
Serial1.begin(9600);
delay(100);
Serial1.print("?");
Serial2.begin(115200);
Serial2.print("$$$");
delay(100);
Serial2.println("U,9600,N");
Serial2.begin(9600);
}
void loop() {
// read from port 1, send to port 0:
while(Serial1.available()>0) {
char inByte = Serial1.read();
Serial.print(inByte);
}
while(Serial2.available()>0) {
char inByte = Serial2.read();
Serial.print(inByte);
}
// read from port 0, send to port 1:
while (Serial.available()>0) {
char inByte = Serial.read();
Serial1.print(inByte);
delay(10);
Serial2.print(inByte);
}
}
Thank you
-
Maybe try a Serial1.end() before the second Serial1.begin? Arduino reference documentation gives no specific info here. How do you exit command mode?Wirewrap– Wirewrap2015年06月02日 16:25:56 +00:00Commented Jun 2, 2015 at 16:25
-
What i dont see is the part where you are asking module 1 to act as master, module2 to act as slave. And ask module 1 to connect to module 2.evolutionizer– evolutionizer2015年08月13日 18:59:33 +00:00Commented Aug 13, 2015 at 18:59