Skip to content

Commit a53d95d

Browse files
aliphysmcmchris
andauthored
Expand CI coverage to four examples from the Nicla Sense ME User Manual (#139)
* add ActivityRecognition sketch * add magnetometer example * Add three examples * Spelling corrections * Update Arduino_BHY2/examples/Pressure/Pressure.ino Co-authored-by: Christopher Méndez <[email protected]> --------- Co-authored-by: Christopher Méndez <[email protected]>
1 parent 4120b1f commit a53d95d

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed

examples/AccelGyro/AccelGyro.ino

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* This example shows how to use the access the IMU data and plot it in real time.
3+
*
4+
* Every 50 milliseconds, this sketch will send the x,y and z accelerometer (SensorID 4) and
5+
* gyroscope (SensorID 22) values over serial from the BHI260AP six axis IMU.
6+
* These six values are visually displayed with the Serial Plotter in the Arduino IDE.
7+
*
8+
* Instructions:
9+
* 1. Upload this sketch to your Nicla Sense ME board.
10+
* 2. Open the Serial Plotter at a baud rate of 115200.
11+
* 3. The three axis values for both the accelerometer and gyroscope will be printed to the Serial Plotter.
12+
* 4. Optionally, you can change the visibility of each data by clicking on the legend.
13+
*
14+
* Initial author: @mcmchris
15+
*/
16+
17+
#include "Arduino_BHY2.h"
18+
19+
SensorXYZ accel(SENSOR_ID_ACC);
20+
SensorXYZ gyro(SENSOR_ID_GYRO);
21+
22+
23+
void setup() {
24+
Serial.begin(115200);
25+
BHY2.begin();
26+
accel.begin();
27+
gyro.begin();
28+
}
29+
30+
void loop() {
31+
static auto printTime = millis();
32+
33+
// Update function should be continuously polled
34+
BHY2.update();
35+
36+
if (millis() - printTime >= 50) {
37+
printTime = millis();
38+
39+
// Accelerometer values
40+
Serial.print("acc_X:");
41+
Serial.print(accel.x());
42+
Serial.print(",");
43+
Serial.print("acc_Y:");
44+
Serial.print(accel.y());
45+
Serial.print(",");
46+
Serial.print("acc_Z:");
47+
Serial.print(accel.z());
48+
Serial.print(",");
49+
50+
// Gyroscope values
51+
Serial.print("gyro_X:");
52+
Serial.print(gyro.x());
53+
Serial.print(",");
54+
Serial.print("gyro_Y:");
55+
Serial.print(gyro.y());
56+
Serial.print(",");
57+
Serial.print("gyro_Z:");
58+
Serial.println(gyro.z());
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This example shows how to use the Nicla Sense ME library to detect activity and send it over serial.
3+
*
4+
* Every 1 second, this sketch will send the latest activity detected via the BHI260AP sensor to the serial port.
5+
* Without any additional training, it is possible to detect Still, Walking, Running and Tilting activities
6+
* A full description of supported activities is available in Table 93 of the BHI260AP datasheet.
7+
*
8+
* Instructions:
9+
* 1. Upload this sketch to your Nicla Sense ME board.
10+
* 2. Open the Serial Monitor at a baud rate of 115200.
11+
* 3. Observe the detected activity, by moving the Nicla Sense ME board.
12+
*
13+
* Initial author: @mcmchris
14+
*/
15+
16+
#include "Arduino_BHY2.h"
17+
18+
SensorActivity active(SENSOR_ID_AR);
19+
20+
unsigned long previousMillis = 0; // will store last time the sensor was updated
21+
const long interval = 1000;
22+
23+
24+
void setup() {
25+
Serial.begin(115200);
26+
BHY2.begin();
27+
active.begin();
28+
}
29+
30+
void loop() {
31+
BHY2.update();
32+
unsigned long currentMillis = millis();
33+
if (currentMillis - previousMillis >= interval) {
34+
previousMillis = currentMillis;
35+
Serial.println(String("Activity info: ") + active.toString());
36+
}
37+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This example shows how to use the access the magnetometer data and send it over serial.
3+
*
4+
* Every 1 second, this sketch will send the heading of the magnetometer over serial.
5+
* by calculating the arctangent between the x and y axis and multiplied in a conversion
6+
* factor to convert the headings from radians to degrees.
7+
*
8+
* Instructions:
9+
* 1. Upload this sketch to your Nicla Sense ME board.
10+
* 2. Open the Serial Monitor at a baud rate of 115200.
11+
* 3. The heading of the magnetometer will be printed to the serial monitor.
12+
*
13+
* Initial author: @mcmchris
14+
*/
15+
16+
#include "Arduino_BHY2.h"
17+
#include "Math.h"
18+
19+
SensorXYZ magnetometer(SENSOR_ID_MAG);
20+
21+
float heading = 0;
22+
unsigned long previousMillis = 0; // will store last time the sensor was updated
23+
const long interval = 1000;
24+
25+
void setup() {
26+
Serial.begin(115200);
27+
BHY2.begin();
28+
magnetometer.begin();
29+
}
30+
31+
void loop() {
32+
BHY2.update();
33+
unsigned long currentMillis = millis();
34+
if (currentMillis - previousMillis >= interval) {
35+
previousMillis = currentMillis;
36+
heading = round(atan2(magnetometer.x(), magnetometer.y()) * 180.0 / PI);
37+
Serial.println(String(heading) + "º");
38+
}
39+
}

examples/Pressure/Pressure.ino

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This example shows how to use the access the pressure data and send it over serial.
3+
*
4+
* Every 1 second, this sketch will send the pressure in hPa over serial.
5+
* SensorID 129 (SENSOR_ID_BARO) is read with the Sensor class from the BMP390.
6+
*
7+
* Instructions:
8+
* 1. Upload this sketch to your Nicla Sense ME board.
9+
* 2. Open the Serial Monitor at a baud rate of 115200.
10+
* 3. The pressure will be printed to the serial monitor.
11+
*
12+
* Initial author: @mcmchris
13+
*/
14+
15+
#include "Arduino_BHY2.h"
16+
17+
18+
unsigned long previousMillis = 0; // will store last time the sensor was updated
19+
const long interval = 1000;
20+
21+
Sensor pressure(SENSOR_ID_BARO);
22+
23+
void setup() {
24+
Serial.begin(9600);
25+
BHY2.begin();
26+
pressure.begin();
27+
}
28+
29+
void loop() {
30+
BHY2.update();
31+
unsigned long currentMillis = millis();
32+
if (currentMillis - previousMillis >= interval) {
33+
previousMillis = currentMillis;
34+
Serial.println(String(pressure.value()) + " hPa");
35+
}
36+
}

0 commit comments

Comments
 (0)