MATLAB and Arduino UNO for Servo Control

Hi everybody,

I'm new with Arduino. I've an arduino UNO and I'm trying to establish a connection with Matlab and arduino. The objective is to send values contained in a matrix to 6 servos connected to the board. Actually, I've found a program able to isolate single numbers from the matrix and send them like string: 30,90,40,70,60,80 This is for example the first row of a matrix with 6 colums due to the fact I use 6 servos. The comma separate each numbers. For the moment I've commented the part of control servo, I want just to display if what I send is correct.
The arduino code accumulate char in a string, till when it receives the comma, then converts it in an int number and, finally, send back the current number acquired.
I've attached my code in arduino and matlab. It seems work just for few number and that's it or sometimes display always 'A', like:

I'M IN
A
A
A
A
A
A
NEW GROUP
A
A
100
120
A
NEWGROUP

where NEWGROUP indicates that the row is changed

Can anybody help me?????????????
I send the serial data on COM3 that is where the usb cable of the arduino board is connected.
Thanks Again and best regards

Marco

ArduinoCommunication.m (4.71 KB)

sketch_serial_comm_matlab3.ino (2.2 KB)

     while (Serial.available() <= 0)

It is IMPOSSIBLE for there to be a negative number of characters to read. Testing for negative values is useless.

The Arduino sends A until the PC sends some kind of response. If you see A again later, it is because you reset the Arduino. So, stop doing that.

I haven't used Matlab, but I only send a single message to establish contact - for example a simple Serial.println('A'); in setup(). I have no idea why you keep repeating the A, or why you bother testing to see if anything is received at that stage. Or why not just use Serial.println("Continuum Robot Arduino Control by Marco Galli"); to esatblish contact?

If you know how many characters will be sent to the Arduino just wait until they are all available with

if (Serial.available() >= numChars) {

then read all the characters into a char array and add a 0 to terminate it. Don't use Striings (capital S) as they can cause problems in the limited memory on an Arduino.

If you don't know how many characters will be sent then you should arrange for Matlab to send a special terminating character and the Arduino can use that to detect when all the data has arrived.

The readCSV() function in this demo and the Arduino code in this demo should help to illustrate what I am saying.

...R