Skip to content

Instantly share code, notes, and snippets.

@jbott jbott/acc_to_angles.ino
Last active Jul 7, 2016

Embed
What would you like to do?
Set of scripts to do get angles from the 9dof sensor stick
#include <Wire.h>
#include <I2Cdev.h>
#include <ADXL345.h>
ADXL345 acc;
void setup() {
Serial.begin(9600);
Wire.begin();
acc.initialize();
delay(100);
}
void loop() {
int16_t aX, aY, aZ;
acc.getAcceleration(&aX, &aY, &aZ);
float gX = (float) aX / 1023.0 * 5;
float gY = (float) aY / 1023.0 * 5;
float gZ = (float) aZ / 1023.0 * 5;
//Roll & Pitch Equations
double roll = (atan2(-gY, gZ)*180.0)/M_PI;
double pitch = (atan2(gX, sqrt(gY*gY + gZ*gZ))*180.0)/M_PI;
Serial.print(pitch);
Serial.print(":");
Serial.println(roll);
delay(100);
}

#include <Wire.h> #include <I2Cdev.h> #include <ADXL345.h>

ADXL345 acc;

void setup() { Serial.begin(9600); Wire.begin(); acc.initialize(); delay(100); }

void loop() { int16_t aX, aY, aZ; acc.getAcceleration(&aX, &aY, &aZ);

float gX = (float) aX / 1023.0 * 5; float gY = (float) aY / 1023.0 * 5; float gZ = (float) aZ / 1023.0 * 5;

//Roll & Pitch Equations double roll = (atan2(-gY, gZ)180.0)/M_PI; double pitch = (atan2(gX, sqrt(gYgY + gZ*gZ))*180.0)/M_PI;

Serial.print(pitch); Serial.print(":"); Serial.println(roll);

delay(100); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.