Skip to content

Instantly share code, notes, and snippets.

@jbott jbott/000-links.md

Last active Jul 7, 2016
Embed
What would you like to do?
Arduino Accelerometer Angles
#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 <ITG3200.h>
// Set a default time of 100 ms
#define TIME_STEP 100
ITG3200 gyro;
float roll = 0;
float pitch = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
gyro.initialize();
delay(100);
}
void loop() {
unsigned long timer = millis();
int16_t gx, gy, gz;
gyro.getRotation(&gx, &gy, &gz);
roll = roll + (float)gx/14.375 * TIME_STEP / 1000.0;
pitch = pitch + (float)gy/14.375 * TIME_STEP / 1000.0;
while (millis() - timer < TIME_STEP) {}
Serial.print(pitch);
Serial.print(":");
Serial.println(roll);
}
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.