Hardware Reference
In-Depth Information
The loop() calls a method to
convert the analog readings to acceler-
ation values. Then it plugs those results
into the trigonometric calculations
described above, and prints the results.
8
void loop() {
// read the accelerometer axes, and convert
// the results to acceleration values:
float xAxis = readAcceleration(analogRead(A0));
delay(10);
float yAxis= readAcceleration(analogRead(A1));
delay(10);
float zAxis = readAcceleration(analogRead(A2));
// apply trigonometry to get the pitch and roll:
float pitch = atan(xAxis/sqrt(pow(yAxis,2) + pow(zAxis,2)));
float roll = atan(yAxis/sqrt(pow(xAxis,2) + pow(zAxis,2)));
pitch = pitch * (180.0/PI);
roll = roll * (180.0/PI) ;
// print the results:
Serial.print(pitch);
Serial.print(",");
Serial.println(roll);
}
The readAcceleration() method
takes an analog reading and converts it
to an acceleration value from 0 to 1g.
8
float readAcceleration(int thisAxis) {
// the accelerometer's zero reading is at half
// its voltage range:
float zeroPoint = 1.65;
// convert the reading into a voltage:
float voltage = (thisAxis * 3.3 / 1024.0) - zeroPoint;
// divide by the accelerometer's sensitivity:
float acceleration = voltage / 0.3;
// return the acceleration in g's:
return acceleration;
}
 
Search WWH ::




Custom Search