[英]How do I get two stepper motors to rotate at the exact same time and in the same direction using only one Arduino Uno?
My current code: 我当前的代码:
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
Stepper myStepper(stepsPerRevolution, 3, 4, 5, 6);
Stepper secondStepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
secondStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
Serial.println("clockwise");
secondStepper.step(stepsPerRevolution);
delay(500);
}
I was working on a project involving the above code during a hackathon this past weekend, but I couldn't get the two motors to move simultaneously. 在过去的一个周末的黑客马拉松中,我正在从事一个涉及上述代码的项目,但是我无法让两个电机同时运动。 I was wondering if anyone on here knew how to do it properly so that I may be better prepared in the future. 我想知道这里的人是否知道如何正确地做,以便将来有更好的准备。
I'm using the Arduino IDE, if that matters. 如果这很重要,我正在使用Arduino IDE。
You need a non-blocking step (or start) call that starts the motion and returns immediately. 您需要一个非阻塞步骤(或开始)调用,以开始运动并立即返回。 After starting the two motions, you would then wait for an asynchronous callback from each motor indicating that the motion has completed. 在开始两个动作之后,您将等待每个电机的异步回调,指示动作已完成。 One thread per motor would work in a straightforward manner. 每个电机一个线程将以一种简单的方式工作。 The main thread would wait for both motor threads to complete before proceeding. 在继续之前,主线程将等待两个电动机线程完成。
是的,您可以并联两个电动机,以在Ramps 1.4板上控制z轴!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.