极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 21248|回复: 7

Arduino Rx 读取数值问题 求大家帮忙

[复制链接]
发表于 2014-3-12 06:02:18 | 显示全部楼层 |阅读模式
大家好,小女子是个新手。
请问我现在想用Arduino Uno Rx 读取以下数值要怎么做?
我已经将Arduino 第一板的Tx连接到第二块板的Rx


我用Serial.println(y)在第一块板上输出int y的值, 却没办法在第二块板上用Serial.read(y)读出相等数值


请求各位大神帮助!!!!!!

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2014-3-12 08:37:28 | 显示全部楼层
在具体一点,画个图吧,要不然不知道你想干嘛
回复 支持 反对

使用道具 举报

发表于 2014-3-12 08:40:03 | 显示全部楼层
正如1楼所述
需要2个条件:
1.你的源码
2.你的接线方式(你这个仅仅是跳连RX和TX么)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-12 21:45:47 | 显示全部楼层
darkorigin 发表于 2014-3-12 08:40
正如1楼所述
需要2个条件:
1.你的源码

1.源码
#include <Wire.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24

int L3G4200D_Address = 105; //I2C address of the L3G4200D

int x;
int y;
int z;

void setup(){
  
  Serial.begin(9600);
  Wire.begin();

  setupL3G4200D(2000); // Configure L3G4200  - 250, 500 or 2000 deg/sec

  delay(1500); //wait for the sensor to be ready
  
}

void loop(){
   getGyroValues();  // This will update x, y, and z with new values

   Serial.println(y);
   
  delay(100); //Just here to slow down the serial to make it more readable
}

void getGyroValues(){

  byte xMSB = readRegister(L3G4200D_Address, 0x29);
  byte xLSB = readRegister(L3G4200D_Address, 0x28);
  x = ((xMSB << 8)| xLSB);

  byte yMSB = readRegister(L3G4200D_Address, 0x2B);
  byte yLSB = readRegister(L3G4200D_Address, 0x2A);
  y = ((yMSB << 8)| yLSB);

  byte zMSB = readRegister(L3G4200D_Address, 0x2D);
  byte zLSB = readRegister(L3G4200D_Address, 0x2C);
  z = ((zMSB << 8)| zLSB);
}

int setupL3G4200D(int scale){

  writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);

  writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);

  writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);

  if(scale == 250){
    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
  }else if(scale == 500){
    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
  }else{
    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
  }
  writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}

void writeRegister(int deviceAddress, byte address, byte val) {
    Wire.beginTransmission(deviceAddress); // start transmission to device
    Wire.write(address);       // send register address
    Wire.write(val);         // send value to write
    Wire.endTransmission();     // end transmission
}

int readRegister(int deviceAddress, byte address){

    int v;
    Wire.beginTransmission(deviceAddress);
    Wire.write(address); // register to read
    Wire.endTransmission();

    Wire.requestFrom(deviceAddress, 1); // read a byte

    while(!Wire.available()) {
        // waiting
    }

    v = Wire.read();
    return v;
}

2.我用两条线交叉连接了两块板的RX和TX
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-12 21:48:14 | 显示全部楼层
zoologist 发表于 2014-3-12 08:37
在具体一点,画个图吧,要不然不知道你想干嘛

我用第一个板传送陀螺仪上的Y轴数值源码如上,想用第二块板读取相应的y轴数值从而控制伺服的角度。只是第二块板怎么都读不如图上一致的相同数值
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-12 21:57:35 | 显示全部楼层
zoologist 发表于 2014-3-12 08:37
在具体一点,画个图吧,要不然不知道你想干嘛

第二块板的源码。。。。 可是接收的数据却是完全不同的。。。
#include <Servo.h>

int y;
int currentAngle1=90;

Servo myservo1;  //servo1 for y-axis

void setup(){
  Serial.begin(9600);

  myservo1.attach(10); //servo for y-axis

}

void loop(){

   y=Serial.read();
   Serial.println(Serial.read());
  
   delay(100);
  
if(y>=200||y<=-200){
   y/=130; //set the y value divides by time to reach the angle
   currentAngle1+=y;
}

   if(currentAngle1>180)
   currentAngle1=180;
     else if(currentAngle1<0)
     currentAngle1=0;

   myservo1.write(currentAngle1);
  
}

____
54
10
53
10
54
10
53
10
55
10
56
10
53
10
54
10
54
10
56
10
回复 支持 反对

使用道具 举报

发表于 2014-3-13 09:06:04 | 显示全部楼层
个人感觉不像是读取的问题,有可能是第二块板子串口冲突了?

建议你实验第一块板子发固定的数值,第二个板子直接接收并且打印出来

或者直接试试软串口来接受
回复 支持 反对

使用道具 举报

发表于 2014-3-13 09:15:22 | 显示全部楼层
你这个出来的值似乎很规律啊,都是53-57之间的值加一个 10  
根据ASCII码表 貌似是字符5  6 7 8等的值  10是TAB的ASCII值。
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-15 03:21 , Processed in 0.039124 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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