So I was fooling around with the SparkFun RF Link - 4800bps - 315MHz for a school project. I needed to make a simple transmitter and receiver to control two servo motors. I don't have two arduinos so I made do with the BS2 I got in highschool. The code is modified so its pretty generic now.
A few things to note:
- BS2 really can't handle 4800bps so I made it run at 2400bps which worked fine.
- Sometimes garbage is received so we need to send a start byte and an end byte so that we can verify what is valid.
From the code below and the datasheet for the transmitter I hooked it up. Pin 4 from the arduino is hooked up to the rx pin on the transmitter.
I used the software serial library to set up everything.
// Include the software serial library so we can control the RF
#include <SoftwareSerial.h>
// Define some pins
#define rftx 4
#define rfrx 3
// Set up a new serial port for RF
SoftwareSerial rfSer = SoftwareSerial(rfrx, rftx);
void setup()
{
// Define pin modes for rx, tx
pinMode(rfrx, INPUT);
pinMode(rftx, OUTPUT);
// Set the data rate for the serial ports
rfSer.begin(2400);
Serial.begin(9600);
}
void loop()
{
delay(100);
rfSer.print("A11*"); //Send a command. Start bit is A, end bit is *
delay(100);
}
From here on, all sent commands are: rfSer.print("A(command)*");
The receiver code is also pretty simple. The hardest part for me was trying to set the baud mode on the BS2. This may be intuitive to some, but I'm an idiot so, sorry. In this case its 396. The tx pin of the receiver is hooked up to pin 10 on the BS2.
' {$STAMP BS2}
CHOICE VAR Byte(2)
loop:
CHOICE = 0
SERIN 10, 396, [WAIT("A"),STR CHOICE\2\"*"]
DEBUG CHOICE, CR
IF CHOICE(0) = "1" AND CHOICE(1) = "1" THEN COMMANDA
IF CHOICE(0) = "1" AND CHOICE(1) = "2" THEN COMMANDB
IF CHOICE(0) = "1" AND CHOICE(1) = "3" THEN COMMANDC
IF CHOICE(0) = "1" AND CHOICE(1) = "4" THEN COMMANDD
GOTO loop
COMMANDA:
'stuff
GOTO loop
COMMANDB:
'stuff
GOTO loop
COMMANDC:
'stuff
GOTO loop
COMMANDD:
'stuff
GOTO loop
All of the BS2 code can be changed. Right now it takes commands 11, 12, 13, 14 and then the stamp can execute code based on those commands. This turned out to be a pretty simple project, but for a n00b like me, it was a nice challenge. Hope that someone can make use of this.
Nick