Acc 03: Tilt measurement
This example shows how to get the acceleration on the different axis and calculate the tilt of Waspmote.

Required Materials

1 x Waspmote 1 x MiniUSB wire

Notes

This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------ [ACC_3] Waspmote Accelerometer tilt measurement --------
3
*
4
* Explanation: This example shows how to get the acceleration on the
5
* different axis and calculate the tilt of Waspmote
6
*
7
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
8
* http://www.libelium.com
9
*
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
14
*
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
19
*
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22
*
23
* Version: 3.0
24
* Design: David Gascón
25
* Implementation: Marcos Yarza
26
*/
27
28
void setup()
29
{
30
ACC.ON(FS_2G);
31
USB.ON(); // starts using the serial port
32
USB.println(F("ACC_03 example"));
33
}
34
35
void loop()
36
{
37
float x_acc = ACC.getX();
38
if (x_acc > 1000) x_acc = 1000;
39
if (x_acc < -1000) x_acc = -1000;
40
float angle_x = (180 / PI) * asin(x_acc/1000);
41
42
float y_acc = ACC.getY();
43
if (y_acc > 1000) y_acc = 1000;
44
if (y_acc < -1000) y_acc = -1000;
45
float angle_y = (180 / PI) * asin(y_acc/1000);
46
47
float z_acc = ACC.getZ();
48
if (z_acc > 1000) z_acc = 1000;
49
if (z_acc < -1000) z_acc = -1000;
50
float angle_z = (180 / PI) * asin(z_acc/1000);
51
52
USB.println(F("+++++++++++++++++++++++++++++++++"));
53
USB.print(F("Acceleration X: "));
54
USB.print(x_acc,DEC);
55
USB.print(F(" | X angle: "));
56
USB.println(angle_x);
57
58
USB.print(F("Acceleration Y: "));
59
USB.print(y_acc,DEC);
60
USB.print(F(" | Y angle: "));
61
USB.println(angle_y);
62
63
USB.print(F("Acceleration Z: "));
64
USB.print(z_acc,DEC);
65
USB.print(F(" | Z angle: "));
66
USB.println(angle_z);
67
68
delay(500);
69
}
Copied!

Output

1
H#
2
ACC_03 example
3
+++++++++++++++++++++++++++++++++
4
Acceleration X: -66 | X angle: -3.7842721939
5
Acceleration Y: -11 | Y angle: -0.6302662372
6
Acceleration Z: 970 | Z angle: 75.9301376342
7
+++++++++++++++++++++++++++++++++
8
Acceleration X: -67 | X angle: -3.8416948318
9
Acceleration Y: -25 | Y angle: -1.4325436592
10
Acceleration Z: 964 | Z angle: 74.5794219970
11
+++++++++++++++++++++++++++++++++
12
Acceleration X: 40 | X angle: 2.2924425601
13
Acceleration Y: -287 | Y angle: -16.6784343719
14
Acceleration Z: 932 | Z angle: 68.7487487792
15
+++++++++++++++++++++++++++++++++
16
Acceleration X: 22 | X angle: 1.2606086730
17
Acceleration Y: -523 | Y angle: -31.5336971282
18
Acceleration Z: 829 | Z angle: 55.9961433410
19
+++++++++++++++++++++++++++++++++
20
Acceleration X: -798 | X angle: -52.9395370483
21
Acceleration Y: 122 | Y angle: 7.0075411796
22
Acceleration Z: 540 | Z angle: 32.6836395263
Copied!
Last modified 1yr ago