当我们需要在程序中播放语音内容时,就需要使用到语音模块,今天我们就来实验一下使用OPT语音模块来方法语音。
- const int voicePin = 5;
- const int voiceBusyPin = 18;
- const int testLEDPin = 2;
-
- unsigned long pmillis = 0;
-
- int busyVal = 0; // 默认高-空闲
-
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- pinMode(voicePin, OUTPUT);
- pinMode(voiceBusyPin, INPUT);
- pinMode(testLEDPin, OUTPUT);
- digitalWrite(voicePin , HIGH);
- delay(300);
-
- digitalWrite(testLEDPin , HIGH);
-
- // 组合语音播放 方法二
- CVPlay(8, 74, 34, 36, 29, 39, 34, 40, 61); //设置播放语音内容
- delay(10000);
- sendData(0xFE); // 停止播放
- delay(50);
- }
-
- void loop() {
- // put your main code here, to run repeatedly:
- }
-
- // 发送数据-无起始位
- void cSendData(int addr) {
- for (int i = 0; i < 8; i++) {
- digitalWrite(voicePin, HIGH);
- if (addr & 1) {
- delayMicroseconds(2400); // >2400us
- digitalWrite(voicePin, LOW);
- delayMicroseconds(800); // >800us
- } else {
- delayMicroseconds(800); // >800us
- digitalWrite(voicePin , LOW);
- delayMicroseconds(2400); // >2400us
- }
- addr >>= 1;
- }
- digitalWrite(voicePin, HIGH);
- }
-
-
- void CVPlay(byte cNotes, ...) {
- va_list ap;
- unsigned int uAddr;
- int checksum = 0;
- va_start(ap, cNotes);
- while (cNotes > 0 ) {
- Serial.println(cNotes);
- uAddr = va_arg(ap, unsigned int);
- sendData(uAddr);
- delay(100);
- while (!digitalRead(voiceBusyPin)) {} // busy信号低 - 忙
- cNotes--;
- }
- va_end(ap);
- }
这里使用OPT语音模块,模块中内置里一些语音文字,通过使用文字组合提前设置好写到程序中,即可播放自己需要的语音内容。需要使用的文字根据模块提供的词语表进行对照。
OPT模块有4个针脚,其中2个是电源,另外一个数据,一个是busy。
接通好电路后,编译上传后即可听到设置的语音内容:“现在时刻:12:56分。”
通过以上实验,我们可以知道使用OPT模块可以进行语音播报,但是这个需要播放的内容是需要提前知道并且设置到程序中,而比如想要播放一个变量时则这种方法就不行了,后续我们再继续研究。