用arduino和USART HMI触摸屏做控制电机的实验,触摸屏发送一个信号给arduino板,然后电机运行。程序如下
#define STEPPIN 6
#define DIRPIN 7
void setup(){
pinMode(STEPPIN, OUTPUT);
pinMode(DIRPIN, OUTPUT);
Serial.begin(9600);
}
void loop(){
char cha;
if(Serial.available())
{
cha=Serial.read();
Serial.println(cha);
}
if(cha=='a')
{
Serial.println("Forward Begins");
digitalWrite(DIRPIN, HIGH);
// 正向转1圈(200脉冲)
for (int x = 0; x < 2048; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(500);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(500);
}
Serial.println("Forward Ends");
delay(1000); // Delay for one second
Serial.println("Backward Begins");
digitalWrite(DIRPIN, LOW);
// 反向转3圈(600脉冲)
for (int x = 0; x < 2048; x ++) {
digitalWrite(STEPPIN, HIGH);
delayMicroseconds(500);
digitalWrite(STEPPIN, LOW);
delayMicroseconds(500);
}
Serial.println("Backward Ends");
delay(2000); //Delay for two seconds
}
else
;
delay(200);
}
在串口监视器直接输入a,程序只运行一遍就会停止。
file:///C:\Users\7SENSES\Documents\Tencent Files\2504551296\Image\C2C\IGD2DL0$_30`X59AE~_ATJH.png
file:///C:\Users\7SENSES\Documents\Tencent Files\2504551296\Image\C2C\NW$Q%CME3_T]D9{`45SK5XT.png
在触摸屏输入a,程序会一直循环,请问大神们这怎么解决啊。
file:///C:\Users\7SENSES\Documents\Tencent Files\2504551296\Image\C2C\E4AC48DFABD57F23AE912759E6662559.jpg
file:///C:\Users\7SENSES\Documents\Tencent Files\2504551296\Image\C2C\O~9SDTCGW9SEFJ%3CTTJI}T.png
|