【问题标题】:Problem with blinking a led with bluetooth in arduino在arduino中用蓝牙闪烁LED的问题
【发布时间】:2021-04-27 19:21:03
【问题描述】:

我正在尝试制作一个在蓝牙的帮助下打开、关闭和闪烁 LED 的程序 On 和 of 很容易复制,但我无法让眨眼工作。有选项可以眨眼一次,或者如果我广告一段时间,它永远不会停止循环。我尝试了 if 和 case。有人可以吗帮帮我。我正在使用 esp32。 带有 if 的代码:

    #include "BluetoothSerial.h"
    #include <Arduino.h>
    #include <analogWrite.h>
    
    #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
    #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
    #endif
    
    BluetoothSerial SerialBT;
    int received;// received value will be stored in this variable
    char receivedChar;// received value will be stored as CHAR in this variable
    
    const char turnON ='a';
    const char turnOFF ='b';
    const char turnBLINK= 'c';
    //const char turnFADE='d';
    const int LEDpin = 12;
    //int brightStep = 1;
    //int brightness = 0;
    
    
    void setup() {
      Serial.begin(115200);
      SerialBT.begin("Mono"); //Bluetooth device name
      Serial.println("The device started, now you can pair it with bluetooth!");
      Serial.println("To turn ON send: a");//print on serial monitor  
      Serial.println("To turn OFF send: b"); //print on serial monitor 
      pinMode(LEDpin, OUTPUT);
     // analogWriteResolution(LEDpin, 12);
     
    }
    
    void loop() {
        receivedChar =(char)SerialBT.read();
    
      if (Serial.available()) {
        SerialBT.write(Serial.read());
      
      }
      if (SerialBT.available()) {
       // while(SerialBT.available()){
      //  receivedChar =(char)SerialBT.read();
      //  }
        
        SerialBT.print("Received:");// write on BT app
        SerialBT.println(receivedChar);// write on BT app      
        Serial.print ("Received:");//print on serial monitor
        Serial.println(receivedChar);//print on serial monitor    
        //SerialBT.println(receivedChar);//print on the app    
        //SerialBT.write(receivedChar); //print on serial monitor
        if(receivedChar == turnON)
        {
         SerialBT.println("LED ON:");// write on BT app
         Serial.println("LED ON:");//write on serial monitor
         digitalWrite(LEDpin, HIGH);// turn the LED ON
           
        }
        if(receivedChar == turnOFF)
        {
         SerialBT.println("LED OFF:");// write on BT app
         Serial.println("LED OFF:");//write on serial monitor
          digitalWrite(LEDpin, LOW);// turn the LED off 
        }    
          if(receivedChar == turnBLINK)
        {
         SerialBT.println("LED blink:");// write on BT app
         Serial.println("LED blink:");//write on serial monitor
         while (receivedChar == turnBLINK){
          //receivedChar =(char)SerialBT.read();
           //if(receivedChar != turnBLINK){
         // break;
          // } else {
          digitalWrite(LEDpin, HIGH);// turn the LED off 
          delay(1000);
          digitalWrite(LEDpin,LOW);
          delay(1000);
           if(receivedChar != turnBLINK){
                 break;    }  
    
     }
        }
      }
    delay(20);
    }

和大小写:

    #include "BluetoothSerial.h"
    #include <Arduino.h>
    #include <analogWrite.h>
    
    #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
    #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
    #endif
    
    BluetoothSerial SerialBT;
    
    int received;// received value will be stored in this variable
    char receivedChar;// received value will be stored as CHAR in this variable
    char data;
    int option;
    int blink=0;
    const int LEDpin = 12;
    
    void setup() {
    Serial.begin(115200);
      SerialBT.begin("Mono"); //Bluetooth device name
      Serial.println("The device started, now you can pair it with bluetooth!");
      Serial.println("To turn ON send: 1");//print on serial monitor  
      Serial.println("To turn OFF send: 0"); //print on serial monitor 
     pinMode(12, OUTPUT);
     }
    
    
    void loop(){
       receivedChar =(char)SerialBT.read();
        
     // if (Serial.available()) {
      //  SerialBT.write(Serial.read());
      
    //  }
      if (SerialBT.available()) {
       // while(SerialBT.available()){
      //  receivedChar =(char)SerialBT.read();
      //  }
        
        SerialBT.print("Received:");// write on BT app
        SerialBT.println(receivedChar);// write on BT app      
        Serial.print ("Received:");//print on serial monitor
        Serial.println(receivedChar);//print on serial monitor    
        //SerialBT.println(receivedChar);//print on the app    
        //SerialBT.write(receivedChar); //print on serial monitor
     
      data=receivedChar;
      if(data == '0')
      {
        option = 0;
        blink=0;
      }else if(data == '1')
      {
        option = 1;
        blink=0;
      }else if(data == '2')
      {
        option = 2;
        blink=1;
      }
      switch (option)
      {
        case 0: // LED OFF
          digitalWrite(LEDpin, LOW);
        break;
        case 1: //LED ON
          digitalWrite(LEDpin, HIGH);
        break;
        case 2:
        while(blink=1){// LED BLINK
          digitalWrite(LEDpin , HIGH);
          delay(200);
          digitalWrite(LEDpin, LOW);
          delay(200);
        }
          break;
        
      }
    
      }
    }

【问题讨论】:

    标签: arduino bluetooth esp32 led


    【解决方案1】:

    首先,您应该将代码清理为最小可重现的示例。删除所有不必要的 cmets 和不代表您面临的主要问题的代码。

    在快速浏览您的代码后,我立即注意到了这一点:

    while(blink=1){// LED BLINK
          digitalWrite(LEDpin , HIGH);
          delay(200);
          digitalWrite(LEDpin, LOW);
          delay(200);
     }
    

    应该在哪里while(blink==1){ } --> 很常见的错误。这应该是比较,而不是分配。

    现在,您提到它永远不会停止运行。即使在纠正了我刚才指出的错误之后,您的 while 循环中的哪一部分打破了 blink 的逻辑等于 1?否则,while循环永远不会停止

    最后,不要读取主loop()里面的串口数据。而是使用SerialEvent。

    同样,遵循代码流程非常棘手。我建议您将代码分成函数以使其更具可读性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多