| 1 |
/* |
| 2 |
Memsic2125 |
| 3 |
|
| 4 |
Read the Memsic 2125 two-axis accelerometer. Converts the pulses output by the |
| 5 |
2125 into milli-g's (1/1000 of Earth's gravity) and prints them over the |
| 6 |
serial connection to the computer. |
| 7 |
|
| 8 |
The circuit: |
| 9 |
- X output of accelerometer to digital pin 2 |
| 10 |
- Y output of accelerometer to digital pin 3 |
| 11 |
- +V of accelerometer to +5V |
| 12 |
- GND of accelerometer to ground |
| 13 |
|
| 14 |
created 6 Nov 2008 |
| 15 |
by David A. Mellis |
| 16 |
modified 30 Aug 2011 |
| 17 |
by Tom Igoe |
| 18 |
|
| 19 |
This example code is in the public domain. |
| 20 |
|
| 21 |
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Memsic2125 |
| 22 |
*/ |
| 23 |
|
| 24 |
// these constants won't change: |
| 25 |
const int xPin = 2; // X output of the accelerometer |
| 26 |
const int yPin = 3; // Y output of the accelerometer |
| 27 |
|
| 28 |
void setup() { |
| 29 |
// initialize serial communications: |
| 30 |
Serial.begin(9600); |
| 31 |
// initialize the pins connected to the accelerometer as inputs: |
| 32 |
pinMode(xPin, INPUT); |
| 33 |
pinMode(yPin, INPUT); |
| 34 |
} |
| 35 |
|
| 36 |
void loop() { |
| 37 |
// variables to read the pulse widths: |
| 38 |
int pulseX, pulseY; |
| 39 |
// variables to contain the resulting accelerations |
| 40 |
int accelerationX, accelerationY; |
| 41 |
|
| 42 |
// read pulse from x- and y-axes: |
| 43 |
pulseX = pulseIn(xPin, HIGH); |
| 44 |
pulseY = pulseIn(yPin, HIGH); |
| 45 |
|
| 46 |
// convert the pulse width into acceleration |
| 47 |
// accelerationX and accelerationY are in milli-g's: |
| 48 |
// Earth's gravity is 1000 milli-g's, or 1 g. |
| 49 |
accelerationX = ((pulseX / 10) - 500) * 8; |
| 50 |
accelerationY = ((pulseY / 10) - 500) * 8; |
| 51 |
|
| 52 |
// print the acceleration |
| 53 |
Serial.print(accelerationX); |
| 54 |
// print a tab character: |
| 55 |
Serial.print("\t"); |
| 56 |
Serial.print(accelerationY); |
| 57 |
Serial.println(); |
| 58 |
|
| 59 |
delay(100); |
| 60 |
} |