Go Down

### Topic: Memsic 2125 Programming help !!! (Read 756 times)previous topic - next topic

#### spookyjack123

##### Aug 22, 2011, 02:28 am
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 !

Code: [Select]
`const int xPin = 2; // X output of the accelerometerconst int yPin = 3; // Y output of the accelerometervoid 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);}`

#### wildbill

#1
##### Aug 22, 2011, 08:48 am
Replace this:
Code: [Select]
`if (accelerationX > 900) digitalWrite(13, HIGH);  if (accelerationX < -900) digitalWrite(13, HIGH);if (accelerationY > 900) digitalWrite(13, HIGH);  if (accelerationY < -900) digitalWrite(13, HIGH);`

with this:
Code: [Select]
`digitalWrite(13, (abs(accelerationX )>900) || (abs(accelerationY )>900) );`

#### PaulS

#2
##### Aug 22, 2011, 11:49 am
Quote
Replace this:...with this:

Or keep, but fix, the readable code:
Code: [Select]
`if (accelerationX > 900) digitalWrite(13, HIGH);  else if (accelerationX < -900) digitalWrite(13, HIGH);else if (accelerationY > 900) digitalWrite(13, HIGH);  else if (accelerationY < -900) digitalWrite(13, HIGH);else digitalWrite(13, LOW);`