极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13382|回复: 7

调出了两块uno互连的程序

[复制链接]
发表于 2013-8-14 18:10:54 | 显示全部楼层 |阅读模式
本帖最后由 wunanyx 于 2013-8-15 11:35 编辑

这个程序有问题,我第二天又调试了一版,见楼下的~~~~~

这两天在坛子里找如何通过串口进行uno互连的程序,还是弘毅大版的et库最好
让我调出了两个uno互连的程序,一个uno连接gy-80,我读取角速度值,通过串口传给另个uno
接线方面,就用了两根线实现互连,一根是tx1对接,另一根有点意外,是gnd的对接。
[pre lang="arduino" line="1" file="tx"]#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;
};

//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();  
int gx = gyro.g.x;  
  //send the data
  ET.sendData();
  Serial.println(gx);
  delay(100);
}[/code]
以上是发送方的程序,含一个4200的角速度取值
以下是接收方的程序

[pre lang="arduino" line="1" file="rx"]#include <SoftEasyTransfer.h>

/*   For Arduino 1.0 and newer, do this:   */
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);

/*   For Arduino 22 and older, do this:   */
//#include <NewSoftSerial.h>
//NewSoftSerial mySerial(2, 3);


//create object
SoftEasyTransfer 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;

};

//give a name to the group of data
RECEIVE_DATA_STRUCTURE mydata;

void setup(){
  mySerial.begin(9600);
  //start the library, pass in the data details and the name of the serial port.
  ET.begin(details(mydata), &mySerial);
  
  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.
  char x = mydata.gx;
    Serial.println(x);
  }
  //you should make this delay shorter then your transmit delay or else messages could be lost
  delay(100);
}[/code]

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2013-8-14 19:57:49 | 显示全部楼层
你那小台灯很有型,哪厂家产品?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-8-14 20:24:22 来自手机 | 显示全部楼层
林定祥 发表于 2013-8-14 19:57
你那小台灯很有型,哪厂家产品?

(⊙o⊙)…还真不知道,发的
回复 支持 反对

使用道具 举报

发表于 2013-8-14 21:20:19 | 显示全部楼层
wunanyx 发表于 2013-8-14 20:24
(⊙o⊙)…还真不知道,发的

那个小弟弟当开关了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-8-15 11:39:44 | 显示全部楼层
本帖最后由 wunanyx 于 2013-8-15 11:42 编辑

我用easytransfer那个库,进行了修改
以下是tx的代码,三个变量,xyz轴的角速度
  1. #include <L3G4200D.h>
  2. #include <Wire.h>
  3. #include <EasyTransfer.h>
  4. L3G4200D gyro;
  5. //create object
  6. EasyTransfer ET;

  7. struct SEND_DATA_STRUCTURE{
  8.   //put your variable definitions here for the data you want to send
  9.   //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  10.   int gx;
  11.   int gy;
  12.   int gz;
  13. };

  14. //give a name to the group of data
  15. SEND_DATA_STRUCTURE mydata;

  16. void setup(){
  17.   Serial.begin(9600);
  18.   //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.      
  19.     Wire.begin();
  20.   gyro.enableDefault();  
  21.   ET.begin(details(mydata), &Serial);  
  22.   pinMode(13, OUTPUT);  
  23.   randomSeed(analogRead(0));
  24. }

  25. void loop(){
  26.     gyro.read();  
  27.   mydata.gx = gyro.g.x;
  28.   mydata.gy = gyro.g.y;
  29.    mydata.gz = gyro.g.z;
  30.   //send the data
  31.   ET.sendData();
  32.   Serial.print(mydata.gx);
  33.   Serial.print(" ");  
  34.   Serial.print(mydata.gy);
  35.     Serial.print(" ");
  36.     Serial.println(mydata.gz);
  37.   delay(100);
  38. }
复制代码
以下是rx端的代码
  1. #include <EasyTransfer.h>
  2. #include <LiquidCrystal_I2C.h>
  3. LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
  4. #include <Wire.h>
  5. //create object
  6. EasyTransfer ET;

  7. struct RECEIVE_DATA_STRUCTURE{
  8.   //put your variable definitions here for the data you want to receive
  9.   //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  10.   int gx;
  11.   int gy;
  12.   int gz;
  13. };

  14. //give a name to the group of data
  15. RECEIVE_DATA_STRUCTURE mydata;

  16. void setup(){
  17.   Serial.begin(9600);
  18.   //start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
  19.   ET.begin(details(mydata), &Serial);
  20.           lcd.init();                      // initialize the lcd
  21.   // Print a message to the LCD.
  22.    lcd.backlight();
  23.    lcd.print("Set success!");
  24.   pinMode(13, OUTPUT);
  25.   
  26. }

  27. void loop(){
  28.   //check and see if a data packet has come in.
  29.   if(ET.receiveData()){
  30.     //this is how you access the variables. [name of the group].[variable name]
  31.     //since we have data, we will blink it out.
  32. int x = mydata.gx;
  33. //  char y = mydata.gy;
  34.   //char z = mydata.gz;
  35.           lcd.clear();
  36.         lcd.print(x,DEC);
  37.   }
  38.   
  39.   //you should make this delay shorter then your transmit delay or else messages could be lost
  40.   delay(100);
  41. }

复制代码
用1602的i2c作为观察,x轴的数据做检验

接线与第一次不同的是,tx rx反接rx tx,gnd对接gnd

上次只是将数据传到了rx的串口,并没有变量控制
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-8-15 15:34:23 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2013-8-15 17:07:38 | 显示全部楼层
LZ 的台灯很有型 ,当然程序也不错,多谢了
回复 支持 反对

使用道具 举报

发表于 2013-8-15 17:08:52 | 显示全部楼层
一看标题,我以为是用操作系统了......
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-8 15:34 , Processed in 0.037747 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表