@@ -52,9 +52,13 @@ const int RESISTANCE_AUX_PIN = 8;
52
52
String name;
53
53
unsigned long lastNotify = 0 ;
54
54
55
+ unsigned long imuTime;
56
+
55
57
#define RESISTOR_AUX_LOW 47000.0
56
58
#define RESISTOR_AUX_HIGH 979.16 // 47k in parallel with 1k = 979.16 Ohm
57
59
60
+ #define IMU_UPDATE_TIME 100
61
+
58
62
// #define DEBUG //uncomment to debug the code :)
59
63
60
64
Adafruit_LSM9DS1 imu = Adafruit_LSM9DS1();
@@ -129,6 +133,7 @@ void setup() {
129
133
BLE.addService (service);
130
134
131
135
BLE.advertise ();
136
+ imuTime = millis ();
132
137
}
133
138
134
139
void loop () {
@@ -256,35 +261,37 @@ int analogReadAverage(int pin, int numberOfSamples) {
256
261
}
257
262
258
263
void updateSubscribedIMUCharacteristics () {
264
+ if (millis () - imuTime > IMU_UPDATE_TIME) {
265
+ imuTime = millis ();
266
+ imu.read ();
267
+ sensors_event_t a, m, g, temp;
268
+ imu.getEvent (&a, &m, &g, &temp);
269
+
270
+ if (accelerationCharacteristic.subscribed ()) {
271
+ float acceleration[3 ];
272
+
273
+ acceleration[0 ] = a.acceleration .x ;
274
+ acceleration[1 ] = a.acceleration .y ;
275
+ acceleration[2 ] = a.acceleration .z ;
276
+ accelerationCharacteristic.writeValue ((byte*)acceleration, sizeof (acceleration));
277
+ }
259
278
260
- imu.read ();
261
- sensors_event_t a, m, g, temp;
262
- imu.getEvent (&a, &m, &g, &temp);
263
-
264
- if (accelerationCharacteristic.subscribed ()) {
265
- float acceleration[3 ];
266
-
267
- acceleration[0 ] = a.acceleration .x ;
268
- acceleration[1 ] = a.acceleration .y ;
269
- acceleration[2 ] = a.acceleration .z ;
270
- accelerationCharacteristic.writeValue ((byte*)acceleration, sizeof (acceleration));
271
- }
272
-
273
- if (gyroscopeCharacteristic.subscribed ()) {
274
- float gyroscope[3 ];
279
+ if (gyroscopeCharacteristic.subscribed ()) {
280
+ float gyroscope[3 ];
275
281
276
- gyroscope[0 ] = g.gyro .x ;
277
- gyroscope[1 ] = g.gyro .y ;
278
- gyroscope[2 ] = g.gyro .z ;
279
- gyroscopeCharacteristic.writeValue ((byte*)gyroscope, sizeof (gyroscope));
280
- }
282
+ gyroscope[0 ] = g.gyro .x ;
283
+ gyroscope[1 ] = g.gyro .y ;
284
+ gyroscope[2 ] = g.gyro .z ;
285
+ gyroscopeCharacteristic.writeValue ((byte*)gyroscope, sizeof (gyroscope));
286
+ }
281
287
282
- if (magneticFieldCharacteristic.subscribed ()) {
283
- float magneticField[3 ];
288
+ if (magneticFieldCharacteristic.subscribed ()) {
289
+ float magneticField[3 ];
284
290
285
- magneticField[0 ] = m.magnetic .x ;
286
- magneticField[1 ] = m.magnetic .y ;
287
- magneticField[2 ] = m.magnetic .z ;
288
- magneticFieldCharacteristic.writeValue ((byte*)magneticField, sizeof (magneticField));
291
+ magneticField[0 ] = m.magnetic .x ;
292
+ magneticField[1 ] = m.magnetic .y ;
293
+ magneticField[2 ] = m.magnetic .z ;
294
+ magneticFieldCharacteristic.writeValue ((byte*)magneticField, sizeof (magneticField));
295
+ }
289
296
}
290
297
}
0 commit comments