Arduino stop to communicate with the computer during a "while" loop.

1 次查看(过去 30 天)
Hi,
i have a trouble with arduino. I wrote a while loop that delete, for each cycle, the first number of a matrix. During the loop some time the connection with arduino is lost, and the program crash.
here an example of the matlab script
ar = arduino();
x = [0, 1, 1, 0, 1, 0, 0 ,1, 1];
counter = 0;
while ~isempty(x);
counter = counter +1;
if x(1) == 1;
writeDigitalPin(ar,'D13',1);
end;
if x(1) == 1;
writeDigitalPin(ar,'D13',0);
end;
x(1) = [];
end
I don't know why it happened.
Does it depend on the usb cable that connect arduino to the computer? I had to add a usb prolong form the arduino USB.

回答(1 个)

Ankita Nargundkar
Ankita Nargundkar 2016-10-21
Attach the crash log and the exact error message you are seeing. That will help.
  1 个评论
Ivan
Ivan 2016-10-24
Dear Ankita, Thank you very much for your answer. We managed to solve the problem, and the solution is to communicate with arduino "directly" by means of the serial port, so just create a serial port object and send the commands by opening the port and using the command "fwrite". I share the method here just in case someone else need it. There are two steps:
1) prepare arduino to communicate with matlab 2) write the matlab script.
STEP 1
In the arduino code you have to set the channels you want to use. In this case you prepare these channel to communicate with Matlab. Here an example where i used as channels input the channel n 11 and 12.
int ledPin1 = 11;
int ledPin2 = 12;
int matlabData;
void setup()
{
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0) // if there is data to read
{
matlabData=Serial.read(); // read data
if(matlabData==1)
digitalWrite(ledPin1,HIGH); // turn light on
else if(matlabData==2)
digitalWrite(ledPin1,LOW); // turn light off
else if (matlabData==3)
digitalWrite(ledPin2,HIGH); // turn light on
else if(matlabData==4)
digitalWrite(ledPin2,LOW); // turn light off
}
}
STEP 2
Matlab and Arduino communication.
You have to create a serial object first, and then send the commands to arduino trough this serial object. Since i am using a mac, my serial is /dev/cu.usbmodem641, this should be COM1 or something else on windows. just check in arduino -> tools -> port
arduino = serial('/dev/cu.usbmodem641');
fopen(arduino);
fwrite(arduino,'1'); %this activate the channel 11 on arduino board
fwrite(arduino,'2');%this deactivate the channel 11 on arduino board
fwrite(arduino,'3');%this activate the channel 12 on arduino board
fwrite(arduino,'4');%this deactivate the channel 12 on arduino board.
fclose(arduino);
In this way the connection with arduino is much more stable than when using the Arduino matlab toolbox. If you would like to check the connection during a loop, just put an if statement like this:
if strcmp(arduino.Serial,'close') == 1;
fopen(arduino);
end
In this case matlab check if the port connection is open, and if not it try to open it again.
Hope this can help other people in setting up experiments with arduino. I don't know why the connection is lost when i try to use the Arduino toolbox.
Thank you again for your answer,
Ivan

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by