Hello. Im currently working on a project where I have sensors connected to and Arduino, which uses HC-05 Bluetooth module to send values to Matlab.
However, when I run my code on matlab, i keep getting timeout errors. I have tried a lot of troubleshooting, but I'm out of ideas.
Any help would be appreciated.
This is my matlab code:
clear all;
s = bluetooth("DSD TECH HC-05",1);
fopen(s);
x1 = [];
time = [];
i = 1;
tic;
while true
b = readline(s);
value = str2double(b);
x1 = [x1, value];
time(i) = toc; % record the elapsed time
i = i + 1;
end
This is my Arduino Code:
#include <SoftwareSerial.h>
#define TxD 2
#define RxD 3
SoftwareSerial bt (RxD, TxD); // RX, TX for Bluetooth
int analogInput1 = A0;
unsigned long start_time;
unsigned long end_time;
int sample_count = 0;
void setup()
{
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
pinMode(analogInput1, INPUT);
bt.begin(230400); /* Define baud rate for software serial communication */
Serial.begin(230400); /* Define baud rate for serial communication */
start_time = millis();
}
void loop()
{
float sensorValue1 = analogRead(analogInput1);
float millivolt1 = (sensorValue1/1023)*5;
Serial.print(millivolt1*1000);
Serial.println("");
bt.print(millivolt1*1000);
bt.println("");
sample_count++;
// Store the end time
end_time = millis();
// Check if 1 second has elapsed
if (end_time - start_time >= 1000) {
// Calculate the sample rate
float sample_rate = (float)sample_count / (float)(end_time - start_time) * 1000.0;
// Print the sample rate to the serial
Serial.println("Sample rate: " + String(sample_rate) + " samples/s");
// Reset the sample count and start time
sample_count = 0;
start_time = end_time;
}
}