Skip to content
This repository was archived by the owner on Oct 4, 2021. It is now read-only.

Commit cdf0064

Browse files
committed
Reduced imu update time
1 parent e26c9b0 commit cdf0064

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

examples/PhysicsLabFirmware/PhysicsLabFirmware.ino

+33-26
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ const int RESISTANCE_AUX_PIN = 8;
5252
String name;
5353
unsigned long lastNotify = 0;
5454

55+
unsigned long imuTime;
56+
5557
#define RESISTOR_AUX_LOW 47000.0
5658
#define RESISTOR_AUX_HIGH 979.16 // 47k in parallel with 1k = 979.16 Ohm
5759

60+
#define IMU_UPDATE_TIME 100
61+
5862
//#define DEBUG //uncomment to debug the code :)
5963

6064
Adafruit_LSM9DS1 imu = Adafruit_LSM9DS1();
@@ -129,6 +133,7 @@ void setup() {
129133
BLE.addService(service);
130134

131135
BLE.advertise();
136+
imuTime = millis();
132137
}
133138

134139
void loop() {
@@ -256,35 +261,37 @@ int analogReadAverage(int pin, int numberOfSamples) {
256261
}
257262

258263
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+
}
259278

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];
275281

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+
}
281287

282-
if (magneticFieldCharacteristic.subscribed()) {
283-
float magneticField[3];
288+
if (magneticFieldCharacteristic.subscribed()) {
289+
float magneticField[3];
284290

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+
}
289296
}
290297
}

0 commit comments

Comments
 (0)