updated example to match tutorial style guide

This commit is contained in:
Tom Igoe 2009-06-30 21:52:25 +00:00
parent 7958dc1818
commit c8ae5d9d20
1 changed files with 44 additions and 26 deletions

View File

@ -1,42 +1,60 @@
/* /*
* Memsic2125 Memsic2125
*
* Read the Memsic 2125 two-axis accelerometer. Converts the Read the Memsic 2125 two-axis accelerometer. Converts the
* pulses output by the 2125 into milli-g's (1/1000 of earth's pulses output by the 2125 into milli-g's (1/1000 of earth's
* gravity) and prints them over the serial connection to the gravity) and prints them over the serial connection to the
* computer. computer.
*
* http://www.arduino.cc/en/Tutorial/Memsic2125 The circuit:
* X output of accelerometer to digital pin 2
* Y output of accelerometer to digital pin 3
* +V of accelerometer to +5V
* GND of accelerometer to ground
http://www.arduino.cc/en/Tutorial/Memsic2125
created 6 Nov 2008
by David A. Mellis
modified 30 Jun 2009
by Tom Igoe
*/ */
int xpin = 2; // these constants won't change:
int ypin = 3; const int xPin = 2; // X output of the accelerometer
const int yPin = 3; // Y output of the accelerometer
void setup() void setup() {
{ // initialize serial communications:
Serial.begin(9600); Serial.begin(9600);
pinMode(xpin, INPUT); // initialize the pins connected to the accelerometer
pinMode(ypin, INPUT); // as inputs:
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
} }
void loop() void loop() {
{ // variables to read the pulse widths:
int pulseX, pulseY; int pulseX, pulseY;
int accX, accY; // variables to contain the resulting accelerations
int accelerationX, accelerationY;
// read pulse from x- and y-axes // read pulse from x- and y-axes:
pulseX = pulseIn(xpin,HIGH); pulseX = pulseIn(xPin,HIGH);
pulseY = pulseIn(ypin,HIGH); pulseY = pulseIn(yPin,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. // accelerationX and accelerationY are in milli-g's:
accX = ((pulseX / 10) - 500) * 8; // earth's gravity is 1000 milli-g's, or 1g.
accY = ((pulseY / 10) - 500) * 8; accelerationX = ((pulseX / 10) - 500) * 8;
accelerationY = ((pulseY / 10) - 500) * 8;
// print the acceleration // print the acceleration
Serial.print(accX); Serial.print(accelerationX);
Serial.print(" "); // print a tab character:
Serial.print(accY); Serial.print("\t");
Serial.print(accelerationY);
Serial.println(); Serial.println();
delay(100); delay(100);