From 1dcfb7e67ba8b934553841ff5152d91872502c33 Mon Sep 17 00:00:00 2001 From: lobodol Date: Tue, 15 May 2018 09:41:58 +0200 Subject: [PATCH 1/2] Don't wait for Wire.available after Wire.requestFrom --- imu.ino | 3 --- 1 file changed, 3 deletions(-) diff --git a/imu.ino b/imu.ino index 1c5e8bc..3051b8a 100644 --- a/imu.ino +++ b/imu.ino @@ -135,9 +135,6 @@ void readSensor() { Wire.endTransmission(); // End the transmission Wire.requestFrom(MPU_ADDRESS, 14); // Request 14 bytes from the MPU-6050 - // Wait until all the bytes are received - while (Wire.available() < 14); - acc_raw[X] = Wire.read() << 8 | Wire.read(); // Add the low and high byte to the acc_raw[X] variable acc_raw[Y] = Wire.read() << 8 | Wire.read(); // Add the low and high byte to the acc_raw[Y] variable acc_raw[Z] = Wire.read() << 8 | Wire.read(); // Add the low and high byte to the acc_raw[Z] variable From da3344d32979f40b274be5bd40c0434f5ef0ce3c Mon Sep 17 00:00:00 2001 From: lobodol Date: Tue, 15 May 2018 09:48:55 +0200 Subject: [PATCH 2/2] Add conditional statement to handle Wire error --- imu.ino | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/imu.ino b/imu.ino index 3051b8a..0d8d273 100644 --- a/imu.ino +++ b/imu.ino @@ -135,6 +135,10 @@ void readSensor() { Wire.endTransmission(); // End the transmission Wire.requestFrom(MPU_ADDRESS, 14); // Request 14 bytes from the MPU-6050 + if (Wire.available() == 0) { + // That means MPU is not connected + } + acc_raw[X] = Wire.read() << 8 | Wire.read(); // Add the low and high byte to the acc_raw[X] variable acc_raw[Y] = Wire.read() << 8 | Wire.read(); // Add the low and high byte to the acc_raw[Y] variable acc_raw[Z] = Wire.read() << 8 | Wire.read(); // Add the low and high byte to the acc_raw[Z] variable