Неисправность шагового двигателя с Arduino - PullRequest
0 голосов
/ 24 ноября 2018

У меня есть шаговый двигатель (драйвер pololu 4988), управляемый с помощью Arduino, он посылает сигналы на сцену в Unity.Двигатель должен вращать (по часовой стрелке или против часовой стрелки) легкую полистирольную основу.У меня проблемы, потому что иногда он не вращается с достаточно сильным (очень очень медленно) или вращается только в одном направлении.Я прилагаю код Arduino.

#include <SerialCommand.h>
#include <SoftwareSerial.h>
#define DEBUG(a) Serial.println(a);
const int dirPin = 2;
const int stepPin = 3;
float right = 0;
float left = 0;
const int steps = 200;
int stepDelay;


void setup() {
  // Mark pins as OUT
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    int data = Serial.parseInt();
    Serial.print(data);
    DEBUG(data);
    if (data > 1 ) {
      right = data;
      //Change direction and increase speed
      digitalWrite(dirPin, HIGH);
      stepDelay = 600;
      //Turn 400 steps to complete 2 turns
      for (int x = 0; x < right; x++) {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(stepDelay);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(stepDelay);
      }
    }

    if (data < 0) {
      left = data;
      left *=-1;
      digitalWrite(dirPin, LOW);
      stepDelay = 600;
      // Turn 200 steps to complete 2 turns
      for (int x = 0; x < left; x++) {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(stepDelay);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(stepDelay);
      }
    }
  }}

  /*//Activate a dirección and lock a speed with stepDelay
    digitalWrite(dirPin, HIGH);
    stepDelay = 600;
    // Turn 200 steps to complete 2 turns
    for (int x = 0; x < left; x++) {
     digitalWrite(stepPin, HIGH);
     delayMicroseconds(stepDelay);
     digitalWrite(stepPin, LOW);
     delayMicroseconds(stepDelay);
    }
    delay(1000);

    //Change direction and increase speed
    digitalWrite(dirPin, LOW);
    stepDelay = 600;
    // Turn 400 steps to complete 2 turns
    for (int x = 0; x < right; x++) {
     digitalWrite(stepPin, HIGH);
     delayMicroseconds(stepDelay);
     digitalWrite(stepPin, LOW);
     delayMicroseconds(stepDelay);
    }
    delay(1000);
  */

Спасибо!

...