The entire Arduino hardware setup is quite straight-forward. I double-checked all the wirings, including between the Arduino UNO R3 and the HC-06 Bluetooth module.
I also have an MPU6050 sensor connected to the UNO. When the Bluetooth module is disconnected, the hardware is working as expected via the Arduino Serial Monitor, that is, there are no issues when outputting sensor data and sending commands via the Arduino Serial Monitor. The problem is the following:
The commands sent from the app are not working (although sensor data from the Arduino is being displayed correctly in the app and updated at a fixed time interval). The up and down buttons in the app should turn ON and OFF the onboard LED on the Arduino. When the BT module is unplugged and the Arduino UNO is connected to the PC via USB cable, sensor data is shown in the Arduino Serial Monitor and i can also control the onboard LED by typing the value 1 or 2 in the Arduino Serial Monitor. But after i connect the BT module to the Arduino UNO, i tried the ON and OFF buttons in the app but neither work.
Here is my updated Arduino code. The main change that i made from the initial version in the first post, is the use of millis to time the sensor data output more precisely instead of relying on the number of loops. This was necessary to time it relative to the app Clock TimerInterval set to 100.
#include <FlexiTimer2.h>
#include <Wire.h>
#include <MPU6050.h>
MPU6050 accelgyro;
volatile int16_t ax, ay, az, gx, gy, gz; //Define three-axis acceleration and three-axis gyroscope variables
float Gyro_x, Gyro_y, Gyro_z; //Gyro angular velocity
float accelAngle = 0; //calculated tilt angle from accelerometer
char val;
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 300;
void setup()
{
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
//join I2C bus
Wire.begin(); //join I2C bus sequence
Serial.begin(9600); //open the serial monitor, set the baud rate to 9600
accelgyro.initialize(); //initialize MPU6050
accelgyro.setXAccelOffset(-1784); //set offset values for the accelerometer and gyroscope.
accelgyro.setYAccelOffset(88);
accelgyro.setZAccelOffset(951);
accelgyro.setXGyroOffset(106);
accelgyro.setYGyroOffset(-32);
accelgyro.setZGyroOffset(-6);
delay(4000); //give enough time for MPU-6050 values to stabilise.
FlexiTimer2::set(5, timerISR); //run timerISR function every 5ms
FlexiTimer2::start(); //start timer interrupt
startMillis = millis(); //initial start time
}
void loop()
{
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if(Serial.available() > 0)
{
val = Serial.read(); //assign the value read from serial port to variable val
switch(val) //switch case statement
{
case '1':
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
Serial.print("******************** ");
Serial.print("LED ON");
Serial.println(" ********************");
break;
case '2':
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
Serial.print("******************** ");
Serial.print("LED OFF");
Serial.println(" ********************");
break;
}
}
// the sensor data is sent to the MIT app every 300 ms.
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
//By default, Serial. print() prints floats with two decimal digits.
Serial.print(accelAngle); //send tilt angle to App
Serial.print(";"); //delimiter for variables so the app can distinguish between them
Serial.print(Gyro_x);
Serial.print(";");
Serial.print(Gyro_y);
Serial.println(";");
startMillis = currentMillis; //to save the new start time.
}
}
void timerISR()
{
interrupts(); //Re-enables interrupts
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); //IIC to get MPU6050 six-axis data ax ay az gx gy gz
angle_calculation(ax, ay, az, gx, gy, gz); // get angle
}
void angle_calculation(int16_t ax, int16_t ay, int16_t az, int16_t gx, int16_t gy, int16_t gz)
{
accelAngle = -atan2(ay, az) * (180/ PI); //tilt angle from accelerometer
Gyro_x = -gx / 131.0; //angular speed of X-axis from gyro
Gyro_y = -gy / 131.0; //angular speed of Y-axis from gyro
}
I thought that maybe having the UNO connected via USB cable to my PC is what's causing this communication issue with the BT module and MIT app. So, i tried to power the Arduino via USB separately from a charger but same problem - the MIT app control buttons don't have any effect. Maybe instead of powering the Arduino via its USB port (which i'm guessing might be interfering with the RX and TX transmissions from the BT module), i should power it via its power connector? But i'm not sure if it's going to make any difference. Any advice is very welcome.