gpt4 book ai didi

c++ - 2wd 蓝牙控制汽车无法停止的问题

转载 作者:行者123 更新时间:2023-11-28 04:15:22 28 4
gpt4 key购买 nike

所以我正在为 2wd 蓝牙控制的足球车创建代码。当我向前按时,它会不停地向前移动。

我试过多次更改代码,例如:

if( val == '1') // Forward
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
delay(1);
digitalWrite(m1a, LOW);
digitalWrite(m2a, LOW);
}

收件人:

if( val == '1') // Forward
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
delay(200);
digitalWrite(m1a, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2b, LOW);
}

完整代码如下:

// Starting of Program
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11);
int m1a = A3;
int m1b = A4;
int m2a = 3;
int m2b = A5;
char val;

void setup()
{
pinMode(m1a, OUTPUT); // Digital pin 10 set as output Pin
pinMode(m1b, OUTPUT); // Digital pin 11 set as output Pin
pinMode(m2a, OUTPUT); // Digital pin 12 set as output Pin
pinMode(m2b, OUTPUT); // Digital pin 13 set as output Pin
mySerial.begin(9600);
}

void loop()
{
while (mySerial.available() > 0)
{
val = mySerial.read();
mySerial.println(val);
}

if( val == '1') // Forward
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
delay(1);
digitalWrite(m1a, LOW);
digitalWrite(m2a, LOW);
}
else if(val == '2') // Backward
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
delay(200);
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
}

else if(val == '3') //Left
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, HIGH);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
delay(200);
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
}
else if(val == '4') //Right
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, HIGH);
delay(200);
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
}

else if(val == 'S')
{
digitalWrite(m1a, LOW);
digitalWrite(m1b, LOW);
digitalWrite(m2a, LOW);
digitalWrite(m2b, LOW);
delay(1000);
digitalWrite(m2a, HIGH);
}
}

// End of program

没有错误按摩请帮忙....我正在使用 this app在我的手机和最后 11 行代码中,(val == 'S') 用于应用程序中的停止按钮。

我需要它只在按下按钮时移动,在没有按下任何按钮时停止。它不需要停止按钮。

最佳答案

if( val == '1') // Forward
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
delay(1);
digitalWrite(m1a, LOW);
digitalWrite(m2a, LOW);
}

if( val == '1') // Forward
{
digitalWrite(m1a, HIGH);
digitalWrite(m1b, LOW);
digitalWrite(m2a, HIGH);
digitalWrite(m2b, LOW);
delay(1);
digitalWrite(m1a, LOW);
digitalWrite(m2a, LOW);
val = ' ';

}

关于c++ - 2wd 蓝牙控制汽车无法停止的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56767642/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com