I am a begginer in arduino coding and I wrote a code to control a stepper speed and direction but the code stalls in the middle it does not reverse the direction.
please help me with this
test.ino (1.52 KB)
I am a begginer in arduino coding and I wrote a code to control a stepper speed and direction but the code stalls in the middle it does not reverse the direction.
please help me with this
test.ino (1.52 KB)
hi, i think the problem is setting the motor speed to 0 even if you set 0 steps, try to delete this or replace with just a delay()
These links may be useful
Stepper Motor Basics
Simple Stepper Code
For short programs please include them in your post between
tags so we don't have to download them. Your code shouldlook like this
and that also makes it easy to copy to a text editor
...R
Thanks guys for your interest
I removed the part of speed and step zero but still dont work
Post the corrected code then - we can't fix what we can't see.
Use code tags this time.
#include <Stepper.h>
const int start=5;
const int motor=6;
const int SPR= 200;
Stepper mystpr (SPR,8,9,10,11);
void setup(){
pinMode(start,INPUT);
pinMode (motor,OUTPUT);
}
void loop() {
digitalWrite(motor,LOW);
// if(digitalRead(start)==HIGH){
digitalWrite(motor,HIGH);
mystpr.setSpeed (40.6);
mystpr.step (198);
mystpr.setSpeed (32.97);
mystpr.step (185);
mystpr.setSpeed (25.14);
mystpr.step (159);
mystpr.setSpeed (20.68);
mystpr.step (143);
mystpr.setSpeed (17.71);
mystpr.step (132);
mystpr.setSpeed (14.26);
mystpr.step (113);
mystpr.setSpeed (10.45);
mystpr.step (88);
mystpr.setSpeed (8.9);
mystpr.step (78);
mystpr.setSpeed (7.4);
mystpr.step (67);
mystpr.setSpeed (5.2);
mystpr.step (48);
mystpr.setSpeed (3.6);
mystpr.step (34);
mystpr.setSpeed (2.3);
mystpr.step (22);
mystpr.setSpeed (0.7);
mystpr.step (7);
//mystpr.setSpeed (0);
// mystpr.step (0);
//delay(5000);
mystpr.setSpeed (1);
mystpr.step (-9);
mystpr.setSpeed (1.8);
mystpr.step (-17);
mystpr.setSpeed (3.8);
mystpr.step (-37);
mystpr.setSpeed (3.5);
mystpr.step (-33);
mystpr.setSpeed (5.6);
mystpr.step (-53);
mystpr.setSpeed (6.7);
mystpr.step (-61);
mystpr.setSpeed (8);
mystpr.step (-72);
mystpr.setSpeed (9);
mystpr.step (-79);
mystpr.setSpeed (11.5);
mystpr.step (-96);
mystpr.setSpeed (15.5);
mystpr.step (-123);
mystpr.setSpeed (15.4);
mystpr.step (-115);
mystpr.setSpeed (16.4);
mystpr.step (-115);
mystpr.setSpeed (20.7);
mystpr.step (-137);
mystpr.setSpeed (27);
mystpr.step (-166);
digitalWrite(motor,LOW);
}
//}
What are all the similar pairs of lines of code for?
If you are trying for smooth acceleration use the AccelStepper library.
...R
I need to connect the motor to a ball screw and control the displacement and the speed in a determined time, for example I want to move 2mm backward in 2 seconds then 3mm backwarn in 1.5 seconds ...etc
then I need to go forward in the same way.
The code moves backward correctly but stalls when reverse the direction!!
jeo-hosny:
The code moves backward correctly but stalls when reverse the direction!!
Do you mean it fails at Line 44 ?
Post a link to the datasheets for your stepper motor and your stepper motor driver.
What power supply (volts and amps) are you using for the stepper motor.
If you just use a very simple program are you able to make the motor move in either direction?
Is there a load on the motor? Is the load affecting the motor's behaviour?
...R
PS ...
If you put your values into arrays (for illustration, pretend there are 50 values) you could reduce all that code to something like
for (byte n = 0; n <50 n++) {
mystpr.setSpeed(mSpeed[n]);
mystpr.step(mSteps[n]);
}
I am still testing the code and the control circuit, I didn't connect any stepper yet, I am connecting LED's instead of the four coils of the motor.
the upper half of the code run correctly and stop at the reverse part
when I cancel the forward part and keep the reverse the code run in the reverse direction without any problem
I will try the array solution hope it works
thanks for your interest
jeo-hosny:
I am still testing the code and the control circuit, I didn't connect any stepper yet, I am connecting LED's instead of the four coils of the motor.
the upper half of the code run correctly and stop at the reverse part
If you are using LEDs then you need to explain to me what exactly you are seeing that leads you to that conclusion.
But IMHO testing without a motor is a waste of time.
You need to post a link to the datasheet for your motor and for your stepper driver.
...R
I see the LEDs start blinking fast then according to the code they start to blink slowly until they stop blinking, after they stop they should start to blink faster but they dont
the program just stall
I asked earlier if the stall happens at line 44 (code in Reply #5) - but you did not answer that.
Put some print statements into your program so that you can follow progress through the code.
...R
thanks for helping guys, the problem was that i am giving then arduino a 0.7 speed, seems that it cant take a speed less than 1
thanks for your helping