与Arduino和MATLAB的串行通信 - 调试问题

时间:2018-02-15 12:07:28

标签: matlab arduino-uno

我编写了以下MATLAB代码,它将数组的每个元素“sendData”并将其发送到Arduino Uno。然后,Arduino使用此数据来确定使用if / ifelse循环时电机必须运行的配置。

当我逐行进入while循环时,电机有时会转动。当他们不这样做时,我尝试在命令窗口中执行fprintf语句,这往往会起作用。

如果有人深入了解为什么当我运行和/或进入它时这个代码没有正确执行,但是当我在命令窗口中执行fprintf语句时工作正常,我将非常感谢任何帮助。

delete(instrfindall);
clear all;
clc

% assign hardware to variable a
a=serial('com3');
% create incriment variable
incr = 1;
% create sendData string array
sentData = ['1','2','3','4','0'];
% loop through the 4 possible motor set ups

while incr<5
     % open link to arduino
     fopen(a);
     % send string to arduino

     % open link to arduino
     fopen(a);
     % send string to arduino
     % string will be an index from the sentData array
     fprintf(a,sentData(incr));
     pause(2);
     % close the file
     fclose(a);

     % inciment
     incr =incr+1;
end
% tunr off all motors by sending '0' to Arduino
incr=5;
fopen(a);
% send string to arduino
% close the link
fclose(a);

0 个答案:

没有答案