How to create function that control speed of stepper motor?

3 visualizzazioni (ultimi 30 giorni)
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??

Risposte (1)

surya venu
surya venu il 28 Ago 2024
Hi,
The direction logic seems incorrect. You should set "direc" based on the sign of "steps_to_move". If "steps_to_move" is positive, "direc" should be 1; if negative, it should be -1.
if steps_to_move > 0
direc = 1;
else
direc = -1;
end
Hope it helps.

Categorie

Scopri di più su Arduino Hardware in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by