我是 Arduino 编程的新手,花了几天时间才走到这一步,但似乎无法弄清楚为什么这段代码不起作用。
我正在尝试使用业余级遥控器 RX/TX 来控制步进电机。
我有一个 RC 接收器将 1000 到 2000 的模拟值发送到我的 Arduino。如果该信号是 1000,我想在步进器上设置 1000 = -360 度,在步进器上设置 2000 = +360 度。
我从接收器接收到正确的信号并将它们打印到串行监视器,但我似乎无法用这个值控制步进电机。该电机只是卡在第一个 while 循环中并继续沿 1 个方向旋转。
int ch1 = 0; // RC Reciever Channel Value
int ch1previous = 0; // RC Receiver Channel Previous Value
int PUL=7; //define Pulse pin of stepper driver
int DIR=6; //define Direction pin of stepper driver
int ENA=5; //define Enable Pin of stepper driver
void setup() {
pinMode (PUL, OUTPUT); // Stepper Driver Pulse Pin
pinMode (DIR, OUTPUT); // Stepper Driver Direction Pin
pinMode (ENA, OUTPUT); // Stepper Driver Enable Pin
pinMode(3, INPUT); // RC Reciever Pin Input
Serial.begin(9600);
}
void loop() {
ch1 = pulseIn(3, HIGH, 50000); // Read RC Reciever Channel Value
while ( ch1 > ch1previous) { // If CH1 is greater than CH1Previous run the differance
for ( int i = ch1; i < ch1previous; i++);{ // in steps to maintain the setpoint value in forwared position
digitalWrite(DIR,HIGH);
digitalWrite(ENA,HIGH); // pulsing stepper motor in forward motion
digitalWrite(PUL,HIGH);
delayMicroseconds(50);
digitalWrite(PUL,LOW);
delayMicroseconds(50);
}
}
while ( ch1 < ch1previous) { // if CH1 is less than CH1Previous run the differance
for ( int i = ch1; i<ch1previous; i--);{ // in steps to maintain the setpoint value in reverse motion
digitalWrite(DIR,LOW);
digitalWrite(ENA,HIGH); // pulsing stepper motor in reverse motion
digitalWrite(PUL,HIGH);
delayMicroseconds(50);
digitalWrite(PUL,LOW);
delayMicroseconds(50);
}
}
Serial.print ("Channel 1: "); // print text to the serial monitor
Serial.println(ch1); // print ch1 value to the serial monitor and end line
Serial.print("CH1 Previous: "); // print text to the serial monitor
Serial.println(ch1previous); // print ch1previous value to the serial monitor and end line
ch1previous = ch1; // remember the previous value from ch1
delay(500); // just to clean up the serial monitor
}