memsic 2125 accelerometer calcs - parallax

I am having difficulty understanding the code in the examples section for the memsic accelerometer. My misunderstanding started when i was trying to figure out why the count was multiplied by 18 to get the number of milliseconds that the digital input was HIGH. Can anyone shed light on the number 18?

I then started to look at things like was it linked to baud rate or was i missing something with the rate at which pulses are generated... The code i ended up thinking was correct (but doesn't appear to be) was:

// with a baud rate of 9600 each count is actually 0.104 bits per second timer = (count * 10) / 96;

i then looked at the equation in the Parallax documentation (equation for avg G in milli g's = ((T1 / T2)-0.5) / 12.5%) where T2 is calibrated to 10ms at 25 deg C - room temp - and T1 is the milliseconds that we are trying to pass in. If T2 is the total duty cycle and T1 is the HIGH component of the HIGH LOW duty cycle - shouldn't T1 always be less than T2? This then confused me even more as I could then not understand the equation in operateAcceleration

Can anyone help?


Just wanted to see if you had found any success in figuring tilt with the Memsic chip?

I have been working with the examples you mentioned, both from Parallax and from the arduino site, and have many of the same questions. Below is my code. It’s kind of a jumble between a couple examples. I tried to duplicate the math/code from a Basic Stamp example into Arduino code.

Please let me know if you can help in any way.

Thanks in advance.

Bill Goodrich


#include <math.h>

int ledPin = 13;
int xaccPin = 3;
int yaccPin = 2;
float xRaw;
float yRaw;
float xmG;
float ymG;
int Scale = 18; //value to multiply pulse width by to convert to microseconds for particular chip, in this case the atmega168
float disp;
int angle;
int xTilt;
int yTilt;

void setup() {
Serial.begin(9600); // Sets the baud rate to 9600
pinMode(ledPin, OUTPUT);
pinMode(xaccPin, INPUT);
pinMode(yaccPin, INPUT);
Serial.println("Hello World");

void loop() {

/* Read G Force on X and Y axis */
xRaw = pulseIn(xaccPin, HIGH);   //read the duration of a high pulse on the x pin
Serial.println(xRaw, DEC);
xRaw = xRaw * Scale;  //convert to microseconds. need to find conversion factor for arduino
Serial.println(xRaw, DEC);
xmG = ((xRaw / 10) - 500) * 8;  // calc milli-g's
Serial.println(xmG, DEC);
yRaw = pulseIn(yaccPin, HIGH);
Serial.println(yRaw, DEC);
yRaw = yRaw * Scale;
Serial.println(yRaw, DEC);
ymG = ((yRaw / 10) - 500) * 8;
Serial.println(ymG, DEC);
Serial.println("end of g force calc");

/* Convert G force to Tilt on X and Y axis */
Serial.println("begin tilt calc");
disp = constrain ((abs(xmG) / 10), 0, 99);
Serial.println(disp, DEC);
Serial.println(angle, DEC);
xTilt = angle * (-2 * xmG + 1);  //fix sign
disp = constrain ((abs(ymG) / 10), 0, 99);
yTilt = angle * (-2 * ymG + 1);

/* Print results to serial port */


/* Arcsine function */
void Arcsine(float disp){
  disp = disp * 0.574 ; //normalize input to 127. value should be 0.574 or 3.84
  Serial.println("arcsine step 1");
  Serial.println(disp, DEC);
  angle = 63 - (disp / 2);  //approximate angle
  Serial.println("arcsine step 2");
  Serial.println(angle, DEC);
  do  //find angle
    angle = angle + 1;
    Serial.println(angle, DEC);
  } while (cos(angle) <= disp);  //problem with program seems to occur here.  gets caught in loop. seems to be a number prior to getting here that causes it, though.
  angle = angle / 360;  //is this right?

/* Arccosine function */
//void Arccosine(int angle){
//  angle = Arcsine(angle);
//  angle = 90 - angle;