I am using the acceleration + gyro data from MPU6050 IMU to be transmitted using HC-05 Bluetooth modules for head mouse project to move the mouse cursor by movement data. Both of my Arduinos are Arduino Micro. The problem is when I send simple numerical values to test the wireless connection, I find that the data is transmitting perfectly. But when I connect the MPU6050 and try to send data, the receiver side sometimes receives the data perfectly and sometimes receives garbage values indicated by the serial monitor causing the mouse cursor to misbehave. Even the format/order of the data structure values gets scrambled sometimes. The code I'm using and the circuit connection is given below:
N.B: Both HC-05 modules set to 9600 baud rates which was the default setting. I tested with 19200 and 115200. But the same behavior remained.
Circuit:
HC-05 Arduino Micro (same for both HC-05 modules)
RX 11
TX 10
EN 9
VCC 5V
GND GND
MPU6050 Arduino Micro (sender side)
SDA 2
SCL 3
VCC 5V
GND GND
BT_mouse_integrate_send.ino
#include<SoftwareSerial.h>
#include<SPI.h>
#include<Mouse.h>
#include<Wire.h>
#include<I2Cdev.h>
#include<MPU6050.h>
SoftwareSerial BTSerial(10,11);
MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz, oax, oay, oaz, ogx, ogy, ogz;
int16_t vx, vy;
struct dataStruct{
int moveX;
int moveY;
int flag;
int counter;
};
struct dataStruct myData;
int size_data = sizeof(struct dataStruct);
void send(const dataStruct* table){
BTSerial.write((const char*)table, size_data);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
mpu.initialize();
if(!mpu.testConnection()){
while(1);
}
BTSerial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
oax = ax+72;
oay = ay-382;
oaz = az-505;
ogx = gx+99;
ogy = gy-29;
ogz = gz-50;
vx = gx/200;
vy = -gz/200;
myData.moveX = vx;
myData.moveY = vy;
Serial.print(myData.moveX);
Serial.print(" ");
Serial.print(myData.moveY);
Serial.print(" ");
Serial.print(myData.flag);
Serial.print(" ");
Serial.println(myData.counter);
send(&myData);
myData.counter++;
delay(20);
}
BT_mouse_integrate_get.ino
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Mouse.h>
#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>
SoftwareSerial BTSerial(10, 11);
struct dataStruct {
int moveX;
int moveY;
int flag;
int counter;
};
struct dataStruct myData;
int size_data = sizeof(struct dataStruct);
bool receive(dataStruct* table) {
return (BTSerial.readBytes((char*)table, sizeof(dataStruct)) == sizeof(dataStruct));
}
void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(9600);
BTSerial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
receive(&myData);
Serial.print(myData.moveX, DEC);
Serial.print(" ");
Serial.print(myData.moveY, DEC);
Serial.print(" ");
Serial.print(myData.flag);
Serial.print(" ");
Serial.println(myData.counter, DEC);
Mouse.move(myData.moveX, myData.moveY);
delay(20);
}