|
1 | 1 | /*
|
2 |
| - I2C communication protocol |
3 |
| - ADXL345 Arduino Uno/Nano |
| 2 | + I2C communication protocol |
| 3 | + ADXL345 Arduino Uno/Nano |
4 | 4 | GND GND
|
5 | 5 | Vin 3.3V
|
6 | 6 | SDA A4
|
7 | 7 | SDO A5
|
8 |
| -Reference: |
| 8 | + Reference: |
9 | 9 | http://codeyoung.blogspot.in/2009/11/adxl345-accelerometer-breakout-board.html
|
10 | 10 | http://morf.lv/modules.php?name=tutorials&lasit=31
|
11 | 11 | http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf
|
12 |
| - |
| 12 | +
|
13 | 13 | */
|
14 | 14 | #include<Wire.h>
|
15 | 15 |
|
16 | 16 | #define DEVICE (0x53) //Device Address
|
17 | 17 | const int numBytes = 6; //Number of bytes to receive(two bytes for each axis)
|
18 | 18 |
|
19 | 19 | byte values[6]; //To store the data received
|
20 |
| -int rawx,rawy,rawz; |
| 20 | +int rawx, rawy, rawz; |
21 | 21 |
|
22 |
| -typedef struct{ |
| 22 | +typedef struct { |
23 | 23 | double x;
|
24 | 24 | double y;
|
25 | 25 | double z;
|
26 |
| -}accel; |
27 |
| - |
| 26 | +} accel; |
28 | 27 | accel a;
|
29 | 28 |
|
30 |
| -void setup(){ |
| 29 | +void setup() { |
31 | 30 | Wire.begin();
|
32 | 31 | Serial.begin(9600);
|
33 | 32 |
|
34 | 33 | writeTo(DEVICE, 0x2D, 0);
|
35 | 34 | writeTo(DEVICE, 0x2D, 16);
|
36 | 35 | writeTo(DEVICE, 0x2D, 8);
|
37 | 36 | writeTo(DEVICE, 0x31, 11); // 11 for 13 bit resolution and ±16 g
|
| 37 | + init(60, 45, 127); |
38 | 38 | }
|
39 | 39 |
|
40 |
| -void loop(){ |
| 40 | +void loop() { |
41 | 41 | readFrom(DEVICE, 0x32, numBytes, values);
|
42 | 42 | rawx = (((int)values[1]) << 8) | values[0];
|
43 | 43 | rawy = (((int)values[3]) << 8) | values[2];
|
44 | 44 | rawz = (((int)values[5]) << 8) | values[4];
|
45 | 45 |
|
46 |
| -a.x = rawx*0.00390625; |
47 |
| -a.y = rawy*0.00390625; |
48 |
| -a.z = rawz*0.00390625; |
| 46 | + a.x = rawx * 0.00390625; |
| 47 | + a.y = rawy * 0.00390625; |
| 48 | + a.z = rawz * 0.00390625; |
49 | 49 |
|
50 |
| - Serial.print(a.x, 4); Serial.print(" "); |
| 50 | + Serial.print(a.x, 4); Serial.print(" "); |
51 | 51 | Serial.print(a.y, 4); Serial.print(" "); Serial.println(a.z, 4);
|
52 | 52 | delay(30);
|
53 | 53 | }
|
|
0 commit comments