~ubuntu-branches/debian/experimental/arduino/experimental

« back to all changes in this revision

Viewing changes to build/shared/examples/6.Sensors/Memsic2125/Memsic2125.ino

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2012-03-11 18:19:42 UTC
  • mfrom: (1.1.5) (5.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20120311181942-be2clnbz1gcehixb
Tags: 1:1.0.1~rc1+dfsg-1
New upstream release, experimental.

Show diffs side-by-side

added added

removed removed

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