Untitled

By Commodious Parakeet, 1 Month ago, written in php.
  1. // these constants won't change:
  2. const int xPin = 2;             // X output of the accelerometer
  3. const int yPin = 3;             // Y output of the accelerometer
  4.  
  5. void setup() {
  6.   // initialize serial communications:
  7.   Serial.begin(9600);
  8.   // initialize the pins connected to the accelerometer
  9.   // as inputs:
  10.   pinMode(xPin, INPUT);
  11.   pinMode(yPin, INPUT);
  12. }
  13.  
  14. void loop() {
  15.   // variables to read the pulse widths:
  16.   int pulseX, pulseY;
  17.   // variables to contain the resulting accelerations
  18.   int accelerationX, accelerationY;
  19.  
  20.   // read pulse from x- and y-axes:
  21.   pulseX = pulseIn(xPin,HIGH);  
  22.   pulseY = pulseIn(yPin,HIGH);
  23.  
  24.   // convert the pulse width into acceleration
  25.   // accelerationX and accelerationY are in milli-g's:
  26.   // earth's gravity is 1000 milli-g's, or 1g.
  27.   accelerationX = ((pulseX / 10) - 500) * 8;
  28.   accelerationY = ((pulseY / 10) - 500) * 8;
  29.  
  30.   // print the acceleration
  31.   Serial.print(accelerationX);
  32.   // print a tab character:
  33.   Serial.print("\t");
  34.   Serial.print(accelerationY);
  35.   Serial.println();
  36.  
  37.   delay(100);
  38. }
  39.