Hi all, I am using a Ardunio nano, MPU5060 and a HC-05 bluetooth module to connect my Arduino project to my android phone via the Blynk app. The Idea is to eventually send data to the Blynk app and plot a graph.
Initially I spit the code into two sections, the first being to test the MPU and the second to test the connectivity between the HC-05 and the Blynk app. Individually they both work well, the MPU provides constant gyro data and the Bluetooth module allows me to activate D13 LED pin from the Blynk app.
Now the issue comes when I combine these two pieces of code. It starts of fine but within a few second everything becomes unresponsive and freezes. I am then unable to see the gyro data in the serial monitor and I am no longer able to control the D13 LED. Please see below combined code.
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
MPU6050 mpu;
#define OUTPUT_READABLE_YAWPITCHROLL
#define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
bool blinkState = false;
// MPU control/status vars
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer
// orientation/motion vars
Quaternion q; // [w, x, y, z] quaternion container
VectorInt16 aa; // [x, y, z] accel sensor measurements
VectorInt16 aaReal; // [x, y, z] gravity-free accel sensor measurements
VectorInt16 aaWorld; // [x, y, z] world-frame accel sensor measurements
VectorFloat gravity; // [x, y, z] gravity vector
float euler[3]; // [psi, theta, phi] Euler angle container
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll container and gravity vector
//Calibration
float RollCalibrationReading; // initial roll reading before calibration, this value is used for calibrtion
float RollValue; // Roll angle
float CallibrationAdjustment; // Calibration adjustment
boolean RunCalibration = false; // has calibration run yet
// Handstand
boolean standStatus;
// timing
unsigned long startMillis;
unsigned long currentMillis;
unsigned long strandStartTime;
unsigned long standDuration;
// packet structure for InvenSense teapot demo
uint8_t teapotPacket[14] = { '
I am a little out of my depth here and I have tried trouble shooting by removing section of code and trying to isolate the issue but i'm totally stomped. Any advise or help in solving this would be greatly appreciated.
, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0x00, 0x00, '\r', '\n' };
// ================================================================
// === INTERRUPT DETECTION ROUTINE ===
// ================================================================
volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
mpuInterrupt = true;
}
// ================================================================
// === BLUETOOTH SETUP ===
// ================================================================
#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(7, 8); // RX, TX
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "my auth code";
SoftwareSerial SerialBLE(7, 8); // RX, TX
// ================================================================
// === INITIAL SETUP ===
// ================================================================
void setup() {
// bluetooth
Serial.begin(9600);
SerialBLE.begin(9600);
Blynk.begin(SerialBLE, auth);
Serial.println("Connected");
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
Serial.println("Start testing LED");
// bluetooth
startMillis = millis();
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
Serial.begin(9600);
while (!Serial); // wait for Leonardo enumeration, others continue immediately
u.initialize();
devStatus = mpu.dmpInitialize();
// supply your own gyro offsets here, scaled for min sensitivity
mpu.setXGyroOffset(138);
mpu.setYGyroOffset(-37);
mpu.setZGyroOffset(41);
mpu.setZAccelOffset(1338); // 1688 factory default for my test chip
// make sure it worked (returns 0 if so)
// turn on the DMP, now that it's ready
mpu.setDMPEnabled(true);
// enable Arduino interrupt detection
attachInterrupt(0, dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();
// set our DMP Ready flag so the main loop() function knows it's okay to use it
dmpReady = true;
// get expected DMP packet size for later comparison
packetSize = mpu.dmpGetFIFOPacketSize();
}
// ================================================================
// === MAIN PROGRAM LOOP ===
// ================================================================
void loop() {
Blynk.run();
currentMillis = millis();
// if programming failed, don't try to do anything
if (!dmpReady) return;
// wait for MPU interrupt or extra packet(s) available
while (!mpuInterrupt && fifoCount < packetSize) {
}
// reset interrupt flag and get INT_STATUS byte
mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();
// get current FIFO count
fifoCount = mpu.getFIFOCount();
// check for overflow (this should never happen unless our code is too inefficient)
if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
// reset so we can continue cleanly
mpu.resetFIFO();
Serial.println(F("FIFO overflow!"));
// otherwise, check for DMP data ready interrupt (this should happen frequently)
} else if (mpuIntStatus & 0x02) {
// wait for correct available data length, should be a VERY short wait
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
// read a packet from FIFO
mpu.getFIFOBytes(fifoBuffer, packetSize);
// track FIFO count here in case there is > 1 packet available
// (this lets us immediately read more without waiting for an interrupt)
fifoCount -= packetSize;
// display Euler angles in degrees
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
RollValue = (ypr[2] * 180 / M_PI);
if ((RunCalibration == false) && (currentMillis - startMillis >= 2000)) //optimum millis 5000
{
Calibration();
}
else if (RunCalibration == true) {
TestMeasurements();
}
}
}
// ================================================================
// === CALIBRATION FUNCTION ===
// ================================================================
void Calibration()
{
//Calibration reading
RollCalibrationReading = (ypr[2] * 180 / M_PI);
if ((RollCalibrationReading >= 0)&&(RollCalibrationReading <= 180)){
CallibrationAdjustment = RollCalibrationReading;
}
else if ((RollCalibrationReading > 180)&&(RollCalibrationReading <= 360)){
CallibrationAdjustment = RollCalibrationReading - 360;
}
//Take calibration reading once
Serial.print("Calibdarion value");
Serial.println(CallibrationAdjustment);
Serial.print("Time");
Serial.println(currentMillis);
Serial.println("");
RunCalibration = true;
}
// ================================================================
// === TEST MEAUSREMENTS ===
// ================================================================
void TestMeasurements() {
//Serial.print("rool value before calibration\t");
//Serial.println(ypr[2] * 180 / M_PI);
//Serial.print("roll angle after calibration\t");
Serial.println(RollValue);
//Serial.print("\t");
//Serial.println(currentMillis);
}
I am a little out of my depth here and I have tried trouble shooting by removing section of code and trying to isolate the issue but i'm totally stomped. Any advise or help in solving this would be greatly appreciated.