|
|

楼主 |
发表于 2013-8-15 11:39:44
|
显示全部楼层
本帖最后由 wunanyx 于 2013-8-15 11:42 编辑
我用easytransfer那个库,进行了修改
以下是tx的代码,三个变量,xyz轴的角速度- #include <L3G4200D.h>
- #include <Wire.h>
- #include <EasyTransfer.h>
- L3G4200D gyro;
- //create object
- EasyTransfer ET;
- struct SEND_DATA_STRUCTURE{
- //put your variable definitions here for the data you want to send
- //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
- int gx;
- int gy;
- int gz;
- };
- //give a name to the group of data
- SEND_DATA_STRUCTURE mydata;
- void setup(){
- Serial.begin(9600);
- //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
- Wire.begin();
- gyro.enableDefault();
- ET.begin(details(mydata), &Serial);
- pinMode(13, OUTPUT);
- randomSeed(analogRead(0));
- }
- void loop(){
- gyro.read();
- mydata.gx = gyro.g.x;
- mydata.gy = gyro.g.y;
- mydata.gz = gyro.g.z;
- //send the data
- ET.sendData();
- Serial.print(mydata.gx);
- Serial.print(" ");
- Serial.print(mydata.gy);
- Serial.print(" ");
- Serial.println(mydata.gz);
- delay(100);
- }
复制代码 以下是rx端的代码- #include <EasyTransfer.h>
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
- #include <Wire.h>
- //create object
- EasyTransfer ET;
- struct RECEIVE_DATA_STRUCTURE{
- //put your variable definitions here for the data you want to receive
- //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
- int gx;
- int gy;
- int gz;
- };
- //give a name to the group of data
- RECEIVE_DATA_STRUCTURE mydata;
- void setup(){
- Serial.begin(9600);
- //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
- ET.begin(details(mydata), &Serial);
- lcd.init(); // initialize the lcd
- // Print a message to the LCD.
- lcd.backlight();
- lcd.print("Set success!");
- pinMode(13, OUTPUT);
-
- }
- void loop(){
- //check and see if a data packet has come in.
- if(ET.receiveData()){
- //this is how you access the variables. [name of the group].[variable name]
- //since we have data, we will blink it out.
- int x = mydata.gx;
- // char y = mydata.gy;
- //char z = mydata.gz;
- lcd.clear();
- lcd.print(x,DEC);
- }
-
- //you should make this delay shorter then your transmit delay or else messages could be lost
- delay(100);
- }
复制代码 用1602的i2c作为观察,x轴的数据做检验
接线与第一次不同的是,tx rx反接rx tx,gnd对接gnd
上次只是将数据传到了rx的串口,并没有变量控制 |
|