17
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
*/
19
19
20
- #include < ArduinoBLE.h>
21
- #include < MKRIMU.h>
20
+ #include < ArduinoBLE.h> // click here to install the library: http://librarymanager#ArduinoBLE
21
+ #include < Adafruit_LSM9DS1.h> // click here to install the library: http://librarymanager#Adafruit&LSM9DS1
22
+ #include < Adafruit_Sensor.h> // click here to install the library: http://librarymanager#Adafruit&unified&Sensor&abstraction
22
23
23
24
#include " INA226.h"
24
25
@@ -46,16 +47,22 @@ const int INPUT3_PIN = A0;
46
47
const int OUTPUT1_PIN = 5 ;
47
48
const int OUTPUT2_PIN = 1 ;
48
49
const int RESISTANCE_PIN = A2;
49
- const int RESISTANCE_AUX_PIN = 13 ;
50
+ const int RESISTANCE_AUX_PIN = 8 ;
50
51
51
52
String name;
52
53
unsigned long lastNotify = 0 ;
53
54
55
+ unsigned long imuTime;
56
+
54
57
#define RESISTOR_AUX_LOW 47000.0
55
58
#define RESISTOR_AUX_HIGH 979.16 // 47k in parallel with 1k = 979.16 Ohm
56
59
60
+ #define IMU_UPDATE_TIME 50
61
+
57
62
// #define DEBUG //uncomment to debug the code :)
58
63
64
+ Adafruit_LSM9DS1 imu = Adafruit_LSM9DS1();
65
+
59
66
void setup () {
60
67
Serial.begin (9600 );
61
68
#ifdef DEBUG
@@ -79,12 +86,16 @@ void setup() {
79
86
while (1 );
80
87
}
81
88
82
- if (!IMU .begin ()) {
89
+ if (!imu .begin ()) {
83
90
Serial.println (" Failled to initialized IMU!" );
84
91
85
92
while (1 );
86
93
}
87
94
95
+ imu.setupAccel (imu.LSM9DS1_ACCELRANGE_2G );
96
+ imu.setupMag (imu.LSM9DS1_MAGGAIN_4GAUSS );
97
+ imu.setupGyro (imu.LSM9DS1_GYROSCALE_245DPS );
98
+
88
99
if (!BLE.begin ()) {
89
100
Serial.println (" Failled to initialized BLE!" );
90
101
@@ -122,6 +133,7 @@ void setup() {
122
133
BLE.addService (service);
123
134
124
135
BLE.advertise ();
136
+ imuTime = millis ();
125
137
}
126
138
127
139
void loop () {
@@ -186,7 +198,7 @@ void updateSubscribedCharacteristics() {
186
198
187
199
digitalWrite (RESISTANCE_AUX_PIN, LOW);
188
200
Vout = getVoutAverage ();
189
- if (Vout >= 0.1 ) {
201
+ if (( Vout >= 0.1 ) && (Vout <= 3.0 ) ) {
190
202
resistanceAuxLow = RESISTOR_AUX_LOW * ((3.3 / Vout) - 1 );
191
203
}
192
204
@@ -213,7 +225,8 @@ void updateSubscribedCharacteristics() {
213
225
} else if ((resistanceAuxHigh == INFINITY) && (resistanceAuxLow != INFINITY)) {
214
226
resistanceAvg = resistanceAuxLow;
215
227
}
216
-
228
+ resistanceAvg += 0.025 * resistanceAvg;
229
+
217
230
#ifdef DEBUG
218
231
Serial.print (" Resistance (AVG): " );
219
232
Serial.print (resistanceAvg);
@@ -248,26 +261,36 @@ int analogReadAverage(int pin, int numberOfSamples) {
248
261
}
249
262
250
263
void updateSubscribedIMUCharacteristics () {
251
- if (accelerationCharacteristic.subscribed ()) {
252
- float acceleration[3 ];
253
-
254
- if (IMU.accelerationAvailable () && IMU.readAcceleration (acceleration[0 ], acceleration[1 ], acceleration[2 ])) {
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 ;
255
276
accelerationCharacteristic.writeValue ((byte*)acceleration, sizeof (acceleration));
256
277
}
257
- }
258
278
259
- if (gyroscopeCharacteristic.subscribed ()) {
260
- float gyroscope[3 ];
279
+ if (gyroscopeCharacteristic.subscribed ()) {
280
+ float gyroscope[3 ];
261
281
262
- if (IMU.gyroscopeAvailable () && IMU.readGyroscope (gyroscope[0 ], gyroscope[1 ], gyroscope[2 ])) {
282
+ gyroscope[0 ] = g.gyro .x ;
283
+ gyroscope[1 ] = g.gyro .y ;
284
+ gyroscope[2 ] = g.gyro .z ;
263
285
gyroscopeCharacteristic.writeValue ((byte*)gyroscope, sizeof (gyroscope));
264
286
}
265
- }
266
287
267
- if (magneticFieldCharacteristic.subscribed ()) {
268
- float magneticField[3 ];
288
+ if (magneticFieldCharacteristic.subscribed ()) {
289
+ float magneticField[3 ];
269
290
270
- if (IMU.magneticFieldAvailable () && IMU.readMagneticField (magneticField[0 ], magneticField[1 ], magneticField[2 ])) {
291
+ magneticField[0 ] = m.magnetic .x ;
292
+ magneticField[1 ] = m.magnetic .y ;
293
+ magneticField[2 ] = m.magnetic .z ;
271
294
magneticFieldCharacteristic.writeValue ((byte*)magneticField, sizeof (magneticField));
272
295
}
273
296
}
0 commit comments