I am trying to send some data from Matlab to arduino. It is a matrix of [1 by 2]. My plan is to convert this two numbers in to a string and send to the arduino. However in the serial monitor, I can not read any value coming from matlab.
This is my matlab code,
val_a = matt(n,:);
A = [val_a];
asd = A(1:1);
asb = A(:,2);
strA = num2str(asd);
strB = num2str(asb);
comma = ',';
endVal = '#';
theString = strcat(strA,comma,endVal);
obj1 = instrfind('Type', 'serial', 'Port', 'COM19', 'Tag', '');
if isempty(obj1)
obj1 = serial('COM19');
else
fclose(obj1);
obj1 = obj1(1);
end
fopen(obj1);
fprintf(obj1,theString)
fclose(obj1);
delete(obj1);
A = [];
And this is the serial event of arduino
bool gotalfa = false;
bool event = false;
void serialEvent() {
while (Serial.available())
{
char inChar = (char)Serial.read();
event = true;
if (inChar == , && !gotalfa)
{
alfa = inputString;
inputString = "";
gotalfa = true;
event = false;
}
if (inChar == '#' && gotalfa)
{
theta = inputString;
gotalfa = false;
inputString = "";
Serial.print("alfa ");
Serial.print(alfa);
Serial.print("theta ");
Serial.println(theta);
//some program....
event = false;
}
if(event)
{
inputString += inChar;
}}}
Do I have to change anything in my matlab/arduino code. Any helpful tip is greatly appreciated.
Thank you in advance