微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在arduino中用蓝牙闪烁led的问题

如何解决在arduino中用蓝牙闪烁led的问题

我正在尝试制作一个程序,该程序可以在蓝牙的帮助下打开、关闭和闪烁 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;
        
      }
    
      }
    }

解决方法

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

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

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

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

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

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

同样,跟踪代码流非常棘手。我建议你把你的代码分成函数,以使其更具可读性。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?