Go Down

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

spookyjack

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 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);

}


wildbill

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

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);

spookyjack

I tried PaulS code and it worked great ! Thanks folks ! This code worked perfect !  :smiley-mr-green: :smiley-mr-green: :smiley-mr-green: :smiley-mr-green:

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy