Hi,
I am trying to read the acceleration data from the memsic 2125, and the code from here doesn’t work the way I expect, so I’ve borrowed and modified some other code to get what I want:
int xPin = 5; // x-axis input (pin 5 from memsic)
int yPin = 4; // y-axis input (pin 2 from memsic)
unsigned long xDuration;
unsigned long yDuration;
unsigned long xLow;
unsigned long yLow;
unsigned long xAccel;
unsigned long yAccel;
/* (int) Operate Acceleration
* function to calculate acceleration
* returns a float so that precision isn't lost
* by dividing time2 by 1000.0 puts the overall value on a 1000.0 point scale.
*/
float operateAcceleration(int time1,int time2) {
//Serial.print(time1);
return 8.0 * ((float)time1 / ((float)time2/1000.0) - 500.0);
}
void setup() {
beginSerial(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
}
void loop(){
yDuration = pulseIn(yPin, HIGH); // pulseIn gives the time in microseconds
yLow = pulseIn(yPin,LOW);
xDuration = pulseIn(xPin, HIGH);
xLow = pulseIn(xPin,LOW);
xAccel = (int)operateAcceleration(xDuration,xDuration+xLow); // estimate time2 for x
yAccel = (int)operateAcceleration(yDuration,yDuration+yLow); // estimate time2 for y
//serialWrite('Y');
//serialWrite('-');
Serial.print("Y,X: ");
//printInteger(yDuration);
//Serial.print(" LOW:");
// printInteger(yLow);
//Serial.print("ms ");
//Serial.print(" Total: ");
// printInteger(yDuration+yLow);
// serialWrite(' ');
printInteger(yAccel);
// Serial.print(" accel");
//printByte(10);
Serial.print(",");
//printInteger(xDuration);
//Serial.print(" LOW:");
//printInteger(xLow);
//Serial.print(" Total: ");
//printInteger(xDuration+xLow);
//serialWrite('X');
//serialWrite(' ');
//printInteger(xDuration);
//Serial.print("ms ");
printInteger(xAccel);
//Serial.print(" accel");
printByte(10);
delay(100);
}
I have followed the instructions from page 70-71 of this document to test if the code is working, except the difference is that the X and Y values should be from -1000 to 1000 instead of from 1875 to 3125.
I get the following results:
When tilting the x axis, I get values around -1000, 0 , and 1000 (+/- around 100)
When tilting the y axis, I get values around -3000 (+/- 1000), as I approach level the values sometimes approach 0, and as I tilt up, I get values around 1000 (+/- 500).
I can’t figure out why I would be getting such noisy values for y but not for x.
Any ideas?
thanks.
David