The code I got to work... well kind of is here. It is more or less thrown together and very messy due to debugging (lots of commented out commands). When I used the atan2(); function with the xGForce and yGForce values I get correct readings if I hold the accelerometer so the z-axis is perpendicular to gravity. There is no z axis on the chip but that should give a good picture of how I have to hold it.
I then tried to use the asin(); function which should work better with the 1000 mG's as the divisor but it ONLY outputs 0.00 or +/- 90.00 degrees... I know it should work but something is obviously being missed!
#include <math.h>
#define M_PI 3.141592653589793238462643
int xPin = 2;
int yPin = 3;
int xraw, xGForce, yraw, yGForce, xtime;
double angle;
void setup(){
Serial.begin(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
}
void loop(){
xraw = pulseIn (xPin, HIGH);
xraw = pulseIn (xPin, HIGH);
yraw = pulseIn (yPin, HIGH);
yraw = pulseIn (yPin, HIGH);
xGForce = ((xraw/10) - 500) *8;
yGForce = ((yraw/10) - 500) *8;
Serial.print(xGForce);
Serial.print(" ");
Serial.print(yGForce);
Serial.print(" ");
angle = atan2 (xGForce , yGForce);
//angle = asin(xGForce/1000);
//printDouble(angle,100);
Serial.print(" ");
angle = angle * (360/(2*M_PI));
printDouble(angle, 100);
delay(100);
}
// printDouble found on arduino forums through Google search.
// code written by user mems.
void printDouble( double val, unsigned int precision){
Serial.print (int(val));
Serial.print (".");
unsigned int frac;
if(val>=0){
frac = (val - int(val)) * precision;
}
else
frac = (int(val) - val ) * precision;
int frac1 = frac;
while( frac1 /= 10 ){
precision /= 10;
}
precision /= 10;
while( precision /= 10){
Serial.print("0");
}
Serial.println(frac, DEC);
}
Bryce
KB1LQC