Clear Filters
Clear Filters

How to create function that control speed of stepper motor?

12 views (last 30 days)
Hello every one..
I want to control stepper motor using arduino and matlab and stepper motor driver l298d, but without using adafruit motor shield v2 library. so i write this code
delete(instrfindall)
close all;
clear;
clc;
prompt = 'How many steps to move? '; % enter number of steps to move
steps_to_move = input(prompt);
step_number = 0;
number_of_steps = 200;
last_step_time = 0; % the time of last step
whatSpeed = 60;
%% initialization arduino pins
a = arduino();
configurePin(a,"D7","DigitalOutput");
configurePin(a,"D6","DigitalOutput");
configurePin(a,"D5","DigitalOutput");
configurePin(a,"D4","DigitalOutput");
%% determine delay time and dirction
steps_left = abs(steps_to_move);
step_delay = uint64(double(60*10^6)/number_of_steps / whatSpeed) % time between steps
if (steps_to_move)
direc = 1;
else
direc = 0;
end
%%
timeVal = tic; % start counting time
while (steps_left > 0)
now = uint64(toc(timeVal)*10^6); % time of right moment
if(now - last_step_time >= step_delay) % if now - last step time bigger than delay time
% then move the motor
last_step_time = now; % store the time
if (direc == 1)
step_number = step_number + 1;
if (step_number == number_of_steps)
step_number = 0;
end
else
if (step_number == 0)
step_number = number_of_steps;
end
step_number = step_number - 1;
end
steps_left = steps_left - 1;
end
stepMotor(a,mod(step_number,4)); % function to move motor
end
function stepMotor(ardObj,thisStep)
switch (thisStep)
case 0 % step 1
writeDigitalPin(ardObj,"D7",1);
writeDigitalPin(ardObj,"D6",0);
writeDigitalPin(ardObj,"D5",1);
writeDigitalPin(ardObj,"D4",0);
case 1 % step 2
writeDigitalPin(ardObj,"D7",0);
writeDigitalPin(ardObj,"D6",1);
writeDigitalPin(ardObj,"D5",1);
writeDigitalPin(ardObj,"D4",0);
case 2 % step 3
writeDigitalPin(ardObj,"D7",0);
writeDigitalPin(ardObj,"D6",1);
writeDigitalPin(ardObj,"D5",0);
writeDigitalPin(ardObj,"D4",1);
case 3 % step 4
writeDigitalPin(ardObj,"D7",1);
writeDigitalPin(ardObj,"D6",0);
writeDigitalPin(ardObj,"D5",0);
writeDigitalPin(ardObj,"D4",1);
end
end
the motor does not move correctly, there is no control speed.
i write the code with help of stepper motor library for arduino.
so any one could help me to correct the code or using other equation or ideas??

Answers (0)

Categories

Find more on 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!