Hello,
I need to send data from matlab to arduino and then back. I'm using arduino uno and matlab R2015. I tried send data numbers one by one and it works. I receive first number back, because I don't have there loop it's only "test program" .But mine professor want to send numbers in matrix. So I must do some changes. Here is example of matrix(servalue= [0 1 2 3 4];). And when I want to send a matrix, matlab showed me this: (Warning: Unsuccessful read: A timeout occurred before the Terminator was reached..).
I tried almost everything, I changed terminator to CR to make it faster response, different Baudrate etc. But still same problem. I saw somewhere, that function Serial.read(); in arduino, read data one by one. So can be problem there?
Or do you have some advice please?
Sorry for my English.
Matlab one by one"works":
s=serial('COM7','BAUD', 9600);
fopen(s);
for m=1:10
servalue= input('Enter the value 1 to turn ON LED & 0 to turn OFF LED :');
fprintf(s,servalue);
end
i = fscanf(s, '%s');
disp(i);
Matlab matrix:
s=serial('COM7');
set(s, 'Baudrate', 9600);
set(s, 'Parity', 'none');
set(s, 'Databits', 8);
set(s, 'Stopbit', 1);
set(s, 'Terminator', 'CR');
fopen(s);
servalue= [0 1 2 3 4];
fprintf(s,servalue);
i = fscanf(s, '%s');
disp(i);
%display the properties of serial port object in MATLAB Window
disp(get(s,{'Type','Name','Port','BaudRate','Parity','DataBits','StopBits','Terminator'}));
disp('Character sent to Serial Port is:');
disp(servalue);
pause(3);
return;
fclose(s);
delete(s);
clear s;
Arduino code:
const int ledpin=13;
int recValue;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
delay(2500);
}
void loop()
{
if(Serial.available()>0)
{
recValue=Serial.read();
if (recValue == 1) // If use will send value 100 from MATLAB then LED will turn ON
{
digitalWrite(ledpin, HIGH);
}
if(recValue == 0) // If use will send value 101 from MATLAB then LED will turn OFF
{
digitalWrite(ledpin, LOW);
}
Serial.print("I received: ");
Serial.println(recValue, DEC);
delay(200);
}
}