Donfero
November 13, 2022, 1:44pm
1
Hi,
I'm trying to run 2 stepper motors at the same time using an Adafruit Motor Shield. I have managed to get the motors to move in both directions but not at the same time. how can I get both to move at the same time?
Here is the sketch i'm using;
#include <AFMotor.h>
int start = 0;
// Number of steps per output rotation
const int stepsPerRevolution = 200;
const int stepsPerRevolution1 = 200;
AF_Stepper motor1(stepsPerRevolution, 1);
AF_Stepper motor2(stepsPerRevolution, 2);
void setup() {
Serial.begin(9600);
Serial.println("Stepper test!");
motor1.setSpeed(95); // 85 rpm
motor2.setSpeed(95); // 85 rpm
pinMode(13, OUTPUT);
}
void swip()
{
Serial.println("Double coil steps");
motor1.step(430, BACKWARD, DOUBLE);
motor2.step(430, BACKWARD, DOUBLE);
delay(1500);
motor1.step(430, FORWARD, DOUBLE);
motor2.step(430, FORWARD, DOUBLE);
start++;
}
void loop() {
if (start == 0)
swip();
delay(500);
digitalWrite(13, HIGH);
}
for (int i = 0; i < 430; i++)
{
motor1.step(1, BACKWARD, DOUBLE);
motor2.step(1, BACKWARD, DOUBLE);
}
1 Like
Donfero
November 13, 2022, 2:12pm
3
Can i be stupid and ask what to replace?
never used the AFMotor.h-library
if the motors do not move at the same time your code
Donfero:
motor1.step(430, BACKWARD, DOUBLE);
motor2.step(430, BACKWARD, DOUBLE);
does: run motor1 for 430 steps and do nothing else than create the 430 step-pulses until the 430th-pulse is created
after the 430 pulses for motor 1 have been created
after that
do:
run motor2 for 430 steps and do nothing else than create the 430 step-pulses until the 430th-pulse has been created
The code that user @johnwasser posted
johnwasser:
for (int i = 0; i < 430; i++)
{
motor1.step(1, BACKWARD, DOUBLE);
motor2.step(1, BACKWARD, DOUBLE);
}
does the following
motor1: create one single step then finsish function motor1.step(1, BACKWARD, DOUBLE)
motor2: create one single step then finsish function motor2.step(1, BACKWARD, DOUBLE)
and this pattern
create one step for motor1
directly after that
create one step for motor2
is repeated 430 times through the for-loop
best regards Stefan
Donfero
November 13, 2022, 8:16pm
5
This is what I did, but the motors are no longer moving;
#include <AFMotor.h>
int start = 0;
// Number of steps per output rotation
const int stepsPerRevolution = 200;
const int stepsPerRevolution1 = 200;
AF_Stepper motor1(stepsPerRevolution, 1);
AF_Stepper motor2(stepsPerRevolution, 2);
void setup() {
Serial.begin(9600);
Serial.println("Stepper test!");
motor1.setSpeed(85); // 85 rpm
motor2.setSpeed(85); // 85 rpm
for (int i = 0; i < 430; i++);
}
void swip()
{
motor1.step(1, BACKWARD, DOUBLE);
motor2.step(1, BACKWARD, DOUBLE);
delay(500);
motor1.step(1, FORWARD, DOUBLE);
motor2.step(1, FORWARD, DOUBLE);
start++;
}
void loop() {
if (start == 0)
swip();
}
This doesn't do anything by itself.
If you want to step 430 steps instead of 1 you have to put loops around:
motor1.step(1, BACKWARD, DOUBLE);
motor2.step(1, BACKWARD, DOUBLE);
and
motor1.step(1, FORWARD, DOUBLE);
motor2.step(1, FORWARD, DOUBLE);
LarryD
November 13, 2022, 8:43pm
7
Also, not in this case, be careful placement of
;
for (int i = 0; i < 430; i++);
this code
does count up variable i from 0 to 429 and thats all
.
.
.
.
.
.
this code
for (int i = 0; i < 430; i++) {
// repeat 430 times
motor1.step(1, BACKWARD, DOUBLE); // drive motor 1 step
motor2.step(1, BACKWARD, DOUBLE); // drive motor 1 step
}
does count up variable i from 0 to 429 and
additionally
create
single
step-pulses
to make it more visible for you
for (int i = 0; i < 430; i++) {
// repeat 430 times
motor1.step(5, BACKWARD, DOUBLE); // drive motor1 5 steps
motor2.step(5, BACKWARD, DOUBLE); // drive motor2 5 step
}
motor1.step(5 , BACKWARD, DOUBLE); // drive motor1 5 steps
motor2.step(5 , BACKWARD, DOUBLE); // drive motor2 5 step
You should start reading code very slow and very carefully instead overflying the code at supersonic speed
Donfero
November 14, 2022, 10:19am
9
Please tell me what I'm doing wrong. My programming skills are very limited.
#include <AFMotor.h>
int i = 0;
// Number of steps per output rotation
const int stepsPerRevolution = 200;
const int stepsPerRevolution1 = 200;
AF_Stepper motor1(stepsPerRevolution, 1);
AF_Stepper motor2(stepsPerRevolution, 2);
void setup() {
Serial.begin(9600);
Serial.println("Stepper test!");
motor1.setSpeed(85); // 85 rpm
motor2.setSpeed(85); // 85 rpm
for (int i = 0; i < 430; i++) // repeat 430 times
motor1.step(5, BACKWARD, DOUBLE); // drive motor1 5 steps
motor2.step(5, BACKWARD, DOUBLE); // drive motor2 5 step
}
void swip()
{
Serial.println("Double coil steps");
motor1.step(5, BACKWARD, DOUBLE);
motor2.step(5, BACKWARD, DOUBLE);
i++;
}
void loop() {
if (i = 0)
swip();
}
baljo
November 14, 2022, 10:51am
10
Cannot help with other things, but isn't this
for (int i = 0; i < 430; i++) // repeat 430 times
motor1.step(5, BACKWARD, DOUBLE); // drive motor1 5 steps
motor2.step(5, BACKWARD, DOUBLE); // drive motor2 5 step
supposed to be this (like @StefanL38 showed)? Notice the curly braces { } ?
for (int i = 0; i < 430; i++) {
// repeat 430 times
motor1.step(5, BACKWARD, DOUBLE); // drive motor1 5 steps
motor2.step(5, BACKWARD, DOUBLE); // drive motor2 5 step
}
Yes I will tell you:
You should improve your programming-knowledge by reading an easy to understand tutorial.
I recommend this one:
Take a look into this tutorial:
Arduino Programming Course
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.
Whenever you have questions about a line of code in this tutorial for sure you can ask all these questions here.
best regards Stefan
system
Closed
May 13, 2023, 4:47pm
13
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.