Arduino的步进电机速度控制

时间:2018-07-06 17:28:12

标签: c++ arduino arduino-uno arduino-ide stepper

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

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

1 个答案:

答案 0 :(得分: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);
}