|
各位大大好 我專題想運用adxl345做伺服馬達的補正讓他永遠維持在我要的位子這是我的code(我是用arduino uno版)現在遇到的問題是我可以燒到我的arduino裡面,comiling有沒有問題但讀出來的Xg值一直為3.91
伺服馬達也不會因我移動ADXL345而改變
#include <Wire.h>
#include <Servo.h>
#define Register_ID 0
#define Register_2D 0x2D
#define Register_X0 0x32
#define Register_X1 0x33
#define Register_Y0 0x34
#define Register_Y1 0x35
int sensorValue0 = 0;
int s1pwm = 93;
Servo s1;
int ADXAddress = 0xA7>>1;
int reading = 0;
int val = 0;
int X0,X1,X_out;
double Xg;
void setup()
{
Serial.begin(9600);
s1.attach(6);
Wire.begin(); //初始化I2C
delay(100);
Wire.beginTransmission(ADXAddress);
Wire.send(Register_2D);
Wire.endTransmission();
}
void loop()
{
Wire.beginTransmission(ADXAddress);
Wire.send(Register_X0);
Wire.send(Register_X1);
Wire.endTransmission();
Wire.requestFrom(ADXAddress,2);
sensorValue0=Xg;
sensorValue0 = map(sensorValue0, -70,70 ,0 ,179 );
s1.write(sensorValue0);
if(Wire.available()<=2);
{
X0 = Wire.receive();
X1 = Wire.receive();
X1 = X1<<8;
X_out = X0+X1;
}
delay(100);
Serial.print("Xg = " );
Serial.println(Xg);
delay(100);
Xg = (X_out-14.4)/3.68;
} |
|