Skip to content

Commit

Permalink
add dynamic baud change to softserial example
Browse files Browse the repository at this point in the history
  • Loading branch information
shadow578 committed Feb 8, 2025
1 parent ab19527 commit 71b04cb
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* Software serial basic example.
*
* echos any character received on the software serial port back to the sender.
* also allows to change the baud rate by sending a number from 1 to 8.
*
* without changing the system clock, the maximum baud rate is around 9600 baud.
*/
#include <Arduino.h>
#include <SoftwareSerial.h>
Expand All @@ -20,7 +23,32 @@ void setup()
void loop()
{
while (mySerial.available())
mySerial.write(mySerial.read());
{
const char c = mySerial.read();
mySerial.write(c);

uint32_t new_baud = 0;
switch(c)
{
case '1': new_baud = 1200; break;
case '2': new_baud = 2400; break;
case '3': new_baud = 4800; break;
case '4': new_baud = 9600; break;
case '5': new_baud = 19200; break;
case '6': new_baud = 38400; break;
case '7': new_baud = 57600; break;
case '8': new_baud = 115200; break;
default: break;
}
if (new_baud != 0)
{
mySerial.print("Set baud to:");
mySerial.print(new_baud);
delay(100);
mySerial.begin(new_baud);
mySerial.println(" - Done");
}
}

delay(10);
}

0 comments on commit 71b04cb

Please sign in to comment.