极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10463|回复: 0

问个问题 卡你看

[复制链接]
发表于 2013-11-9 00:33:52 | 显示全部楼层 |阅读模式
最近在用arduino 弄一个三轴磁场的程序,弄出来后有三个值如x=126,y=125
z=58,我想反映出磁场的变化就把他合并为x2+y2+z2再开根号,但是这样发现 合并为一个结果反映的时候,变化相当的大,我想问下能不能有x轴现在的数字与x轴0.5秒前的数值相减,根据变化量来做出点亮led灯的控制(同轴变化量),谢谢

以前的老大妈是这个#include <Wire.h> //I2C Arduino Library  
  
#define address 0x1E //0011110b, I2C 7bit address of HMC5883  
int ledPin = 13;
void setup(){  
  //Initialize Serial and I2C communications  
  Serial.begin(9600);  
  Wire.begin();  
   
  //Put the HMC5883 IC into the correct operating mode  
  Wire.beginTransmission(address); //open communication with HMC5883  
  Wire.write(0x02); //select mode register  
  Wire.write(0x00); //continuous measurement mode  
  Wire.endTransmission();  
}  
  
void loop(){  
   
  int x,y,z,baa,all; //triple axis data  
  
  //Tell the HMC5883 where to begin reading data  
  Wire.beginTransmission(address);  
  Wire.write(0x03); //select register 3, X MSB register  
  Wire.endTransmission();  
   
   
//Read data from each axis, 2 registers per axis  
  Wire.requestFrom(address, 6);  
  if(6<=Wire.available()){  
    x = Wire.read()<<8; //X msb  
    x |= Wire.read(); //X lsb  
    z = Wire.read()<<8; //Z msb  
    z |= Wire.read(); //Z lsb  
    y = Wire.read()<<8; //Y msb  
    y |= Wire.read(); //Y lsb  
  }  
    baa=sq(x) + sq(y)+ sq(z) ;
    all=sqrt(baa);
  if(all>190){
    digitalWrite(13,HIGH);
   Serial.print("abs(all): ");  
   Serial.print(abs(all));   
   delay(5000);  
}
else
      digitalWrite(13,LOW);
      Serial.print("abs(all): ");  
   Serial.print(abs(all));   
delay(5000);   
}  
回复

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 18:36 , Processed in 0.061986 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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