2

我正在构建一个小项目,根据人按住开关的时间移动步进电机,时间越长,速度越高。我使用的代码提高了速度,我可以在串行监视器上看到它,但步进电机没有改变。我想知道是否可以在不使用电位器的情况下改变步进器的速度以及如何改变?

#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);
}
4

1 回答 1

1

对于任何来这里的人,我已经找到了解决方案。看起来双极步进电机只是以特定速度 {80,100, 160, 180, 200,220, 255} 移动,而我的代码在 0-255 范围内,所以我只是根据时间更改了代码以达到特定速度开关被按下!谢谢你的帮助xD

#include <Stepper.h>

/*
  const int ForwardLimitSwitchPin = 2;
  const int ReverseLimitSwitchPin = 3;
  const int StepperStepPin = 4;
  const int StepperDirection = 5;
*/
const int StepperMaxRPM = 255; //default = 100

const int swDireita = 13;
const int swEsquerda = 12;
int velMotor = 80;

static const unsigned long REFRESH_INTERVAL = 1000; //ms
static unsigned long lastRefreshTime = 0;

//Stepper stepper(200, StepperStepPin, StepperDirectionPin);
Stepper stepper(200, 8, 9, 10, 11);

void setup() {
  Serial.begin(115200);

  pinMode(swDireita, INPUT);
  pinMode(swEsquerda, INPUT);

  digitalWrite(swDireita, HIGH);
  digitalWrite(swEsquerda, HIGH);

  stepper.setSpeed(StepperMaxRPM);
}

void loop() {
  while (digitalRead(swDireita) == 0) {
    motorDireita();
  }
  while (digitalRead(swEsquerda) == 0) {
    motorEsquerda();
  }
  while (digitalRead(swEsquerda) == 1 && digitalRead(swDireita) == 1) {
    Serial.print("to aqui");
    velMotor = 0;
    stepper.setSpeed(velMotor);
    Serial.print(velMotor);
  }
}

void motorDireita() {
  if (millis() - lastRefreshTime >= REFRESH_INTERVAL) {
    lastRefreshTime += REFRESH_INTERVAL;
    switch (velMotor) { //80//100//160//180//200//220//255
      case 0:
        Serial.print("antes: ");
        Serial.print(velMotor);
        velMotor = 80;
        stepper.setSpeed(velMotor);
        break;
      case 80:
        velMotor = 100;
        stepper.setSpeed(velMotor);
        break;
      case 100:
        velMotor = 160;
        stepper.setSpeed(velMotor);
        break;
      case 160:
        velMotor = 180;
        stepper.setSpeed(velMotor);
        break;
      case 180:
        velMotor = 200;
        stepper.setSpeed(velMotor);
        break;
      case 200:
        velMotor = 220;
        stepper.setSpeed(velMotor);
        break;
      case 220:
        velMotor = 255;
        stepper.setSpeed(velMotor);
        break;
    }
  }
  Serial.print(velMotor);
  Serial.print("\n");
  stepper.step(1);
}

void motorEsquerda() {
  if (millis() - lastRefreshTime >= REFRESH_INTERVAL) {
    lastRefreshTime += REFRESH_INTERVAL;
    switch (velMotor) { //80//100//160//180//200//220//255
      case 0:
        Serial.print("antes ");
        Serial.print(velMotor);
        velMotor = 80;
        stepper.setSpeed(velMotor);
        break;
      case 80:
        Serial.print("antes ");
        Serial.print(velMotor);
        velMotor = 100;
        stepper.setSpeed(velMotor);
        break;
      case 100:
        Serial.print("antes ");
        Serial.print(velMotor);
        velMotor = 160;
        stepper.setSpeed(velMotor);
        break;
      case 160:
        Serial.print("antes ");
        Serial.print(velMotor);
        velMotor = 180;
        stepper.setSpeed(velMotor);
        break;
      case 180:
        Serial.print("antes ");
        Serial.print(velMotor);
        velMotor = 200;
        stepper.setSpeed(velMotor);
        break;
      case 200:
        Serial.print("antes ");
        Serial.print(velMotor);
        velMotor = 220;
        stepper.setSpeed(velMotor);
        break;
      case 220:
        Serial.print("antes ");
        Serial.print(velMotor);
        velMotor = 255;
        stepper.setSpeed(velMotor);
        break;
    }
  }

  Serial.print(velMotor);
  Serial.print("\n");
  stepper.step(-1);
}
于 2018-07-11T13:28:27.327 回答