Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Having issues using MATLAB to generate gcode to control stepper motor

339 views
Skip to first unread message

Jooseppi Luna

unread,
Aug 20, 2015, 9:09:07 AM8/20/15
to
Hi everyone! I'm having some strange issues using MATLAB to send gcode to an Arduino to control a stepper motor. This is my code. I have comments in there to explain it.


%Open serial port to send commands to the Arduino
s = serial('COM12');
set(s,'BaudRate',115200);
fopen(s);

%Wait for grbl to initialize
pause(2)

%Output GCode.
for i = 1:20
fprintf(s,'G01 X%d F1000\n',40 * i);
fprintf(s,'G4 P2\n');
pause(5); %This makes things work?
end

fclose(s);
delete(s);
clear s;


The main issue with the code is that, if I loop without the pause(5) in the loop, the motor will loop at most 7 times before stopping. I have put a print statement that prints the value of i into the loop, and it does print the numbers 1-20, so the loop is not an issue. The weird thing is that, if I throw in a pause() command, the loop will run for longer depending how long a time I specify for the pause. Pause(5) is enough for me to run all twenty runs.

Any idea what is going on here?

James Riley

unread,
May 8, 2017, 8:56:07 PM5/8/17
to
Your Baud rate is pretty high. Could you settle for a rate that more closely matches your driver? You might find that your PC has a faster processor than your driver, therefore completing the loops in record time while the driver got fed too quickly. First time posting, sorry if you've considered this.
0 new messages