-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwoADXL_oneARD
82 lines (60 loc) · 2.18 KB
/
twoADXL_oneARD
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Simple code for the ADXL335, prints calculated orientation via serial
//////////////////////////////////////////////////////////////////
//Analog read pins
const int xPin = 2;
const int yPin = 1;
const int zPin = 0;
const int xPin2 = 4; // this is the second ADXL
const int yPin2 = 5;
const int zPin2 = 3;
//The minimum and maximum values that came from
//the accelerometer while standing still
//You very well may need to change these
int minVal = 283;
int maxVal = 354;
//to hold the caculated values
double x;
double y;
double z;
double x2;
double y2; // this is the second ADXL
double z2;
void setup(){
Serial.begin(9600); //################################################################### BAUD BAUD
}
void loop(){
//read the analog values from the accelerometer
int xRead = analogRead(xPin);
int yRead = analogRead(yPin);
int zRead = analogRead(zPin);
//this is the second ADXL
int x2Read = analogRead(xPin2);
int y2Read = analogRead(yPin2);
int z2Read = analogRead(zPin2);
//convert read values to degrees -90 to 90 - Needed for atan2
int xAng = map(xRead, minVal, maxVal, -90, 90);
int yAng = map(yRead, minVal, maxVal, -90, 90);
int zAng = map(zRead, minVal, maxVal, -90, 90);
int x2Ang = map(x2Read, minVal, maxVal, -90, 90); //this is the second ADXL
int y2Ang = map(y2Read, minVal, maxVal, -90, 90);
int z2Ang = map(y2Read, minVal, maxVal, -90, 90);
//Caculate 360deg values like so: atan2(-yAng, -zAng)
//atan2 outputs the value of -π to π (radians)
//We are then converting the radians to degrees
x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);
x2 = RAD_TO_DEG * (atan2(-y2Ang, -z2Ang) + PI);
y2 = RAD_TO_DEG * (atan2(-x2Ang, -z2Ang) + PI);
z2 = RAD_TO_DEG * (atan2(-y2Ang, -x2Ang) + PI);
//Output the caculations
Serial.print(x);
Serial.print('-');
Serial.print('-');
Serial.print('-');
Serial.println(y2);
//delay(5);//just here to slow down the serial output - Easier to read
}