Hey guys I have a question. I have a device that forces me to change the baud rate on the go from 115200 to 9600, send enter and then change back to 115200 again to continue communication. I tried but it seems not to change accordingly. Can anyone spot an obvious mistake in my coding?
#include <SoftwareSerial.h>
//Software-UART Pins for XBee
SoftwareSerial xbee_serial(2, 3); // RX, TX
unsigned long tmp;
//Message to announce completed initialization
char msg[] = {'\n', 'c', 'o', 'm', 'p', 'l', 'e', 't', 'e', 'd', '\n'};
void setup() {
Serial.begin(115200);
xbee_serial.begin(115200);
}
void loop() {
do{
//Transfer bootmessage from UniFi to XBee
if (Serial.available()) {
xbee_serial.write(Serial.read());
}
tmp = millis();
}while (tmp < 50000); //Wait 50 sec. for the UniFi to boot
//Change to 9600
Serial.flush();
Serial.begin(9600);
delay(3000);
//Send "enter"
Serial.write('\n');
delay(3000);
//Change to 115200
Serial.flush();
Serial.begin(115200);
//Message that initialization is completed
for (int x = 0; x < 11; x++) {
xbee_serial.write(msg[x]);
}
while (1) {
//Transfer input from UniFi to XBee
if (Serial.available()) {
xbee_serial.write(Serial.read());
}
//Transfer input from XBee to UniFi
if (xbee_serial.available()) {
Serial.write(xbee_serial.read());
}
}
}