Hi,
I have got it working with some help of examples.
I have the trasmitter side of the software and then the receiver side.
This code just sends the >A001< over and over as fast as it can, I did this to test data loss.
The A001 I will interpret in the receiver to do something for example Motor A speed =1, or servo A position 1.
in this format I can control servos, motors etc A,B,C,D etc etc and send a value up to 999, I can always increase it to 4 or more characters.
Although by using less digits I can send more info in less time.
Transmiter code
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX <<<<< if using MEGA for transmitter use pins 10,11
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(19200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// set the data rate for the SoftwareSerial port
mySerial.begin(4800);
}
void loop() // run over and over
{
mySerial.write(">A001<");
if (mySerial.available()){ //Something has come into the transmitter serial port
if(mySerial.read()=='-')// if the remote serial full, send - to slow down
{
delay(15);// short pause to let remote receiver time t empty some of the serial buffer
}
}
}
receiver code
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX <<<<<< receiver on MEGA. If using uno you can use pins 2,3.
char ByteIn;
String StringIn;
int led = 13;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(19200);
pinMode(led, OUTPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// set the data rate for the SoftwareSerial port
mySerial.begin(4800);
//mySerial.println("Hello, world local?");
}
void loop() // run over and over
{
if (mySerial.available()) // something waiting to be read on software serial port
{
//Serial.println(StringIn);
Serial.println(mySerial.available()); //show buffer size, this is for info only
if (mySerial.available() > 50) // serial port has 64 byte buffer so to stop it getting full and dropping bytes slow incoming transmission for a bit
{
mySerial.write("-"); // send a - to transmitter, could be any character as long as it matches on both sides
}
ByteIn = mySerial.read(); // get a byte from the serial port
//Serial.write(ByteIn);
StringIn = StringIn + ByteIn; // build up the incoming bytes into the string
if (ByteIn == '<') // we have the character that tells me that is the end of that command/data.
{
// now do something with the string
if (StringIn != ">A001<") // here you can now do something with the commands that have arrived the != is for not equal to, I used this for testing to see if I was getting any garbage. you should use something like if(StringIn == ">A001<") or break down the comand. get first Character(removing the >), get last 3 characters.
{
digitalWrite(led, LOW);
}
else
{
digitalWrite(led, HIGH);
}
StringIn = ""; // clear the string ready to build the next one
}
}
}
That is working for me and I can choose any baud rate for the comms, the handshaking will pause if its coming in to fast to proccess. you can change the pause on the transmitter side if required.
Connect pins 2,3 TX,RX and ground from one arduino to the other.
If you also connect the 5v line then you dont even need to power the remote arduino. Although care must be taken if you plug servos etc in the remote one as the power drawn for both arduino will be via the first one.
Albert