Help needed: Sending an array via serial port from MATLAB to Arduino

Hi

I'm looking to send a matrix from MATLAB to an Arduino and store each number as separate integers. For example, if I have a matrix in MATLAB called M = [100,200,300,400] i wan't to send it to the Arduino and store each entry so a = 100 b = 200 c = 300 d = 400

I can't find a way to make sure I'm actually storing the right values in the arduino, since the serial connection is used by MATLAB, and Arduino won't let me access it. So I've made the following code to test it. The code isn't good enough - it doesent matter if I use a, b, c or d as my pinnumber, the LED turns on anyways.

I hope someone is able to help! Please ask if you need more information.

Cheers

% MATLAB code

M=[100,200,13,400]; %the matrix that is to be sent to the Arduino
arduino=serial('COM4','BaudRate',9600); 
fopen(arduino); 
  while M
      fprintf(arduino,'%s',char(M)); 
  end
fclose(arduino);
// Arduino code

char matlabData[20]; // 20 is just a random number, which is large enough to hold the full matrix
int index;

void setup() 
{
  Serial.begin(9600);
  pinMode(13,OUTPUT);
}

void loop() 
{
   if(Serial.available()>0){
    index = Serial.readBytesUntil('#', matlabData,19);
    matlabData[index] = '\0'; 
    a = matlabData[0];
    b = matlabData[1];
    c = matlabData[2];
    d = matlabData[3];
    digitalWrite(c,HIGH); // this should turn on the LED at pin 13 if the 3rd entry in the matrix is 13
    delay(1000);
    }
}

I can't find a way to make sure I'm actually storing the right values in the arduino, since the serial connection is used by MATLAB, and Arduino won't let me access it.

Develop the code using the serial monitor so the arduino can echo back debugging data to verify your code is working as expected.

1) So … your problem is that you can't work out how to get debugging output from the arduino?

Attach a red led to one pin and a green LED to another. Send a known array down the wire. Have the arduino flag whether or not it read what it expect to read using the LED.

Yeah, it's tedious because you can only get a yes/no. Another option is some sort of display.

Oh - hang on - you are already doing this. Never mind :)

2) Note that a char can only hold a value 0-255 (or -128 to 127), so I don't know what char(M) is going to do when the fourth element of M is 400. It will probably take a modulus.

3) you call Serial.readBytesUntil('#', put I don't see anywhere in your MATLAB code that is sending a '#'. This means that it will just read that four-byte array until MATLAB has sent 5 of them (which fills your buffer)

4) the real problem I see is that matlab is just sending a sequence of four characters over and over to the arduino and there's no real synchronisation or handshaking happening.

Try this:

% MATLAB code

M=[100,200,13,400]; %the matrix that is to be sent to the Arduino
arduino=serial('COM4','BaudRate',9600); 
fopen(arduino); 
  while M
      fprintf(arduino,'%s#',char(M)); 
  end
fclose(arduino);

Now - this is shockingly bad as a protocol (it means you can't send a 35 down the wire without it being interpreted as an end-of-frame delimiter) but hopefully after the first pass through your loop(), the arduino will lock on to the series of '#' characters and begin populating its array correctly.

Have a look at serial input basics.

However if you want to be able to send any byte value from 0 to 255 and use start and end markers (as you should) then look at the Arduino code in this Python demo

Is it possible to write Matlab code to receive and display debug data from the Arduino?

...R

hai mdhor,

i want ask u what is this part for?

matlabData[index] = '\0';