-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.ino
61 lines (47 loc) · 1.13 KB
/
Code.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <SPI.h>
#include <Wire.h>
#include <Servo.h>
#define MPU 0x68
Servo ServoX, ServoY;
double AcX,AcY,AcZ;
int Pitch, Roll;
void setup(){
//Serial.begin(9600);
ServoX.attach(3);
init_MPU();
}
void loop()
{
FunctionsMPU();
Roll = FunctionsPitchRoll(AcX, AcY, AcZ);
int ServoRoll = map(Roll, -75, 75, 0, 180);
ServoX.write(ServoRoll);
Serial.print("Roll: "); Serial.print(Roll);
Serial.print("\n");
}
void init_MPU(){
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
delay(1000);
}
double FunctionsPitchRoll(double A, double B, double C){
double DataA, DataB, Value;
DataA = A;
DataB = (B*B) + (C*C);
DataB = sqrt(DataB);
Value = atan2(DataA, DataB);
Value = Value * 180/3.14;
return (int)Value;
}
void FunctionsMPU(){
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU,6,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
}