Page 1 of 1

SoftwareSerial example

Posted: Sat Apr 22, 2017 4:06 pm
by RoboBill
Does anyone have any working examples of using SoftwareSerial?

Thanks

RoboBill

Re: SoftwareSerial example

Posted: Sun Apr 23, 2017 4:11 am
by philr
Hi Bill, what part of the library do you want an example of?

Re: SoftwareSerial example

Posted: Sun Apr 23, 2017 6:02 am
by RoboBill
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

Re: SoftwareSerial example

Posted: Wed Apr 26, 2017 3:41 am
by philr
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();
}

Re: SoftwareSerial example

Posted: Tue May 02, 2017 11:17 pm
by RoboBill
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;
}

Re: SoftwareSerial example

Posted: Tue May 09, 2017 2:58 am
by philr
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

Re: SoftwareSerial example

Posted: Wed May 10, 2017 8:05 pm
by RoboBill
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