Skip to content

Commit 6557caf

Browse files
committed
Add accelerometer orientation example
1 parent a50c6b6 commit 6557caf

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
Copyright (c) 2015 Intel Corporation. All rights reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
18+
*/
19+
20+
/*
21+
This sketch example demonstrates how the BMI160 on the
22+
Intel(R) Curie(TM) module can be used to read accelerometer data
23+
and translate it to an orientation
24+
*/
25+
26+
#include "CurieImu.h"
27+
28+
int lastOrientation = - 1; // previous orientation (for comparison)
29+
30+
void setup() {
31+
Serial.begin(9600); // initialize Serial communication
32+
while (!Serial); // wait for the serial port to open
33+
34+
// initialize device
35+
Serial.println("Initializing IMU device...");
36+
CurieIMU.begin();
37+
38+
// Set the accelerometer range to 2G
39+
CurieIMU.setAccelerometerRange(CURIE_IMU_ACCELEROMETER_RANGE_2G);
40+
}
41+
42+
void loop() {
43+
int orientation = - 1; // the board's orientation
44+
String orientationString; // string for printing description of orientation
45+
/*
46+
The orientations of the board:
47+
0: flat, processor facing up
48+
1: flat, processor facing down
49+
2: landscape, analog pins down
50+
3: landscape, analog pins up
51+
4: portrait, USB connector up
52+
5: portrait, USB connector down
53+
*/
54+
// read accelerometer:
55+
short x = CurieIMU.readAccelerometer(X_AXIS);
56+
short y = CurieIMU.readAccelerometer(Y_AXIS);
57+
short z = CurieIMU.readAccelerometer(Z_AXIS);
58+
59+
// calculate the absolute values, to determine the largest
60+
int absX = abs(x);
61+
int absY = abs(y);
62+
int absZ = abs(z);
63+
64+
if ( (absZ > absX) && (absZ > absY)) {
65+
// base orientation on Z
66+
if (z > 0) {
67+
orientationString = "up";
68+
orientation = 0;
69+
} else {
70+
orientationString = "down";
71+
orientation = 1;
72+
}
73+
} else if ( (absY > absX) && (absY > absZ)) {
74+
// base orientation on Y
75+
if (y > 0) {
76+
orientationString = "digital pins up";
77+
orientation = 2;
78+
} else {
79+
orientationString = "analog pins up";
80+
orientation = 3;
81+
}
82+
} else {
83+
// base orientation on X
84+
if (x < 0) {
85+
orientationString = "connector up";
86+
orientation = 4;
87+
} else {
88+
orientationString = "connector up";
89+
orientation = 4;
90+
}
91+
}
92+
93+
// if the orientation has changed, print out a description:
94+
if (orientation != lastOrientation) {
95+
Serial.println(orientationString);
96+
lastOrientation = orientation;
97+
}
98+
}
99+
100+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy