Skip to content

Instantly share code, notes, and snippets.

@jbott
Last active July 7, 2016 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbott/733af3e0124e2c13fbdc417aed8547b9 to your computer and use it in GitHub Desktop.
Save jbott/733af3e0124e2c13fbdc417aed8547b9 to your computer and use it in GitHub Desktop.
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