本文首發(fā)于DF創(chuàng)客社區(qū),作者:Jeff2
原文鏈接:http://mc.dfrobot.com.cn/thread-289776-1-1.html
項(xiàng)目背景
之前和朋友做AI訓(xùn)練街霸游戲,想加入人工語(yǔ)音識(shí)別輸出特定技能或者連環(huán)招式,以增加游戲的趣味性。也聯(lián)想到目前某些游戲按鍵技能太多,經(jīng)常操作跟不上思想。于是就想著開(kāi)發(fā)一個(gè) 輸出全靠吼的硬件。說(shuō)盤(pán)就盤(pán)。
boom表
LD3320
arduino leonardo
杜邦線
面包板
軟件要求:
先下載ld3320 庫(kù)文件,并復(fù)制到arduino的libraries文件夾下。地址
http://www.waveshare.net/study/portal.php?mod=atta...
連接 LD3320 語(yǔ)音模塊到arduino leonardo
3.3V -- VCC
GND -- GND
MISO -- D12
MOSI -- D11
SCK -- D13
NSS -- D4
RST -- D9
IRQ -- D2
WR -- GND
先做個(gè)簡(jiǎn)單測(cè)試
#include <ld3320.h>
VoiceRecognition Voice;
//聲明一個(gè)語(yǔ)音識(shí)別對(duì)象
#define Led 8 //定義LED控制引腳
void
setup()
{
pinMode(Led,OUTPUT);
//初始化LED引腳為輸出模式
digitalWrite(Led,LOW);
//LED引腳低電平
Voice.init();
//初始化VoiceRecognition模塊
Voice.addCommand(
"kai deng"
,0);
//添加指令,參數(shù)(指令內(nèi)容,指令標(biāo)簽(可重復(fù)))
Voice.addCommand(
"guan deng"
,1);
//添加指令,參數(shù)(指令內(nèi)容,指令標(biāo)簽(可重復(fù)))
Voice.start();
//開(kāi)始識(shí)別
}
void
loop() {
switch
(Voice.read())
//判斷識(shí)別
{
case
0:
//若是指令“kai deng”
digitalWrite(Led,HIGH);
//點(diǎn)亮LED
break
;
case
1:
//若是指令“guan deng”
digitalWrite(Led,LOW);
//熄滅LED
break
;
default
:
break
;
}
}
上面代碼測(cè)試
開(kāi)燈 口令能點(diǎn)亮LED說(shuō)明LD3320通了
接下研究下KEYBORAD代碼
arduino leonardo可以直接用arduino的keyboard實(shí)例
我這邊經(jīng)過(guò)修改后代碼如下
#include <Keyboard.h>
#include <ld3320.h>
#include <PinMap.h>
VoiceRecognition Voice;
//聲明一個(gè)語(yǔ)音識(shí)別對(duì)象
bool
lastCapsLockState;
void
setup() {
// initialize control over the keyboard:
Keyboard.begin();
TIMSK0 = 0;
Voice.init();
//初始化VoiceRecognition模塊
//add fun
Voice.noiseTime(0x10);
Voice.voiceMaxLength(0x14);
Voice.micVol(0x55);
Voice.addCommand(
"zhuo mian"
, 0);
//添加指令,參數(shù)(指令內(nèi)容,指令標(biāo)簽(可重復(fù)))
Voice.addCommand(
"hong"
, 0);
Voice.addCommand(
"tui chu"
, 1);
Voice.addCommand(
"xia ye"
, 2);
Voice.addCommand(
"shang qu"
, 3);
Voice.addCommand(
"di bu"
, 4);
Voice.addCommand(
"zui shang"
, 5);
Voice.addCommand(
"liu lang qi"
, 6);
Voice.addCommand(
"guan bi"
, 7);
Voice.addCommand(
"xian shi"
, 8);
Voice.addCommand(
"qie huan"
, 9);
Voice.addCommand(
"shui jiao"
, 10);
Voice.addCommand(
"fang da"
, 11);
Voice.addCommand(
"bi ge"
, 11);
Voice.addCommand(
"suo xiao"
, 12);
Voice.addCommand(
"si mou"
, 12);
Voice.addCommand(
"mi ma"
, 13);
Voice.addCommand(
"en"
, 14);
Voice.addCommand(
"oh"
, 14);
Voice.addCommand(
"pi"
, 14);
Voice.addCommand(
"kiu"
, 14);
Voice.addCommand(
"wu"
, 14);
Voice.addCommand(
"a"
, 14);
Voice.addCommand(
"ei"
, 14);
Voice.addCommand(
"si"
, 14);
Voice.addCommand(
"jie suo"
, 15);
Voice.start();
//開(kāi)始識(shí)別
}
void
loop() {
switch
(Voice.read())
//判斷識(shí)別
{
case
0:
//若是指令“ei”
Keyboard.press(KEY_LEFT_GUI);
//左win
Keyboard.press(
'd'
);
break
;
case
1:
//若是指令“B”
Keyboard.press(KEY_ESC);
//Keyboard.print("You pressed the button ");
// Keyboard.print(counter);
// Keyboard.println(" times.");
break
;
case
2:
Keyboard.press(KEY_PAGE_DOWN);
break
;
case
3:
Keyboard.press(KEY_PAGE_UP);
break
;
case
4:
Keyboard.press(KEY_END);
break
;
case
5:
Keyboard.press(KEY_HOME);
break
;
case
6:
Keyboard.press(KEY_LEFT_GUI);
//左alt
Keyboard.press(
'2'
);
break
;
case
7:
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_F4);
break
;
case
8:
Keyboard.press(
'f'
);
break
;
case
9:
Keyboard.press(KEY_LEFT_ALT);
//左alt
Keyboard.press(KEY_TAB);
break
;
case
10:
Keyboard.press(KEY_LEFT_GUI);
//左win
delay(500);
Keyboard.press(
'l'
);
Keyboard.release(KEY_LEFT_GUI);
Keyboard.release(
'l'
);
break
;
case
11:
Keyboard.press(KEY_RIGHT_CTRL);
Keyboard.print(
'+'
);
break
;
case
12:
Keyboard.press(KEY_RIGHT_CTRL);
Keyboard.print(
'-'
);
break
;
case
13:
Keyboard.println(
"12312311"
);
break
;
case
14:
;
break
;
case
15:
Keyboard.press(KEY_RETURN);
break
;
default
:
break
;
delayMicroseconds(20000);
}
Keyboard.releaseAll();
Keyboard.end();
//結(jié)束鍵盤(pán)通訊
}