我正在构建一个小项目,根据人按住开关的时间移动步进电机,时间越长,速度越高。我使用的代码提高了速度,我可以在串行监视器上看到它,但步进电机没有改变。我想知道是否可以在不使用电位器的情况下改变步进器的速度以及如何改变?
#include <Stepper.h>
const int ForwardLimitSwitchPin = 2;
const int ReverseLimitSwitchPin = 3;
const int StepperStepPin = 4;
const int StepperDirectionPin = 5;
const int LimitSwitchActivated = LOW; // Limit switch grounds pin
const int StepperMaxRPM = 255;//default = 100
const int swDireita = 13;
const int swEsquerda = 12;
int velMotor = 0;
static const unsigned long REFRESH_INTERVAL = 1000; // ms
static unsigned long lastRefreshTime = 0;
boolean flag;
//Stepper stepper(200, StepperStepPin, StepperDirectionPin);
Stepper stepper(200, 8, 9, 10, 11);
void setup() {
pinMode(swDireita, INPUT);
pinMode(swEsquerda, INPUT);
digitalWrite(swDireita, HIGH);
digitalWrite(swEsquerda, HIGH);
pinMode(ForwardLimitSwitchPin, INPUT_PULLUP);
pinMode(ReverseLimitSwitchPin, INPUT_PULLUP);
stepper.setSpeed(StepperMaxRPM);
Serial.begin(115200);
}
void loop() {
while (digitalRead(swDireita) == 0) {
motorDireita();
}
velMotor = 0;
while (digitalRead(swEsquerda) == 0) {
motorEsquerda();
}
velMotor = 0;
while (digitalRead(swDireita) == 0 && digitalRead(swEsquerda) == 0) {
motorParado();
}
velMotor = 0;
}
void motorParado() {
Serial.print("Ambos ativados, parada");
Serial.print("\n");
stepper.setSpeed(0);
stepper.step(0);
}
void motorDireita() {
//51 por segundo para 5 seg total
/*if(!flag){
flag = true;
velMotor = 51;
}else */
if (velMotor < 255 && millis() - lastRefreshTime >= REFRESH_INTERVAL) {
lastRefreshTime += REFRESH_INTERVAL;
velMotor += 25;
}
//int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
//int motorSpeed = map(velMotor, 0, 1023, 0, 255);
Serial.print("direita ativada, velocidade: ");
Serial.print(velMotor);
//Serial.print(motorSpeed);
Serial.print("\n");
stepper.setSpeed(velMotor);
//stepper.setSpeed(motorSpeed);
stepper.step(1);
}
void motorEsquerda() {
//51 por segundo para 5 seg total
if (velMotor < 255 && millis() - lastRefreshTime >= REFRESH_INTERVAL) {
lastRefreshTime += REFRESH_INTERVAL;
velMotor += 51;
}
Serial.print("Esquerda ativida, velocidade: ");
Serial.print(velMotor);
Serial.print("\n");
stepper.setSpeed(velMotor);
stepper.step(-1);
}