SoftwareSerial example

Support forum for MegunoLink
Post Reply
User avatar
RoboBill
Posts: 31
Joined: Mon May 23, 2016 4:13 pm

Sat Apr 22, 2017 4:06 pm

Does anyone have any working examples of using SoftwareSerial?

Thanks

RoboBill
philr
Posts: 463
Joined: Mon May 26, 2014 10:58 am

Sun Apr 23, 2017 4:11 am

Hi Bill, what part of the library do you want an example of?
User avatar
RoboBill
Posts: 31
Joined: Mon May 23, 2016 4:13 pm

Sun Apr 23, 2017 6:02 am

Hi Phil,

To begin, I'm simply trying to plot several encoders data and to have a simple switch to stop the robot if it looks like its going to crash :oops:

So now I've changed back over to XBee series 1 since I'm just talking between one computer and one arduino. Also because the Arduino code for XBee series 1 appears to be so much easier... I just treat it like any serial connection.

The problem is I'm definitely not a programmer, so I tweak existing programs to fit my needs. And as I look through your examples, all the communication seems to go through the Arduino's Serial. But because I have many encoders on my robot, I need all of my Mega's interrupts. Which leaves me to using SoftwareSerial for my XBee.

And thank you, you did point me to the command handler function, and with any normal programmer NBD... but for me... its way over my head....

So I was hoping someone had an example of using i.e SoftwareSerial XBee(11,8);
... and then I could try it and tweak it

Thanks

RoboBill
philr
Posts: 463
Joined: Mon May 26, 2014 10:58 am

Wed Apr 26, 2017 3:41 am

Hi Bill, here is a modified version of our example showing how to point the command handler at the software serial source. It should work (although I don't have the hardware to test with).

#include <SoftwareSerial.h>
#include <CommandHandler.h>

SoftwareSerial Xbee(3, 4);

int NumberOfTurtles = 0;

CommandHandler<> SerialCommandHandler(Xbee);

void Cmd_GetTurtleCount(CommandParameter &Parameters)
{
Parameters.GetSource().print(F("Number of turtles = "));
Parameters.GetSource().println(NumberOfTurtles);
}

void Cmd_SetTurtleCount(CommandParameter &Parameters)
{
NumberOfTurtles = Parameters.NextParameterAsInteger();
}

void setup()
{
Serial.begin(9600);
Serial.println("MegunoLink Pro Turtle Monitor");
Serial.println("-----------------------------");

SerialCommandHandler.AddCommand(F("SetTurtleCount"), Cmd_SetTurtleCount);
SerialCommandHandler.AddCommand(F("GetTurtleCount"), Cmd_GetTurtleCount);

}

void loop()
{
SerialCommandHandler.Process();
}
User avatar
RoboBill
Posts: 31
Joined: Mon May 23, 2016 4:13 pm

Tue May 02, 2017 11:17 pm

Hi Phil,

Thanks for that example and the links to the CommandHandler you emailed to me. Very informative. Unfortunately I still am unable to change the interval and duration.

I am using XBee Series 1 as a simple serial connection

Here is the code I am using:

#include "CommandHandler.h" // The serial command handler is defined in here.
#include "SoftwareSerial.h"
const int XBeeRxPin = 11;
const int XBeeTxPin = 9;
SoftwareSerial XBeeSerial(XBeeRxPin, XBeeTxPin);

CommandHandler<> SerialCommandHandler(XBeeSerial);

long LastBlink = 0; // Time we last blinked the LED
int OnTime = 10; // Amount of time the LED remains on [milliseconds]
int OffTime = 100; // Amount of time the LED remains off [milliseconds]

const int LEDPin = 13; // Pin the LED is attached to

void setup()
{
XBeeSerial.begin(19200);
XBeeSerial.println(F("Blink 2.0"));
XBeeSerial.println(F("=========="));

SerialCommandHandler.AddVariable(F("OnTime"), OnTime);
SerialCommandHandler.AddVariable(F("OffTime"), OffTime);

pinMode(LEDPin, OUTPUT);
}

void loop()
{
SerialCommandHandler.Process();
uint32_t uNow = millis();
if (uNow - LastBlink < OnTime)
digitalWrite(LEDPin, HIGH);
else
digitalWrite(LEDPin, LOW);

if (uNow - LastBlink > OnTime + OffTime)
LastBlink = uNow;
}
philr
Posts: 463
Joined: Mon May 26, 2014 10:58 am

Tue May 09, 2017 2:58 am

Hi Bill, are you sending the correct message? I think it should be like this example"!OnTime 50\r\n". The command handler uses the ! to identify the start of a message and the \r\n for the end.

Cheers
Phil
User avatar
RoboBill
Posts: 31
Joined: Mon May 23, 2016 4:13 pm

Wed May 10, 2017 8:05 pm

Finally!!!

The problem (yep all me) was I was using the Blink interface as it was downloaded from your site. Thanks to your last message, I first manually sent the !OnTime 50\r\n command via the XBee connection to confirm the SoftwareSerial connection and, sure enough the blink changed. So then I modified the Blink Interface and Presto!

Thanks

RoboBill
Post Reply