I have an Arduino Pro Mini connected to an ADLX335 Accelerometer and HC-05 Bluetooth Module. I am trying to wirelessly record data from the accelerometer within MATLAB for an experiment
This starts out working fine. MATLAB is able to record the data for multiple trials. However, after about 15 minutes, MATLAB will give an error saying, "Warning: Unsuccessful read: A timeout occurred before the Terminator was reached." and the rest of the data will be nonsense.
After this happens, to fix it, I've found that I must do this: Clear all MATLAB variables and run "delete(instrfindall)". Close MATLAB. Go into control panel and "Remove devices" to manually disconnect the HC-05 bluetooth device. Disconnect and reconnect the bluetooth dongle on my desktop computer. Press the reset button on my Arduino. Go to control panel and "Add devices" to reconnect the HC-05 bluetooth device. Reopen MATLAB. Run Script 1 (explained below) to connect to the bluetooth and create bluetooth object. Run Script 2 to record data (explained below). Everything works fine now (for another 10-15 minutes again).
I don't know if all of these steps are required to fix the problem, but a few times that I've forgotten one of the steps above, the problem persisted so now to be safe I do all of the above. So I don't know what is the actual cause of the problem. Is there something wrong with my Arduino code? Is there an issue with my HC-05 bluetooth module or computer's bluetooth dongle? Is the way I'm handling the bluetooth object in MATLAB causing some confusion with the device? Any help on this issue would be appreciated because it is unnecessarily slowing down my experiments. I have included the code that I use below.
Arduino Script that converts the voltages from the three directional pins on my accelerometer into an angle and then sends that angle to the bluetooth device whose pins are configured as a serial port using SoftwareSerial.
//
#include <SoftwareSerial.h>
const int xpin = A1; // x-axis of the accelerometer
const int ypin = A2; // y-axis
const int zpin = A3; // z-axis (only on 3-axis models)
SoftwareSerial Accelerometer(10, 11); // RX, TX
int BluetoothData; // the data given from Computer
int sampleDelay = 50; //number of milliseconds between readings
void setup()
{
// initialize the serial communications:
Accelerometer.begin(9600);
//Make sure the analog-to-digital converter takes its reference voltage from
// the AREF pin
analogReference(DEFAULT);
pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
pinMode(zpin, INPUT);
}
void loop()
{
int x = analogRead(xpin);
int y = analogRead(ypin);
int z = analogRead(zpin);
//zero_G is the reading we expect from the sensor when it detects
//no acceleration. Subtract this value from the sensor reading to
//get a shifted sensor reading.
float zero_G = 512.0;
//scale is the number of units we expect the sensor reading to
//change when the acceleration along an axis changes by 1G.
//Divide the shifted sensor reading by scale to get acceleration in Gs.
float scale = 102.3;
Accelerometer.print(57.3*atan2((((float)y - zero_G)/scale), (((float)x-zero_G)/scale)));
Accelerometer.print("\n"); // delay before next reading:
delay(sampleDelay);
}
MATLAB Script 1 that connects the bluetooth device and creates a MATLAB Bluetooth object. I run this script only once at the beginning to establish the connections so that my main data recording script doesn't waste time re-connecting.
b = Bluetooth('HC-05',1);
fopen(b);
MATLAB Script 2. This is a simplified framework of what the bluetooth device does during my data recording script that I run multiple times for each trial of my experiment. The main points here are that I record data via fscanf of "b" (the MATLAB bluetooth object) AND that this script never opens or closes the object b (since this would add a lot of waiting time in between each trial when I run the script again).
angle = zeros(1,100)
for i = 1:100
angle(i) = str2double(fscanf(b));
end