Have a problem with control the speed of 2 stepper motor

Need to control 2 stepper have the same axis but at different speed ...have a problem with this code

digitalWrite(dirPin3,HIGH);

digitalWrite(dirPin1,HIGH);

for(int x= 0; x < 3000; x++) {

digitalWrite(stepPin3,HIGH);
delayMicroseconds(3000); 
digitalWrite(stepPin3,LOW); 
delayMicroseconds(3000); 

digitalWrite(stepPin1,HIGH);
delayMicroseconds(500); 
digitalWrite(stepPin1,LOW); 
delayMicroseconds(500); 

they run at the same speed

They are in the same loop, they will run at the same speed.


BTW delay( ) is a very bad way to handle timing.

Have you heard of BWD (blink without delay).

1 Like

Post a picture of this assembly!
As I read Your question this construction and this code is a disaster, can never work.
By all means, post the entire code. Wiring might also be interesting.

1 Like

const int dirPin1 = 3;

const int stepPin1 = 4;

const int enPin1 = 5;

const int dirPin3 = 6;

const int stepPin3 = 7;

const int enPin3 = 8;

const int dirPin2 = 9;

const int stepPin2 = 10;

const int enPin2 = 11;

void setup() {

pinMode(stepPin1,OUTPUT);

pinMode(dirPin1,OUTPUT);

pinMode(enPin1,OUTPUT);

digitalWrite(enPin1,LOW);

pinMode(stepPin2,OUTPUT);

pinMode(dirPin2,OUTPUT);

pinMode(enPin2,OUTPUT);

digitalWrite(enPin2,LOW);

pinMode(stepPin3,OUTPUT);

pinMode(dirPin3,OUTPUT);

pinMode(enPin3,OUTPUT);

digitalWrite(enPin3,LOW);

}

void loop() {

digitalWrite(dirPin3,HIGH);
digitalWrite(dirPin1,HIGH);

for(int x= 0; x < 3000; x++) {

digitalWrite(stepPin3,HIGH);
delayMicroseconds(3000); 
digitalWrite(stepPin3,LOW); 
delayMicroseconds(3000); 

digitalWrite(stepPin1,HIGH);
delayMicroseconds(500); 
digitalWrite(stepPin1,LOW); 
delayMicroseconds(500); 

}

delay(1000); // One second delay

digitalWrite(dirPin2,HIGH);
for(int x = 0; x <3000; x++) {
digitalWrite(stepPin2,HIGH);
delayMicroseconds(3000);
digitalWrite(stepPin2,LOW);
delayMicroseconds(3000);

}

delay(1000);
digitalWrite(dirPin1,LOW);
for(int x = 0; x < 3000; x++) {
digitalWrite(stepPin1,LOW);
delayMicroseconds(3000);
digitalWrite(stepPin1,HIGH);
delayMicroseconds(3000);
}
delay(1000); // One second delay

digitalWrite(dirPin2,LOW);
for(int x = 0; x < 3000 ; x++) {
digitalWrite(stepPin2,LOW);
delayMicroseconds(3000);
digitalWrite(stepPin2,HIGH);
delayMicroseconds(3000);
}
delay(1000);

it works with me but i need to using one of the motor with higher speed and in the same axis .. sorry for my english

is there any way to make them in same axis at the same time with different speed ?

Okey. Preferably You read the first topic telling how to get the best from this forum. There it tells about how to post code, using autoformat and code tags as well as tips about info wanted by helpers.

The code is not large and looks like "not good" to me.

I asked You to post a picture of the 2 motors and their axle. Can You work on that?
Using word sallad to describe wiring as well as mechanical design very often goes wrong. Using Google translate is asking for misunderstandings.

Iam using (nema23 16kg/cm) stepper motor .. i have a 3 motors ( 2 in the same direction ) and the other for different direction and i have 3 drivers (Div268N). My code is simple because I'm not a professional just try to finish my graduation project .. the project working but know i need to use one of the two motor at the same direction in different speed but i couldn't do that so I'm asking about solution .. the project is working so the wiring is correct just need to use different speed for one motor

What does this change do for you ?

for(int x= 0; x < 3000; x++) {

digitalWrite(stepPin3,HIGH);
delayMicroseconds(3000); 
digitalWrite(stepPin3,LOW); 
delayMicroseconds(3000); 

digitalWrite(stepPin1,HIGH);
delayMicroseconds(500); 
digitalWrite(stepPin1,LOW); 
delayMicroseconds(500); 
digitalWrite(stepPin1,HIGH);
delayMicroseconds(500); 
digitalWrite(stepPin1,LOW); 
delayMicroseconds(500); 
}

It's working!
Thanks alot sir :heart:
Is there anyway to increase the speed more because when i change the number(500) the motor distribute

Hi, @kiros00
Welcome to the forum.

To add code please click this link;

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Simple, AccelStepper library.

Just a tip; use a Stepper.h library

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.