i want to move 2 stepper motors from Sparkfun with ArduinoUNO at the same time, it will be different degrees each one, the first one 360º clockwise and comeback, and at the same time the motor2 180º clockwise and comeback. I tried to make a code but THEY DONT move at the same time and i dont know how to change the velocity of them. I use a stepper motor of 1.8º and easydriver
const int dir1Pin=2; //dirección motor1
const int step1Pin=3; //nº de pasos motor1
const int dir2Pin=4; //dirección motor2
const int step2Pin=5; //nº de pasos motor2
int STEPS_PER_REV_1=200; //360º
int STEPS_PER_REV_2=100; //180º
int CONTADOR_1=100; //numero de rotaciones del motor1, contando ida y vuelta
void setup() {
Serial.begin(9600); //Inicio de la consola
pinMode(step1Pin,OUTPUT); //Declaracion de los pins como salidas
pinMode(dir1Pin,OUTPUT);
pinMode(step2Pin,OUTPUT);
pinMode(dir2Pin,OUTPUT);
}
void loop() {
for(int n=0; n<(CONTADOR_1);n++){
digitalWrite(dir1Pin,HIGH); //Motor1 giro en el sentido horario 1 rev
for(int x=0; x<(STEPS_PER_REV_1);x++){ //bucle para alcanzar el numero de revoluciones deseadas
digitalWrite(step1Pin, HIGH);
delayMicroseconds(1000);
digitalWrite(step1Pin,LOW);
delayMicroseconds(1000);
}
digitalWrite(dir1Pin,LOW); //Motor1 giro en el sentido antihorario 1 rev
for(int x=0; x<(STEPS_PER_REV_1);x++){
digitalWrite(step1Pin, HIGH);
delayMicroseconds(1000);
digitalWrite(step1Pin,LOW);
delayMicroseconds(1000);
}
digitalWrite(dir2Pin,HIGH); //Motor2 giro en el sentido horario 1 rev(180º)
for(int x=0; x<(STEPS_PER_REV_2);x++){ //bucle para alcanzar el numero de revoluciones deseadas
digitalWrite(step1Pin, HIGH);
delayMicroseconds(1000);
digitalWrite(step1Pin,LOW);
delayMicroseconds(1000);
}
digitalWrite(dir2Pin,LOW); //Motor2 giro en el sentido antihorario 1 rev(180º)
for(int x=0; x<(STEPS_PER_REV_2);x++){
digitalWrite(step1Pin, HIGH);
delayMicroseconds(1000);
digitalWrite(step1Pin,LOW);
delayMicroseconds(1000);
}
}
}
If you want them to move at the same time you must interleave the step instructions for the two motors which means that you can't use FOR loops.
Just use a variable to count the steps and allow loop() to do the repetition. Something like this pseudo code
void loop() {
if motorAsteps < motorAmaxSteps
if it is time for motorA to step
step motorA
motorAsteps ++
if motorBsteps < motorBmaxSteps
if it is time for motorB to step
step motorB
motorBsteps ++
}
mofalafel:
Hi, but i cant understand, so i need to delete the for´s?
You need to change the whole concept by which your program is designed. Programming is not the equivalent of removing one piece of a jigsaw puzzle and replacing it with a different piece.
or just put as setup and your pseudo code in the loop?
Yes the code (based on my psuedo code) should go into loop() but I don't understand what your mean by "just put as setup"
You are lucky that motor2 moves exactly half as many steps as motor1. That makes it easy. Just make a loop for motor1 and pulse motor2 only half the time.
const int dir1Pin = 2; //dirección motor1
const int step1Pin = 3; //nº de pasos motor1
const int dir2Pin = 4; //dirección motor2
const int step2Pin = 5; //nº de pasos motor2
int STEPS_PER_REV = 200; //360º
int CONTADOR_1 = 100; //numero de rotaciones del motor1, contando ida y vuelta
void setup()
{
Serial.begin(9600); //Inicio de la consola
pinMode(step1Pin, OUTPUT); //Declaracion de los pins como salidas
pinMode(dir1Pin, OUTPUT);
pinMode(step2Pin, OUTPUT);
pinMode(dir2Pin, OUTPUT);
}
void loop()
{
for (int n = 0; n < (CONTADOR_1); n++)
{
// the first one 360º clockwise
// at the same time the motor2 180º clockwise
digitalWrite(dir1Pin, HIGH); //Motor1 giro en el sentido horario 1 rev
digitalWrite(dir2Pin, HIGH); //Motor2 giro en el sentido horario 1 rev(180º)
for (int x = 0; x < STEPS_PER_REV; x++) //bucle para alcanzar el numero de revoluciones deseadas
{
digitalWrite(step1Pin, HIGH);
if ((x & 1) == 0) // Every even-numbered step
digitalWrite(step2Pin, HIGH);
delay(1);
digitalWrite(step1Pin, LOW);
if ((x & 1) == 0) // Every even-numbered step
digitalWrite(step2Pin, LOW);
delay(1);
}
// the first one 360º comeback
// at the same time the motor2 180º comeback.
digitalWrite(dir1Pin, LOW); //Motor1 giro en el sentido antihorario 1 rev
digitalWrite(dir2Pin, HIGH); //Motor2 giro en el sentido horario 1 rev(180º)
for (int x = 0; x < STEPS_PER_REV; x++) //bucle para alcanzar el numero de revoluciones deseadas
{
digitalWrite(step1Pin, HIGH);
if ((x & 1) == 0) // Every even-numbered step
digitalWrite(step2Pin, HIGH);
delay(1);
digitalWrite(step1Pin, LOW);
if ((x & 1) == 0) // Every even-numbered step
digitalWrite(step2Pin, LOW);
delay(1);
}
}
}