Bluetooth模块(HC-05)和语音识别模块(V 3.1)可以与Arduino Uno一起正常工作,但是当我尝试同时运行它们时,其中之一停止工作,这就是语音模块停止工作。我一直无法弄清楚为什么。我的代码如下:
# include <SoftwareSerial.h>
#include "VoiceRecognitionV3.h"
VR myVR(2,3); // 2:RX 3:TX
uint8_t buf[64];
SoftwareSerial bluetooth(4,5); //for Bluetooth module RX FOLLOWD BY TX
void printSignature(uint8_t *buf, int len) {
int i;
for (i = 0; i < len; i++) {
if (buf[i] > 0x19 && buf[i] < 0x7F) {
Serial.write(buf[i]);
}
}
}
void setup() {
pinMode(A4,OUTPUT);// for VCC of voice module
digitalWrite(A4,HIGH);
myVR.begin(9600);
Serial.begin(9600);
myVR.load((uint8_t)0); //loading the data of voice module
myVR.load((uint8_t)1);
myVR.load((uint8_t)2);
myVR.load((uint8_t)3);
myVR.load((uint8_t)4);
bluetooth.begin(9600);
}
String one;
void loop() {
int ret = myVR.recognize(buf, 50);
if (bluetooth.available() > 0) {
one = bluetooth.readString();
Serial.println(one);
}
if (ret > 0) {
Serial.print("Voice module said ");
printSignature(buf+4, buf[3]); //priting the signature of command
Serial.println(""); //for new line
}
}
1)问题出在通信中,因为当我删除myVR.begin(9600)时,蓝牙可以正常工作,而语音模块则无法工作。
2)当我删除bluetooth.begin(9600)时,语音模块可以工作,而蓝牙不能。
他们一起不工作,只有一个工作。
答案 0 :(得分:1)
VoiceRecognitionV3
继承了SoftwareSerial
。一个程序中可能有多个SoftwareSerial
实例。但是,一次只能接收一个数据。因此,您想使用listen()
(doc)在端口之间切换。
portOne.listen();
ret = portOne.read();
portTwo.listen();
ret = portTwo.read();
Arduino站点有一个示例。 https://www.arduino.cc/en/Tutorial/TwoPortReceive