c++ - 我正在研究 Arduino Uno。错误 : "' RX' not declared in the scope ."and "duplicate case value"for switch case

标签 c++ bluetooth arduino arduino-uno

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/

相关文章:

java - 是否可以在android中建立多个蓝牙通信

使用 Arduino 的 Python 脚本无法正确读取数据

c++ - Arduino WiFi101 库 - 将 WiFi 和 WiFiClient 传递给子类

c++ - 错误 : expected primary-expression before '<' token : arduino RGB dancing lights

c++ - 定义一个成员类后重新声明它合法吗?

c++ - Direct3D 和 iPhone 加速度计矩阵

c# - 使用 32feet.net 使用独特的服务协议(protocol)连接到 BTLE 设备

python - 不匹配 Ubuntu 12.10 上自动生成的 PIN 的蓝牙 bluez 配对

从命令行启动时,在 for 循环期间不显示 C++ cout 语句

c++ - G++ 错误 : In file included, 然后未声明 Foo