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());***
}
-
it is just a wrong placed comment :-)Juraj– Juraj ♦2017年10月09日 11:27:10 +00:00Commented Oct 9, 2017 at 11:27
-
are you asking about the code highlighted with ***?Juraj– Juraj ♦2017年10月09日 11:28:00 +00:00Commented Oct 9, 2017 at 11:28
2 Answers 2
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.
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.