我有一个由Arduino控制的步进电机(驱动器pololu 4988),它将信号发送到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);
*/
谢谢!