本帖最后由 nemon 于 2013-12-24 18:53 编辑
xbee模块主要功能是实现无线数据通信,利用xbee模块分别与pcDuino和Arduino连接,通过串口读写数据,可以轻松实现二者之间的数据传输。在pcDuino端按下开关按钮控制arduino端的LED灯亮,反之arduino端的开关则控制pcDuino端的LED灯。
线路连接:通过T-board 连接pcDuino和xbee模块,T-board上的电平开关必须打到5VArduino部分 注意:xbee的波特率设置为9600
线路连好之后,编译运行程序代码。运行成功后,按下pcDuino这端的LED灯开关,红色LED灯亮
按下Arduino这端的LED灯开关,绿灯亮
程序代码:
Code for pcDuino:
- #include <core.h>
-
- #include "Serial.h"
-
- #define led_pin 9
-
- #define button_pin 10
-
- unsigned char flag;
-
- void setup()
-
- {
-
- //Initialize serial and wait for port to open:
-
- pinMode(led_pin,OUTPUT);
-
- pinMode(button_pin,INPUT);
-
- int rate = 9600;
-
- Serial.begin(rate);
-
- while (!Serial)
-
- {
-
- ; // wait for serial port to connect. Needed for Leonardo only
-
- }
-
- }
-
- void loop()
-
- {
-
- if (Serial.available() > 0)
-
- {
-
- char receive = Serial.read();
-
- if(receive=='a') flag++;
-
- if( (flag%2) ) digitalWrite(led_pin,HIGH); //LED off
-
- else digitalWrite(led_pin,LOW); //LED on
-
- }
-
- if(digitalRead(button_pin)==LOW)
-
- {
-
- delay(10);
-
- if(digitalRead(button_pin)==LOW)
-
- Serial.print('b');
-
- delay(200);
-
- }
-
- }
复制代码
Code for arduino:
- #include <SoftwareSerial.h>
-
- #define RxD 11
-
- #define TxD 12
-
- #define led_pin 9
-
- #define button_pin 10
-
- unsigned char flag;
-
- SoftwareSerial mySerial(RxD,TxD);
-
- void setup()
-
- {
-
- pinMode(RxD, INPUT);
-
- pinMode(TxD, OUTPUT);
-
- pinMode(led_pin,OUTPUT);
-
- pinMode(button_pin,INPUT);
-
- mySerial.begin(9600); // the Bee baud rate
-
- }
-
- void loop()
-
- {
-
- if(digitalRead(button_pin)==LOW)
-
- {
-
- delay(10);
-
- if(digitalRead(button_pin)==LOW)
-
- mySerial.print('a');
-
- delay(200);
-
- }
-
- if(mySerial.available())
-
- {
-
- char RX = mySerial.read();
-
- if(RX=='b') flag++;
-
- if(flag%2) digitalWrite(led_pin,HIGH);
-
- else digitalWrite(led_pin,LOW);
-
- }
-
- }
复制代码
|
|
|
|
|
|