Matlab - Arduino Due - Mac OS X problem

1 view (last 30 days)
Giacomo Pedretti
Giacomo Pedretti on 17 Apr 2015
Hi, i've been trying to implement a stable serial connection between matlab and arduino due. What i have to do is pretty simple, i have a string of 8 values (0 or 1), i want to send it to arduino and process it in a loop to turn on and off a digital output consequently. I have a couple of problems: 1 - sometimes Matlab gives me the error (only sometimes, no clue why) Open failed: Port: /dev/tty.usbmodemfa131 is not available (port name is an example, i always check for the name and modify it) while the port is connected as i can see from terminal or trough other serial communication tools (arduino IDE) (and yes i closed all the other tools before) 2 - arduino is not receiving the right values, in fact if i monitor the received string i have some 5 and 3 values, no clue why. Can somebody help me plese? The matlab code is:
clc
answer=1; % this is where we'll store the user's answer
arduino=serial('/dev/tty.usbmodemfa131','BaudRate',9600) % create serial communication object on port tty.usbmodemfa131
fopen(arduino); % initiate arduino communication
v1 = [0 1 0 1 0 1 0 1];
while answer < 8
fprintf(arduino,'%s',char(v1(answer))); % send answer variable content to arduino
answer = answer+1;
pause(0.5);
end
fclose(arduino); % end communication with arduino
and the arduino code is
int matlabData [8];
int i=0;
void setup()
{
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
Serial.begin(9600);
}
void loop()
{
for(i=0;i<7;i++){
if(Serial.available()>0) // if there is data to read
{
matlabData[i]=Serial.read(); // read data
}
}
i=0;
for(i=0;i<7;i++){
digitalWrite(13,matlabData[i]);
digitalWrite(12,matlabData[i]);
delay(500);
}
}
Can somebody help me please? thank you

Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!