0

Arduino Uno - HC-05

连接方式为:TX-RXRX-TXLED-D13、5V - 5V+

对于这个项目,我们可以通过任何 +5V 电源为 Arduino 供电。您可以使用计算机的 USB 端口为 Arduino 供电,但在这个项目中,我使用了笔记本电脑。

while (Serial.available())正在返回 0 并Serial.read()正在返回 -1。

需要帮忙!

使用playstore的蓝牙语音识别工具——“Arduino语音控制”

#include <SoftwareSerial.h> //Replace (' ') with (< >)

SoftwareSerial BLU(0,1);
String voice;
int Green = 13; //Connect To Pin #13
//int Yellow = 2; //Connect To Pin #2
//int Red = 3;    //Connect To Pin #3

void allon() {
  //digitalWrite(Red, HIGH);
  //digitalWrite(Yellow, HIGH);
  Serial.print("start");
  digitalWrite(Green, HIGH);
}

void alloff() {
  //digitalWrite(Red, LOW);
  //digitalWrite(Yellow, LOW);
  digitalWrite(Green, LOW);
}

void setup() {
  Serial.begin(9600);
  BLU.begin(9600);

  //pinMode(Red, OUTPUT);
  //pinMode(Yellow, OUTPUT);
  pinMode(Green, OUTPUT);
}

void loop() {
  //Serial.print("start loop");
  //Serial.print(Serial.available());
  while (Serial.available()) { //Check if there is an available byte to read
    //Serial.print("start");
    delay(10); //Delay added to make thing stable
    char c = Serial.read(); //Conduct a serial read
    //Serial.print(Serial.read());
    if (c == '#') {
      break; //Exit the loop when the # is detected after the word
    }
    //Serial.print(c);
    voice += c;
    //Serial.print(voice+"\n");
  }

  if (voice.length() > 0) {
    Serial.print("Start");
    Serial.print(voice);
    if (voice == "*turn on all LED") {
      allon();
    } 
    else if (voice == "*turn off all LED") {
      alloff();
    }
    /*else if(voice == "*switch on red") {
      digitalWrite(Red,HIGH);
    }
    else if(voice == "*switch on yellow") {
      digitalWrite(Yellow,HIGH);
    }*/
    else if(voice == "*switch on green") {
      digitalWrite(Green,HIGH);
    }
    /*else if(voice == "*switch off red") {
      digitalWrite(Red,LOW);
    }
    else if(voice == "*switch off yellow") {
      digitalWrite(Yellow,LOW);
    }*/
    else if(voice == "*switch off green") {
      digitalWrite(Green,LOW);
    }
    voice=""; //Reset variable
  }
}
4

1 回答 1

0

您需要先检查应用程序输出。如果您已经知道,请在评论中提及,否则请先执行以下操作以打印应用程序输出:-

const int LED = 5;

void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);

}

void loop() {
    while(Serial.available()>0){
     
    switchstate = Serial.read();
  
    Serial.print(switchstate); // First check what output are you getting from the application
    
    Serial.print("\n");
    
    delay(15);
   
    if(switchstate == '1'){ // Compare your app output accordingly 
     digitalWrite(5, HIGH);
    }
    else if(switchstate == '0'){
     digitalWrite(5, LOW);
    }
    }
}
于 2020-12-08T11:28:46.060 回答