Pages: [1]   Go Down
Author Topic: Memsic 2125 Programming help !!!  (Read 542 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 88
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

}

Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 72
Posts: 3763
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Replace this:
Code:
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:
digitalWrite(13, (abs(accelerationX )>900) || (abs(accelerationY )>900) );
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Replace this:...with this:
Or keep, but fix, the readable code:
Code:
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);
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 88
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: