Updating Memsic2125 example.

This commit is contained in:
David A. Mellis 2008-11-08 21:58:24 +00:00
parent b2b32eb4f3
commit 68392f471c
1 changed files with 19 additions and 18 deletions

View File

@ -1,16 +1,22 @@
// Memsic2125 /*
* Memsic2125
*
* Read the Memsic 2125 two-axis accelerometer. Converts the
* pulses output by the 2125 into milli-g's (1/1000 of earth's
* gravity) and prints them over the serial connection to the
* computer.
*
* http://www.arduino.cc/en/Tutorial/Memsic2125
*/
#define X 2 int xpin = 2;
#define Y 3 int ypin = 3;
int dx = 0;
int dy = 0;
void setup() void setup()
{ {
Serial.begin(9600); Serial.begin(9600);
pinMode(X, INPUT); pinMode(xpin, INPUT);
pinMode(Y, INPUT); pinMode(ypin, INPUT);
} }
void loop() void loop()
@ -18,21 +24,16 @@ void loop()
int pulseX, pulseY; int pulseX, pulseY;
int accX, accY; int accX, accY;
// wait for previous pulse to end // read pulse from x- and y-axes
while (digitalRead(X) == HIGH) {} pulseX = pulseIn(xpin,HIGH);
// read pulse from x-axis pulseY = pulseIn(ypin,HIGH);
pulseX = pulseIn(X,HIGH);
// wait for previous pulse to end
while (digitalRead(Y) == HIGH) {}
// read pulse from y-axis
pulseY = pulseIn(Y,HIGH);
// convert the pulse width into acceleration // convert the pulse width into acceleration
// accX and accY are in milli-g's: earth's gravity is 1000. // accX and accY are in milli-g's: earth's gravity is 1000.
accX = ((pulseX / 10) - 500) * 8; accX = ((pulseX / 10) - 500) * 8;
accY = ((pulseY / 10) - 500) * 8; accY = ((pulseY / 10) - 500) * 8;
// print the acceleration
Serial.print(accX); Serial.print(accX);
Serial.print(" "); Serial.print(" ");
Serial.print(accY); Serial.print(accY);