1717 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1818*/
1919
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
2223
2324#include " INA226.h"
2425
@@ -46,16 +47,22 @@ const int INPUT3_PIN = A0;
4647const int OUTPUT1_PIN = 5 ;
4748const int OUTPUT2_PIN = 1 ;
4849const int RESISTANCE_PIN = A2;
49- const int RESISTANCE_AUX_PIN = 13 ;
50+ const int RESISTANCE_AUX_PIN = 8 ;
5051
5152String name;
5253unsigned long lastNotify = 0 ;
5354
55+ unsigned long imuTime;
56+
5457#define RESISTOR_AUX_LOW 47000.0
5558#define RESISTOR_AUX_HIGH 979.16 // 47k in parallel with 1k = 979.16 Ohm
5659
60+ #define IMU_UPDATE_TIME 50
61+
5762// #define DEBUG //uncomment to debug the code :)
5863
64+ Adafruit_LSM9DS1 imu = Adafruit_LSM9DS1();
65+
5966void setup () {
6067 Serial.begin (9600 );
6168#ifdef DEBUG
@@ -79,12 +86,16 @@ void setup() {
7986 while (1 );
8087 }
8188
82- if (!IMU .begin ()) {
89+ if (!imu .begin ()) {
8390 Serial.println (" Failled to initialized IMU!" );
8491
8592 while (1 );
8693 }
8794
95+ imu.setupAccel (imu.LSM9DS1_ACCELRANGE_2G );
96+ imu.setupMag (imu.LSM9DS1_MAGGAIN_4GAUSS );
97+ imu.setupGyro (imu.LSM9DS1_GYROSCALE_245DPS );
98+
8899 if (!BLE.begin ()) {
89100 Serial.println (" Failled to initialized BLE!" );
90101
@@ -122,6 +133,7 @@ void setup() {
122133 BLE.addService (service);
123134
124135 BLE.advertise ();
136+ imuTime = millis ();
125137}
126138
127139void loop () {
@@ -186,7 +198,7 @@ void updateSubscribedCharacteristics() {
186198
187199 digitalWrite (RESISTANCE_AUX_PIN, LOW);
188200 Vout = getVoutAverage ();
189- if (Vout >= 0.1 ) {
201+ if (( Vout >= 0.1 ) && (Vout <= 3.0 ) ) {
190202 resistanceAuxLow = RESISTOR_AUX_LOW * ((3.3 / Vout) - 1 );
191203 }
192204
@@ -213,7 +225,8 @@ void updateSubscribedCharacteristics() {
213225 } else if ((resistanceAuxHigh == INFINITY) && (resistanceAuxLow != INFINITY)) {
214226 resistanceAvg = resistanceAuxLow;
215227 }
216-
228+ resistanceAvg += 0.025 * resistanceAvg;
229+
217230#ifdef DEBUG
218231 Serial.print (" Resistance (AVG): " );
219232 Serial.print (resistanceAvg);
@@ -248,26 +261,36 @@ int analogReadAverage(int pin, int numberOfSamples) {
248261}
249262
250263void 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 ;
255276 accelerationCharacteristic.writeValue ((byte*)acceleration, sizeof (acceleration));
256277 }
257- }
258278
259- if (gyroscopeCharacteristic.subscribed ()) {
260- float gyroscope[3 ];
279+ if (gyroscopeCharacteristic.subscribed ()) {
280+ float gyroscope[3 ];
261281
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 ;
263285 gyroscopeCharacteristic.writeValue ((byte*)gyroscope, sizeof (gyroscope));
264286 }
265- }
266287
267- if (magneticFieldCharacteristic.subscribed ()) {
268- float magneticField[3 ];
288+ if (magneticFieldCharacteristic.subscribed ()) {
289+ float magneticField[3 ];
269290
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 ;
271294 magneticFieldCharacteristic.writeValue ((byte*)magneticField, sizeof (magneticField));
272295 }
273296 }
0 commit comments