调出了两块uno互连的程序
本帖最后由 wunanyx 于 2013-8-15 11:35 编辑这个程序有问题,我第二天又调试了一版,见楼下的~~~~~
这两天在坛子里找如何通过串口进行uno互连的程序,还是弘毅大版的et库最好
让我调出了两个uno互连的程序,一个uno连接gy-80,我读取角速度值,通过串口传给另个uno
接线方面,就用了两根线实现互连,一根是tx1对接,另一根有点意外,是gnd的对接。
#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);
}
以上是发送方的程序,含一个4200的角速度取值
以下是接收方的程序
#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. .
//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);
}
你那小台灯很有型,哪厂家产品?
林定祥 发表于 2013-8-14 19:57
你那小台灯很有型,哪厂家产品?
(⊙o⊙)…还真不知道,发的 wunanyx 发表于 2013-8-14 20:24 static/image/common/back.gif
(⊙o⊙)…还真不知道,发的
那个小弟弟当开关了:loveliness: 本帖最后由 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. .
//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的串口,并没有变量控制 http://v.youku.com/v_show/id_XNTk2MzI1OTA0.html LZ 的台灯很有型 ,当然程序也不错,多谢了 一看标题,我以为是用操作系统了......
页:
[1]