3

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

VE7JRO
2,51519 gold badges27 silver badges29 bronze badges
asked Jun 2, 2015 at 11:44
2
  • Maybe try a Serial1.end() before the second Serial1.begin? Arduino reference documentation gives no specific info here. How do you exit command mode? Commented 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. Commented Aug 13, 2015 at 18:59

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

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.