4835| 4
|
[求助] arduino驱动57步进电机问题 |
就是我想通过串口收到的字符来控制电机的正反转和停止,请大神们看看程序该怎么改,哪里错了,收到1和2都可以转,但是只转动几秒就停了,发送3的时候也没有打印停止到串口,我想让它一直转,收到1就一直正转,收到2 就一直反转,收到3就停止 int PUL_1=7; int DIR_1=6; int val; void setup() { Serial.begin(9600);//设置波特率为9600 pinMode(DIR_1,OUTPUT); pinMode(PUL_1,OUTPUT); } void loop() { val=Serial.read();//读取串口收到的数据 if (val>'0'&& val<'3') { if(val== '1') { digitalWrite(DIR_1,HIGH); //电机正转 Serial.println("正转"); for(int a=0;a<1000;a++) { digitalWrite(PUL_1,HIGH); delayMicroseconds(400); digitalWrite(PUL_1,LOW); delayMicroseconds(400); } } else if(val=='2') { digitalWrite(DIR_1,LOW); //电机反转 Serial.println("反转"); for(int a=0;a<1000;a++) { digitalWrite(PUL_1,HIGH); delayMicroseconds(400); digitalWrite(PUL_1,LOW); delayMicroseconds(400); } } else if(val== '3') { digitalWrite(PUL_1,LOW); Serial.println("停止"); } } } |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed