SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bma456h_any_motion.ino
Go to the documentation of this file.
1
30#include <AccelerometerDrv.hpp>
31
32#ifndef SENSOR_SDA
33#define SENSOR_SDA 21
34#endif
35
36#ifndef SENSOR_SCL
37#define SENSOR_SCL 22
38#endif
39
40#ifndef SENSOR_IRQ
41#define SENSOR_IRQ 39
42#endif
43
44// Configuring using BMA456H features
46
47volatile bool isInterruptTriggered = false;
48
50{
51 Serial.println("Any motion detected");
52}
53
54void setup()
55{
56 bool rslt;
57
58 Serial.begin(115200);
59 while (!Serial);
60
61 pinMode(SENSOR_IRQ, INPUT);
62 attachInterrupt(SENSOR_IRQ, +[]() {
64 }, RISING);
65
66 // Sensor initialization
67 rslt = accelSensor.begin(Wire, BMA4XX_I2C_ADDR_SDO_HIGH, SENSOR_SDA, SENSOR_SCL);
68 if (!rslt) {
69 while (1) {
70 Serial.println("BMA4xx sensor initialization failed");
71 delay(1000);
72 }
73 }
74
75 Serial.print(accelSensor.getModelName());
76 Serial.println(" Sensor initialized successfully");
77
78 // Set sensor orientation, based on the hardware placement setting
80 if (!rslt) {
81 Serial.println("Failed to set remap axes");
82 }
83
84 // Accelerometer configuration
85 // The desired operation mode. Allowed values are SUSPEND, NORMAL.
87 // The desired full-scale range. Allowed values are FS_2G, FS_4G, FS_8G, FS_16G.
89 // The desired bandwidth. Allowed values are OSR4_AVG1, OSR2_AVG2, NORMAL_AVG4, etc.
91 // The desired data rate in Hz. Allowed values are 0.78, 1.56, 3.12, 6.25, 12.5, 25, 50, 100, 200, 400, 800, 1600.
92 float data_rate_hz = 50.0f;
93 // The desired performance mode. Allowed values are CIC_AVG_MODE, CONTINUOUS_MODE
95
96 if (!accelSensor.configAccelerometer(operationMode, fullScaleRange, data_rate_hz, bandwidth, perfMode)) {
97 Serial.println("Failed to configure accelerometer");
98 while (1);
99 }
100
101 bool feature_enable = true; // Enable feature
102 bool interrupt_enable = true; //True: hardware interrupt enabled
104
106 pin_map, // Which pin should the hardware interrupt be routed to?
107 false, // level trigger
108 false, // active high
109 true, // output enable
110 false); // input disable
111 if (!rslt) {
112 Serial.println("Failed to set interrupt pin configuration");
113 while (1);
114 }
115
116 /*
117 * Set the slope threshold:
118 * Interrupt will be generated if the slope of all the axis exceeds the threshold (1 bit = 0.48mG)
119 */
120 uint16_t threshold = 10;
121
122 /*
123 * Set the duration for any-motion interrupt:
124 * Duration defines the number of consecutive data points for which threshold condition must be true(1 bit = 20ms)
125 */
126 uint16_t duration = 4;
127
128 if (!accelSensor.configAnyMotion( threshold, duration)) {
129 Serial.println("Failed to configure any-motion detection");
130 }
131
132 // Motion axes configuration
134 cfg.x_axis = 1;
135 cfg.y_axis = 1;
136 cfg.z_axis = 1;
137
138 // Enable any-motion detection feature
139 rslt = accelSensor.enableAnyMotionDetection(cfg, feature_enable, interrupt_enable, pin_map);
140 if (!rslt) {
141 Serial.println("Failed to enable any-motion detection");
142 while (1);
143 }
144
145 // Set any-motion detection callback
147
148 delay(3000);
149
150 Serial.println("Now you can move the sensor.");
151}
152
153void loop()
154{
156 isInterruptTriggered = false;
158 }
159 delay(30);
160}
@license MIT License
OperationMode
Enumeration of sensor operation modes.
@ TOP_LAYER_RIGHT_CORNER
AccelFullScaleRange
Enumeration of accelerometer full-scale range settings.
InterruptPinMap
Enumeration of interrupt pin mapping options.
@ PIN1
Interrupt pin 1.
void onAnyMotionDetected()
SensorBMA456H accelSensor
void setup()
#define SENSOR_SCL
volatile bool isInterruptTriggered
#define SENSOR_SDA
#define SENSOR_IRQ
void loop()
bool begin(CommInterface interface, SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback, uint8_t addr=0)
Initialize the sensor using custom callback interface.
bool configAnyMotion(uint16_t threshold, uint16_t duration) override
Configure any-motion detection parameters with detailed unit conversion.
void update() override
Update sensor state and process interrupts.
void setOnAnyMotionCallback(std::function< void(void)> cb)
Set the any-motion callback for motion events.
bool enableAnyMotionDetection(const MotionAxesConfig &cfg, bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable or disable any-motion detection.
virtual bool setRemapAxes(SensorRemap remap)
Set the remapping of axes for the BMA4XX sensor.
bool setInterruptPinConfig(InterruptPinMap pin_map, bool edge_trigger=false, bool active_low=true, bool output_en=true, bool input_en=false)
Configure the electrical properties of an interrupt pin.
const char * getModelName() const
Get the model name of the BMA4XX sensor.
bool configAccelerometer(OperationMode mode, AccelFullScaleRange range, float data_rate_hz, AccelBandwidth bandwidth, AccelPerfMode perf_mode)
Configure the accelerometer.
AccelBandwidth
BMA4 Accelerometer averaging filter configuration These values configure the internal averaging filte...
@ OSR2_AVG2
OSR2 mode with 2 samples averaged.
AccelPerfMode
BMA4 Accelerometer performance mode configuration Configures the internal filter behavior for sample ...
@ CIC_AVG_MODE
Averaging filter mode: samples are averaged based on configured bandwidth and ODR.
uint8_t z_axis
Enable motion detection on Z-axis (1=enabled, 0=disabled)
uint8_t y_axis
Enable motion detection on Y-axis (1=enabled, 0=disabled)
uint8_t x_axis
Enable motion detection on X-axis (1=enabled, 0=disabled)