Combine accelerometer and Bluetooth codes

Hello everyone! I am new with Arduino and I have an Arduino UNO, that is connected with a BT sensor ( HC05) and an accelerometer ( MPU6050).
The connections are as follows:
Connections with MPU6050:
|Arduino UNO|MPU6050|
|GND|GND|
|5V|Vcc|
|A5|SCL|
|A4|SDA|

Connections with BT module:
|Arduino UNO|HC05|
|GND|GND|
|5V|VCC|
|11|RX|
|10|TX|

I have a code for accelerometer:

// Librerias I2C para controlar el mpu6050
// la libreria MPU6050.h necesita I2Cdev.h, I2Cdev.h necesita Wire.h
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

// La dirección del MPU6050 puede ser 0x68 o 0x69, dependiendo 
// del estado de AD0. Si no se especifica, 0x68 estará implicito
MPU6050 sensor;

// Valores RAW (sin procesar) del acelerometro y giroscopio en los ejes x,y,z
int ax, ay, az;
//int gx, gy, gz;

void setup() {
  Serial.begin(38400);    //Iniciando puerto serial
  Wire.begin();           //Iniciando I2C  
  sensor.initialize();    //Iniciando el sensor

  if (sensor.testConnection()) Serial.println("Sensor iniciado correctamente");
  else Serial.println("Error al iniciar el sensor");
}

void loop() {
  // Leer las aceleraciones 
  sensor.getAcceleration(&ax, &ay, &az);
  //Calcular los angulos de inclinacion:
  float accel_ang_x=atan(ax/sqrt(pow(ay,2) + pow(az,2)))*(180.0/3.14);
  float accel_ang_y=atan(ay/sqrt(pow(ax,2) + pow(az,2)))*(180.0/3.14);
  //Mostrar los angulos separadas por un [tab]
  Serial.print("Inclinacion en X: ");
  Serial.print(accel_ang_x); 
  Serial.print("tInclinacion en Y:");
  Serial.println(accel_ang_y);
  delay(10);
}

that works perfectly, and prints the angles in the serial monitor.
I also have a code for BT:

//importo librerias y configuro pines de comunicacion
#include <SoftwareSerial.h>
#include <TimerOne.h>
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

SoftwareSerial mySerial(10, 11); // RX, TX

// La dirección del MPU6050 puede ser 0x68 o 0x69, dependiendo 
// del estado de AD0. Si no se especifica, 0x68 estará implicito
MPU6050 sensor;

int ax, ay, az;

const int ledPin = 13; // Built in LED in Arduino board
//const int led = LED_BUILTIN;  // the pin with a LED

String msg,cmd; //lo que leo/escribo en bt
unsigned long myTime; //para millis y probar

//esto para protocolo de comunicacion
void rts();
unsigned int buffer1[100];
unsigned int buffer2[100];
bool bufferFlag = false;
bool sendFlag = false;

int level = 0;        // value output to the LED

void setup() {
  // Initialization
  Timer1.initialize(5000);  // every 5ms
  Timer1.attachInterrupt(rts);
  
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, level);

  //para ecg 
  // Inicializar la comunicación en serie:
  pinMode(3, INPUT); // Configuración para la detección LO +
  pinMode(2, INPUT); // Configuración para la detección LO -

  // Communication rate of the Bluetooth Module
  Wire.begin();           //Iniciando I2C  
  sensor.initialize();    //Iniciando el sensor

  if (sensor.testConnection()) Serial.println("Sensor iniciado correctamente");
  else Serial.println("Error al iniciar el sensor");

  mySerial.begin(38400); 
  Serial.begin(38400);
  msg = "";
}

void rts(){
  static unsigned long counter = 0;
  static unsigned int pos = 0;

  counter++;
  
  // Every 5 ms
  if (counter%1 == 0)
  {
    // send the value of analog input 0:
//      sensor.getAcceleration(&ax, &ay, &az);
      buffer1[pos++] = 1;
  }
    
    if (pos == 100)
    {
      for (unsigned int i=0; i<100; i++)
        buffer2[i] = buffer1[i];
      
      bufferFlag = true;
      pos = 0;
    }
  }

void loop() {
  // To read message received from other Bluetooth Device
  if(bufferFlag){
  for (unsigned int i = 0; i < 100; i++) {
    mySerial.write(buffer2[i]);
  }
  bufferFlag = false;
  }
}

As you can see: buffer1[pos++] = 1;. This means I send buffers full of ones. this buffer is sent correctly. When I change the 1 for the accelerometer data, like this:

sensor.getAcceleration(&ax, &ay, &az);
buffer1[pos++] = ax;

it does not work, and it does not send anything to the BT terminal. How can I include the accelerometer code in my BT code, so that accelerometer data is sent correctly?
Thanks a lot!
I will really appreciate suggestions

[quote="barbiezoani, post:1, topic:1183900"]
that works perfectly, and prints the angles in the serial monitor.
[/quote]p
There is nothing wrong with your use of Bluetooth, so consider this.
If you connect HC-05 to the hardware serial serial pins 0,1, you should see the same data on the BT terminal as you see on the serial monitor, without any changes to your first code, and you can ditch the second code altogether.
Does that help?

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.