http://dma.ucla.edu/senselab/node/389<br /><br />#include <softwareserial.h><br /><br />/* <br />Read a pushbutton and send a value<br />to another microcontroller. <br />*/<br /><br />#define rxPin 2<br />#define txPin 3<br />#define buttonPin 12<br />#define ledPin 13<br /><br />// set up a new serial connection for communicating with RF transmitter <br />// (this also frees up Arduino's built in serial port for PC communication)<br />SoftwareSerial rfSerial = SoftwareSerial(rxPin, txPin);<br /><br />byte val = 0;<br />byte onState = 99;<br />byte offState = 98;<br /><br />void setup() {<br /> pinMode(buttonPin, INPUT); // declare pushbutton as input<br /> pinMode(ledPin, OUTPUT); // declare led as output<br /> rfSerial.begin(2400); // our RF Link is a 2400 baud model (make sure you check this!)<br /> // start by blinking high and then low so we can tell the setup routine has been completed<br /> digitalWrite(ledPin,HIGH);<br /> delay(1000);<br /> digitalWrite(ledPin,LOW);<br />}<br /><br />void loop(){<br /> val = digitalRead(buttonPin);<br /> if ( val == HIGH ) { // check if the input is HIGH (button pressed)<br /> rfSerial.print(onState, BYTE); // OK send it to the RF transmitter<br /> } else {<br /> rfSerial.print(offState, BYTE); // send a 0 through the transmitter<br /> }<br />}<br /></softwareserial.h></pre><p>
OK and here is the receiver code which goes on Arduino #2. Once again
I'm using the SoftwareSerial library, but this time it's necessary
because I need to use the usual Serial port to send data up the USB to
the computer. The loop is filled with a little extra code to avoid
sending the same byte over and over and over. A byte is only sent if it
is new (changes from "off" byte to "on" byte for instance and no data
would be sent again until it goes back "off").</p>
<pre>#include <softwareserial.h><br /><br />/* <br />Read from a RF Link receiver module<br />and get the data into a computer. <br />*/<br /><br />#define rxPin 2<br />#define txPin 3<br />#define ledPin 13<br /><br />// set up a new serial connection for communicating with RF receiver <br />// (this also frees up Arduino's built in serial port for PC communication)<br />SoftwareSerial rfSerial = SoftwareSerial(rxPin, txPin);<br /><br />char prevChar = 0;<br /><br />void setup() {<br /> // set up the input and output pins<br /> pinMode(rxPin, INPUT); // set up pins for serial comm. with RF receiver<br /> pinMode(ledPin, OUTPUT);<br /> // initialize serial comm<br /> rfSerial.begin(2400); // begin serial connection with RF Link unit<br /> Serial.begin(2400); // begin serial communication over USB to the computer<br /> // blink LED on and then off just to let us know the setup routine is complete<br /> digitalWrite(ledPin,HIGH); // turn on LED<br /> delay(1000);<br /> digitalWrite(ledPin,LOW); // turn off LED<br />}<br /><br />void loop(){<br /> char someChar = '0'; <br /> someChar = rfSerial.read(); // read whatever the RF Link has to offer<br /> // print out the character: <br /> if (someChar!=prevChar) { // only print out new data (don't print 0 a billion times in a row)<br /> Serial.print(someChar, BYTE); // send data to computer<br /> prevChar=someChar; // store what we just read<br /> }<br />}<br /></softwareserial.h>
2008年10月27日 星期一
RF Link between two Arduinos
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言