I need some help with coding the Memsic 2125 for the Arduino. what I Want to do is make a light appear on the Arduino when the Memsic 2125 recieves 900 or more milli-G’s on either axis (X or Y). I however, have a problem I can’t seem to fix, I can’t make the light stop when the arduino encounters less than 900 milli-G’s, and I need to hit the reset button to make the LED stop lighting so that I can re measure the values. so if anyone could edit my code here so that I could be able to have the LED stop automatically when it is less than 900 milli-G’s. anyways, here is my code !
const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer
void setup() {
// initialize serial communications:
Serial.begin(9600);
// initialize the pins connected to the accelerometer
// as inputs:
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
}
void loop() {
// variables to read the pulse widths:
int pulseX, pulseY;
// variables to contain the resulting accelerations
int accelerationX, accelerationY;
// read pulse from x- and y-axes:
pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(yPin,HIGH);
// convert the pulse width into acceleration
// accelerationX and accelerationY are in milli-g's:
// earth's gravity is 1000 milli-g's, or 1g.
accelerationX = ((pulseX / 10) - 500) * 8;
accelerationY = ((pulseY / 10) - 500) * 8;
if (accelerationX > 900) digitalWrite(13, HIGH);
if (accelerationX < -900) digitalWrite(13, HIGH);
if (accelerationY > 900) digitalWrite(13, HIGH);
if (accelerationY < -900) digitalWrite(13, HIGH);
}