0

I understand most of it. The part I don't understand is the is the code in for void loop. How does that set the data rate for the software serial port? I thought mySerial.begin(9600) did that? Please help.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
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
 }
 Serial.println("Goodnight moon!");
 // set the data rate for the SoftwareSerial port
 mySerial.begin(9600);
 // set master
 mySerial.print("AT+ROLE1");
 delay(10000);
}
void loop() // run over and over
{
 // set the data rate for the SoftwareSerial port
 mySerial.print("test I am master ");
 delay(10000);
 ***if (mySerial.available())
 Serial.write(mySerial.read());
 if (Serial.available())
 mySerial.write(Serial.read());***
}
asked Oct 8, 2017 at 21:09
2
  • it is just a wrong placed comment :-) Commented Oct 9, 2017 at 11:27
  • are you asking about the code highlighted with ***? Commented Oct 9, 2017 at 11:28

2 Answers 2

1

You're confusing "data rate" with "baud rate".

The baud rate is the speed at which bytes are transmitted over the wires, and is set with mySerial.begin(9600);.

The data rate is the speed at which data is sent over the serial port, and that is set by the delay(10000); which dictates how often "test I am master " is sent and any incoming data is checked for and echoed.

answered Oct 8, 2017 at 21:11
0

The void loop

while(!Serial) {
;
}

simply wait for the serial port to be ready to transmite/receive data. Otherwise, your first Serial.print will be lost.

You can omit it if you don't plan to transmit anything during the first second or two in your sketch.

answered Oct 8, 2017 at 21:53

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.