Arduino Uno with Matlab help

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
vho1327
 
Posts: 6
Joined: Sun Aug 19, 2012 11:31 am

Arduino Uno with Matlab help

Post by vho1327 »

Update Figured out the main problem (segmentation violation detected error) is caused by trying to close the communication by deleting the variable "a". Hope someone has a solution to this so I don't have to restart Matlab every time after I run the program.


Original Post
Hi, I am a new Arduino user here and am hoping to get some help with programming with Matlab.

I downloaded the ArduinoIO() from Mathworks that is made to be used with Arduino.
These are the steps I took:

1) In Matlab, typed "install_arduino." I've confirmed that the arduino files are there by testing "help arduino," which returned something instead of an error.

2) Uploaded the adio.pde to the Arduino Uno board using the Arduino IDE.

3) Wrote a simple program to test it. Code below:

Code: Select all

a = arduino('/dev/tty.usbmodem1d11');

a.pinMode(13,'output');

a.digitalWrite(13,1);
pause(2);
a.digitalWrite(13,0);

delete(instrfind({'Port'},{'/dev/tty.usbmodem1d11'}));
Matlab will says "attempting connection...." and I will get the message that it connected successfully. But after I wait a few more seconds, I get a message that says "Segmentation violation detected" and a bunch of other messages.

I am running Matlab 7.8.0. on MBP with an Arduino Uno

Any help will be great. Thanks!
Last edited by vho1327 on Sun Aug 19, 2012 4:52 pm, edited 2 times in total.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Arduino Uno with Matlab help

Post by adafruit_support_rick »

You should probably contact The Mathworks or whoever supplies ArduinoIO for support in this.

I've never heard of ArduinoIO before. But, if I'm reading your code correctly, you connect to the arduino and set pin 13 to output.

Then, you turn on the LED, wait for 2 (seconds? milliseconds?) , turn off the LED, and disconnect from the arduino.

So, the behavior you describe is pretty much what I would expect. If you are expecting the LED to be on for longer, check what pause(2) does. Is it waiting for 2 seconds or for 2 milliseconds?

vho1327
 
Posts: 6
Joined: Sun Aug 19, 2012 11:31 am

Re: Arduino Uno with Matlab help

Post by vho1327 »

pause(#) pauses the program in seconds.

I guess the main problem I am having is after the connection is made, the board doesn't do what the program says. LED goes on but doesn't turn off and a the message "segmentation violation detected" comes on and basically forces Matlab to close. I will also post this on a Matlab forum to get some help.

Thanks

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Arduino Uno with Matlab help

Post by adafruit_support_rick »

try putting a long pause AFTER the a.digitalWrite(13,0); Give it a chance to do something before it disconnects.

vho1327
 
Posts: 6
Joined: Sun Aug 19, 2012 11:31 am

Re: Arduino Uno with Matlab help

Post by vho1327 »

I added a pause(10) right after the digitalWrite(13,0). Unfortunately, the same problem persists :(.

After some more testing, I am pretty sure the problem lies in the end after the main part of the program executes. I altered the code to go on and off 3 times then close the connection:

Code: Select all

a = arduino('/dev/tty.usbmodem1d11');

a.pinMode(13,'output');

a.digitalWrite(13,1);
pause(5);
a.digitalWrite(13,0);
pause(5);
a.digitalWrite(13,1);
pause(5);
a.digitalWrite(13,0);
pause(5)
a.digitalWrite(13,1);
pause(5);
a.digitalWrite(13,0);
pause(5)

delete(a);
Everything is fine until it reaches the last line delete(a) . In fact, the segmentation error only pops up after the last pause(5) command is done.

I isolated the problem and found out that closing the communication by deleting the variable "a" will cause the error message.

I am hoping someone knows a better way to do delete the variable because if I don't delete it, the communication is basically occupied and I would have to restart Matlab to do it.

If I try deleting the variable from the workspace, the same segmentation error message shows up.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Arduino Uno with Matlab help

Post by adafruit_support_rick »

There's probably some clean way to disconnect - something you have to do before deleting the arduino object.

vho1327
 
Posts: 6
Joined: Sun Aug 19, 2012 11:31 am

Re: Arduino Uno with Matlab help

Post by vho1327 »

Maybe there is another way to do it but from the readme file and the sample code listed on the mathworks website, they only have the delete(a) to disconnect.

http://www.mathworks.com/matlabcentral/ ... ange/32374

Code: Select all

Sample usage:

 %-- connect to the board 
 a = arduino('COM9')

 %-- specify pin mode 
 a.pinMode(4,'input'); 
 a.pinMode(13,'output');

 %-- digital i/o 
 a.digitalRead(4) % read pin 4 
 a.digitalWrite(13,0) % write 0 to pin 13

 %-- analog i/o 
 a.analogRead(5) 
 a.analogWrite(9, 155) % write 155 to analog pin 9

 %-- motor shield 
 a.motorRun(4, 'forward') % run motor forward 
 a.servoWrite(1, 175); % move servo#1 to 175 deg position 
 a.stepperStep(1, 'forward', 'double', 100); % move stepper motor

 %-- close session 
 delete(a)

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Arduino Uno with Matlab help

Post by adafruit_support_rick »

I got no clue. You'll have to ask them.

vho1327
 
Posts: 6
Joined: Sun Aug 19, 2012 11:31 am

Re: Arduino Uno with Matlab help

Post by vho1327 »

Thanks for trying. I posted the question on their page and am waiting for a reply now

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”