SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bma423_tilt.ino
Go to the documentation of this file.
1
29#include <AccelerometerDrv.hpp>
30
31#ifndef SENSOR_SDA
32#define SENSOR_SDA 21
33#endif
34
35#ifndef SENSOR_SCL
36#define SENSOR_SCL 22
37#endif
38
39#ifndef SENSOR_IRQ
40#define SENSOR_IRQ 39
41#endif
42
44
45volatile bool isInterruptTriggered = false;
46
48{
49 Serial.println("Tilt detected");
50}
51
52void setup()
53{
54 bool rslt;
55
56 Serial.begin(115200);
57 while (!Serial);
58
59 pinMode(SENSOR_IRQ, INPUT);
60 attachInterrupt(SENSOR_IRQ, +[]() {
62 }, RISING);
63
64 // Sensor initialization
65 rslt = accelSensor.begin(Wire, BMA4XX_I2C_ADDR_SDO_HIGH, SENSOR_SDA, SENSOR_SCL);
66 if (!rslt) {
67 while (1) {
68 Serial.println("BMA4xx sensor initialization failed");
69 delay(1000);
70 }
71 }
72
73 Serial.print(accelSensor.getModelName());
74 Serial.println(" Sensor initialized successfully");
75
76 // Set sensor orientation, based on the hardware placement setting
78 if (!rslt) {
79 Serial.println("Failed to set remap axes");
80 }
81
82 // Accelerometer configuration
83 // The desired operation mode. Allowed values are SUSPEND, NORMAL.
85 // The desired full-scale range. Allowed values are FS_2G, FS_4G, FS_8G, FS_16G.
87 // The desired bandwidth. Allowed values are OSR4_AVG1, OSR2_AVG2, NORMAL_AVG4, etc.
89 // 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.
90 float data_rate_hz = 100.0f;
91 // The desired performance mode. Allowed values are CIC_AVG_MODE, CONTINUOUS_MODE
93
94 if (!accelSensor.configAccelerometer(operationMode, fullScaleRange, data_rate_hz, bandwidth, perfMode)) {
95 Serial.println("Failed to configure accelerometer");
96 while (1);
97 }
98
99 bool feature_enable = true; // Enable feature
100 bool interrupt_enable = true; //True: hardware interrupt enabled
102
104 pin_map, // Which pin should the hardware interrupt be routed to?
105 false, // level trigger
106 false, // active high
107 true, // output enable
108 false); // input disable
109 if (!rslt) {
110 Serial.println("Failed to set interrupt pin configuration");
111 while (1);
112 }
113
114
115 // This setting affects tilt detection. Allowed values are SMARTPHONE or WRISTBAND
117 accelSensor.selectPlatform(platform);
118
119 // @warning The correct axis must be set for proper tilt detection.
120 // Enable tilt detector feature
121 rslt = accelSensor.enableTiltDetector(feature_enable, interrupt_enable, pin_map);
122 if (!rslt) {
123 Serial.println("Failed to enable tilt detector");
124 while (1);
125 }
126
127 // Set tilt detection callback
129
130 delay(3000);
131
132 Serial.println("Now you can tilt the sensor.");
133}
134
135void loop()
136{
138 isInterruptTriggered = false;
140 }
141 delay(30);
142}
@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 setup()
#define SENSOR_SCL
volatile bool isInterruptTriggered
#define SENSOR_SDA
void onTiltDetected()
SensorBMA423 accelSensor
#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 selectPlatform(Platform platform)
Select the platform for the sensor.
Platform
Select the platform for the sensor.
void update() override
Update sensor state and process interrupts.
void setOnTiltDetectedCallback(std::function< void(void)> cb)
Set the callback for tilt detection events.
bool enableTiltDetector(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable or disable tilt 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.