Thanks! I got it working for the most part by forcing the decimal place in the 1000.0! I totally forgot about doing that, did it a few months ago when playing around with the analog input and voltages. So here is the nice cleaned-up code:
#include <math.h>
int xPin = 2;
int yPin = 3;
int Xraw, Yraw;
double xGForce, yGForce, Xangle, Yangle;
void setup(){
Serial.begin(9600);
pinMode(yPin, INPUT);
pinMode(xPin, INPUT);
}
void loop(){
// Due to erroneous output of pulseIn();
// when x and y are calculated together
// pulseIn(); must be used twice.
Xraw = pulseIn (xPin, HIGH);
Xraw = pulseIn (xPin, HIGH);
Yraw = pulseIn (yPin, HIGH);
Yraw = pulseIn (yPin, HIGH);
// Calculate G-force in Milli-g's.
xGForce = (( Xraw / 10 ) - 500) * 8;
yGForce = (( Yraw / 10 ) - 500) * 8;
// uncomment next four lines for milli-g output.
//Serial.print(xGForce);
//Serial.print(" ");
//Serial.print(yGForce);
//Serial.print(" ");
// Calculate angle (radians) for both -x and -y axis.
Xangle = asin ( xGForce / 1000.0 );
Yangle = asin ( yGForce / 1000.0 );
// Convert radians to degrees.
Xangle = Xangle * (360 / (2*M_PI));
Yangle = Yangle * (360 / (2*M_PI));
// Print the angle with printDouble function
printDouble (Xangle, 10);
printDouble (Yangle, 10);
// Delay for number of measurments per second needed.
delay(100);
}
void printDouble( double val, unsigned int precision){
//code based off of arduino code found at:
//
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);
}
It works very well until you get near 90 degrees. Signs of trouble start showing up around 75 degrees. When the axis is close to vertical then the output jumps from >75 degrees roughly to 90.0 degrees and to 0.0 degrees. I will post a picture of the Hyperterminal output soon. Any ideas on how to correct for that or is it inherently from the inaccuracy of the device about 50 degrees (MXD2125 data sheet).
Bryce
KB1LQC