I have written code to collect i2c data from an accelerometer using an arduino (tested on Uno, Lilypad, and Nano) and transmit the data using an HC-05 bluetooth module. The data is read on an android phone using the common 'Serial Bluetooth Terminal' app. The issue that I am having, even with other terminal apps, is that many times while connected and paired there is not any transmission of data. Sometimes unplugging and replugging the USB power or reuploading the code will fix the issue. There is no issue with sending data to the HC-05 from the android phone to power an LED or control a motor. I am also using the SoftwareSerial library but the problem persists even if I send the data directly over the serial pins. The connections and code are the following:
HC-05
Baud rate: 9600 (makes no difference if I change it)
Config: slave
TX - pin 10
RX - pin 9
GND - GND
VCC - 5v/3.3v (no difference with the issue, but with 5v I do use a voltage divider for the RX pin)
The following code usually works fine and is included to show my initialization of the bluetooth module
#include <SoftwareSerial.h>
#define rx 10
#define tx 9
SoftwareSerial BT(rx,tx);
void setup() {
BT.begin(9600);
}
void loop() {
if (BT.available()){
BT.println("hi");}
delay(200);
}
Using the same format as the above code, I send the following data:
if(BT.available()){
BT.print("Acc (g)");
BT.print(" x=");
BT.print(gForceX);
BT.print(" y=");
BT.print(gForceY);
BT.print(" z=");
BT.print(gForceZ);
BT.print(" Rot (°)");
BT.print(" x=");
BT.print(rotX);
BT.print(" y=");
BT.print(rotY);
BT.print(" z=");
BT.print(rotZ);
BT.print(" EMG_1=");
BT.print(EMG_1);
BT.print(" EMG_2=");
BT.print(EMG_2);
BT.print("\n");
}
The Definitions:
#include <SoftwareSerial.h>
#include <Wire.h>
long accX, accY, accZ;
float gForceX, gForceY, gForceZ;
long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;
int EMG_1, EMG_2;
#define rx 10
#define tx 9
#define emg1 A2
#define emg2 A3
As you can see, I am sending a concatenation of strings, floats, and ints. When the code works, it runs great, regardless of whether I include delays or not in the loop function. I have also tried multiple HC-05 modules and the inconsistency problem persists. I hope to include my project in a wearable design with a lithium ion battery as an external supply so I can't keep reuploading the code and unplugging the power source to fix this issue. Any help is greatly appreciated.