我的步进电机使用连接到Arduino的L293d驱动器进行控制。我从中获取信息的教程是https://learn.adafruit.com/wifi-control ... ino-sketch,但更改了代码以匹配步进电机而不是直流电机。目前,我只是测试串行输入 - 还没有无线。
我把一切都搞定了。当我按下前进按钮时,电机向前移动。但是,它只旋转一圈。我不知道如何更改代码,以便当我单击前进按钮时,它会持续向前旋转,直到我按下停止按钮。有谁知道怎么做?
#include <SPI.h>
#include <aREST.h>
#include <Stepper.h>
#include <Wire.h>
aREST rest = aREST();
int inA1 = 12; // input 1 of the stepper
int inA2 = 11; // input 2 of the stepper
int inB1 = 10; // input 3 of the stepper
int inB2 = 9; // input 4 of the stepper
#define STEPS 512
Stepper motor(STEPS, inA1, inA2, inB1, inB2);
void setup() {
pinMode(inA1, OUTPUT);
pinMode(inA2, OUTPUT);
pinMode(inB1, OUTPUT);
pinMode(inB2, OUTPUT);
Serial.begin(115200);
Serial.println(F("Hello, CC3000!\n"));
rest.set_id("1");
rest.set_name("robot");
//Expose functions
rest.function("forward",forward);
rest.function("backward",backward);
rest.function("stop",stop);
}
void loop()
{
rest.handle(Serial);
}
// Forward
int forward(String command) {
motor.setSpeed(10);
motor.step(100);
return 1;
}
//Backforward
int backward(String command) {
motor.setSpeed(-10);
motor.step(100);
return 1;
}
答案 0 :(得分:0)
失败:简单设置motor.step(2147483647)
。 2147483647是int的最大值。
新尝试:尝试实现下一个逻辑。我不是c-guru所以低于脏代码
int lastCommand = 0; // 0 - nothing; 1 - forward; 2 - backward; 3 - stop;
void forward () {
lastCommand = 1;
...
}
void backward () {
lastCommand = 2;
...
}
void stop() {
lastCommand = 0;
...
}
void loop() {
rest.handle(Serial);
if (lastCommand == 1)
forward();
if (lastCommand == 2)
backward();
}
如果我们向后推+向前+向后按钮并发送到Serial
3个命令,那么马达将一致地执行所有这些命令。这是一个问题。