Hello. I am using Matlab to transmit an array to arduino over bluetooth, but I am not sure how to accomplish this. The matlab code I am using is as follows:
b = Bluetooth('HC-05',1);
fopen(b);
a=[1,2,1,1,2,1];
fwrite(b,a);
fclose(b);
I am able to read single values pretty much ok. but having trouble reading and storing values into an array by the arduino. Any help would be much appreciated.
The fclose(b); may cause the arduino to reset. If it does you will need to defeat the auro reset on comport closure. Does your arduino code work when using the serial monitor?
The problem is that I cant view my serial monitor while I am sending the value through matlab. it says the bluetooth is being used by another application. Here's the code i used to check transmission of a single value:
int ledr=7;
int ledg=4;
int val;
boolean newData=false;
void setup()
{
Serial.begin(9600);
pinMode(ledr,OUTPUT);
pinMode(ledg,OUTPUT);
}
void loop()
{
if(Serial.available() > 0){
val=Serial.read();
newData=true;
}
if (newData==true){
if (val==1){
digitalWrite(ledr,HIGH);
digitalWrite(ledg,LOW);
newData=false;
}
else if (val==2){
digitalWrite(ledr,LOW);
digitalWrite(ledg,HIGH);
newData=false;
}
else{
digitalWrite(ledr,LOW);
digitalWrite(ledg,LOW);
newData=false;
}
}
}
This code works fine. but when i try sending an array and doing a serial read of it. it doesnt read all except for the first digit
This code works fine. but when i try sending an array and doing a serial read of it. it doesnt read all except for the first digit
You need to capture the string of characters being sent, then evaluate the contents of what is captured. Below is a simple example.
//zoomkat 3-5-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//send on, or off, from the serial monitor to operate LED
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial LED on/off test with , delimiter"); // so I can keep track
}
void loop() {
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
Serial.println(readString); //prints string to serial port out
//do stuff with the captured readString
if(readString.indexOf("on") >=0)
{
digitalWrite(ledPin, HIGH);
Serial.println("LED ON");
}
if(readString.indexOf("off") >=0)
{
digitalWrite(ledPin, LOW);
Serial.println("LED OFF");
}
readString=""; //clears variable for new input
}
else {
readString += c; //makes the string readString
}
}
}
Not sure if there is anyway to mark this as solved, but anyway I shall post my codes here for anyone who needs a solution to the same problem.
Here is the arduino code:
int dist[20]={};
int ang[20]={};
int i;
boolean newData=false;
boolean flag=false;
boolean a_dist=false;
boolean a_ang=false;
int ledr=7;
int ledg=4;
int len;
void setup() {
Serial.begin(9600);
pinMode(ledr,OUTPUT);
pinMode(ledg,OUTPUT);
}
void loop() {
//check if there is data to be read
if (Serial.available()>0)
{
//check if the length has been received
if (flag==false)
{
len=Serial.read();
flag=true;
}
else
{
//fill up array distances
if (a_dist==false)
{
dist[i]=Serial.read();
i++;
if (i==len)
{
a_dist=true;
i=0;
}
}
else if (a_ang==false)
{
//fill up angles array
ang[i]=Serial.read();
i++;
if (i==len)
{
a_ang=true;
i=0;
}
}
}
if ((flag==true)&&(a_dist==true)&&(a_ang==true))
{
newData=true;
}
}
if (newData==true){
Serial.println(len);
//print out distance array
for (int x=0;x<len;x++)
{
Serial.println(dist[x]);
}
//print out angles array
for (int x=0;x<len;x++)
{
Serial.println(ang[x]);
}
newData=false;
flag=false;
a_dist=false;
a_ang=false;
}
}
And here is the matlab code that was used to test if both arrays are received correctly:
b = Bluetooth('HC-05',1);
fopen(b);
d=[20,10,20,10];
a=[90,34,56,45];
len=length(d);
fwrite(b,len);
fwrite(b,d);
fwrite(b,a);
r_len=fscanf(b,'%d');
for i=1:1:len
r_dist(i)=fscanf(b,'%d');
end
for i=1:1:len
r_ang(i)=fscanf(b,'%d');
end
fclose(b);