Uno 控制 6 个伺服电机通过 RX 引脚获取命令。这是我的代码的初始部分。这些错误是什么意思?如何解决错误?
#include <SoftwareSerial.h>
#include <Servo.h>
Servo servo_1;
Servo servo_2;
Servo servo_3;
Servo servo_4;
Servo servo_5;
Servo servo_6;
SoftwareSerial bluetooth(RX_PIN,TX_PIN);
int RX_PIN= 0;
int TX_PIN= 1;
int motornumber;
int pos=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
bluetooth.begin(4800);
servo_1.attach(3);
servo_2.attach(5);
servo_3.attach(6);
servo_4.attach(9);
servo_5.attach(10);
servo_6.attach(11);
}
void loop()
{
// put your main code here, to run repeatedly:
if(bluetooth.available())
{
motornumber =Serial.write(bluetooth.read());
switch(motornumber)
{
case '7':{ //7 for servo_1,1 for servo_2,2 for servo_3,3 for servo_4,4 for servo_5,5 for servo_6.
for(pos=0; pos<=180; pos++)
{
servo_1.write(pos);
delay(800);
}
for(pos=180; pos>=0; pos--)
{
servo_1.write(pos);
bluetooth.print(9);
}
break;
}
}
}
}
P.S:对于通过 RX 读取数据,bluetooth.read()
合适还是 Serial.write(bluetooth.read())
?
最佳答案
要解决“RX not declared in this scope”的错误,你应该声明你的变量
int RX_PIN= 0;
int TX_PIN= 1;
在这里使用它们之前
SoftwareSerial bluetooth(RX_PIN,TX_PIN);
结果:
/*some lines here*/
Servo servo_1;
Servo servo_2;
Servo servo_3;
Servo servo_4;
Servo servo_5;
Servo servo_6;
int RX_PIN= 0;
int TX_PIN= 1;
SoftwareSerial bluetooth(RX_PIN,TX_PIN);
int motornumber;
int pos=0;
/*code continues here*/
对于“duplicate case value”错误,您应该正确缩进您的代码,您可能会看到重复的 case。
关于c++ - 我正在研究 Arduino Uno。错误 : "' RX' not declared in the scope ."and "duplicate case value"for switch case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29437068/