Arduino蓝牙控制代码执行两次



我使用Arduino蓝牙控制器应用程序与HC-06相结合,并配置应用程序一旦按下按钮就发送' a '到串行监视器,当你松开按钮时发送'0'。目标是保持伺服的移动,直到你放开按钮。这是有效的,除了当你只快速点击按钮;代码总是执行两次。这是一个问题,因为在这种情况下,能够只做一个动作是很重要的。我可以在代码中更改什么以确保代码只执行一次?

提前感谢。

下面的代码按预期工作,除非您希望while循环中的代码只通过点击按钮并立即释放来执行一次。我已经尝试在最后一次cha = Serial.read();之后增加延迟,但这没有帮助。

#include <Servo.h>
Servo servo1U;
Servo servo1L;
Servo servo2U;
Servo servo2L;
Servo servo3U;
Servo servo3L;
Servo servo4U;
Servo servo4L;

int neutral = 90;
int front_back = 0; //1U is omgekeerd, dus 180 - front_back
int front_right = 0; //1U is omgekeerd, dus 180 - front_right
int front_left = 180;
int back_forward = 180; //4U is omgekeerd, dus 180 - back_forward
int back_right = 180; //4U is omgekeerd, dus 180 - back_right
int back_left = 0;
int up = 90; //2L en 4L zijn omgekeerd, dus 180 - up
int down = 170; //2L en 4L zijn omgekeerd dus 180 - down
int small_right = 70;
int small_left = 110;
int state = 0;
void setup() {
Serial.begin(9600);
servo1U.attach(9);
servo1L.attach(2);
servo2U.attach(3);
servo2L.attach(4);
servo3U.attach(5);
servo3L.attach(6);
servo4U.attach(7);
servo4L.attach(8);
servo1U.write(neutral);
servo2U.write(neutral);
servo3U.write(back_forward);
servo4U.write(180 - back_forward);
servo1L.write(down);
servo2L.write(180 - down);
servo3L.write(down);
servo4L.write(180 - down);

}
char cha;
void loop() {
if (Serial.available() > 0)
{
cha = Serial.read();
delay(5);

if (cha == 'A'){ //Up arrow button on phone
while (cha != '0'){
delay(500);
servo1U.write(180 - front_back);
servo2U.write(front_back);
servo3U.write(neutral);
servo4U.write(neutral);
delay(500);

servo1L.write(up);
servo2L.write(180 - up);
servo3L.write(up);
servo4L.write(180 - up);

delay(500);
servo1U.write(neutral);
servo2U.write(neutral);
servo3U.write(back_forward);
servo4U.write(180 - back_forward);
delay(500);
servo1L.write(down);
servo2L.write(180 - down);
servo3L.write(down);
servo4L.write(180 - down);

cha = Serial.read();
delay(500);
}}
}
}

我对你的嵌套循环有点困惑,所以我改变了一点,这应该做完全相同的事情(未经测试):

#include <Servo.h>
Servo servo1U;
Servo servo1L;
Servo servo2U;
Servo servo2L;
Servo servo3U;
Servo servo3L;
Servo servo4U;
Servo servo4L;

int neutral = 90;
int front_back = 0; //1U is omgekeerd, dus 180 - front_back
int front_right = 0; //1U is omgekeerd, dus 180 - front_right
int front_left = 180;
int back_forward = 180; //4U is omgekeerd, dus 180 - back_forward
int back_right = 180; //4U is omgekeerd, dus 180 - back_right
int back_left = 0;
int up = 90; //2L en 4L zijn omgekeerd, dus 180 - up
int down = 170; //2L en 4L zijn omgekeerd dus 180 - down
int small_right = 70;
int small_left = 110;
int state = 0;
void setup() {
Serial.begin(9600);
servo1U.attach(9);
servo1L.attach(2);
servo2U.attach(3);
servo2L.attach(4);
servo3U.attach(5);
servo3L.attach(6);
servo4U.attach(7);
servo4L.attach(8);
servo1U.write(neutral);
servo2U.write(neutral);
servo3U.write(back_forward);
servo4U.write(180 - back_forward);
servo1L.write(down);
servo2L.write(180 - down);
servo3L.write(down);
servo4L.write(180 - down);

}
char lastCharacter;
void loop() {
if (Serial.available() > 0)
{
lastCharacter = Serial.read();
delay(5);
}

if (lastCharacter == 'A') //Up arrow button on phone
{
delay(500);
servo1U.write(180 - front_back);
servo2U.write(front_back);
servo3U.write(neutral);
servo4U.write(neutral);
delay(500);

servo1L.write(up);
servo2L.write(180 - up);
servo3L.write(up);
servo4L.write(180 - up);

delay(500);
servo1U.write(neutral);
servo2U.write(neutral);
servo3U.write(back_forward);
servo4U.write(180 - back_forward);
delay(500);
servo1L.write(down);
servo2L.write(180 - down);
servo3L.write(down);
servo4L.write(180 - down);
delay(500);
}
}

不确定这是否已经解决了您的问题,但我看到了一个严重的缺陷:

通过串行通信的按钮的当前状态只有在伺服运动完成后才会被检查。所以现在如果你在伺服器还在移动的时候发送多个字符它们会被"保存"并将被一个接一个地阅读。例如:

  1. 发送A,电机开始运动
  2. 您发送0
  3. 您发送A
  4. 您发送0
  5. 电机完成移动
  6. 下一个要读取的字符是0,没有移动发生
  7. 下一个字符是a,电机开始移动

编辑:

上述剩余的问题应该通过替换

来解决
if (Serial.available() > 0)
{
lastCharacter = Serial.read();
delay(5);
}

while (Serial.available() > 0)
{
lastCharacter = Serial.read();
delay(5);
}

这种方式读取在一个循环中发送的所有字符,只使用最后一个字符。我不确定在这种情况下是否还需要delay

相关内容

  • 没有找到相关文章

最新更新