I am interfacing the Memsic 2125 accelerometer to the Arduino Pro Mini 5 V. I need to distinguish between the accelerometer being in the upright position and being upside down. As I rotated the accelerometer 180 degrees, the PulseIn values increased but then decreased back to the original upright value. I don’t understand why this is happening and if it is right.
here is my code that I got from a tutorial:
const int yPin = 5; // Y output of the accelerometer
// variables to read the pulse widths:
int pulseY;
void setup() {
// initialize serial communications:
Serial.begin(9600);
// initialize the pins connected to the accelerometer
// as inputs:
pinMode(yPin, INPUT);
}
void loop() {
// read pulse from y-axis:
pulseY = pulseIn(yPin,HIGH);
// print the acceleration
Serial.print(pulseY);
Serial.println();
delay(1000);
}
This is the y-axis output as I rotated it 180 degrees along the axis:
4972 //upright position
4976
5260
5531
5691
5864
6149
6208
6175
6134
6011
5885
5729
5608
5482
5402
5252
5087
4979
4972 //upside down
If this seems right, how can I distinguish between the accelerometer being upright and upside down?
Thank you.