Part 1: Making A Platform
int E1 = 5; //M1 Speed Control
int E2 = 6; //M2 Speed Control
int M1 = 4; //M1 Direction Control
int M2 = 7; //M1 Direction Control
void stop(void) //Stop
{
digitalWrite(E1,0);
digitalWrite(M1,LOW);
digitalWrite(E2,0);
digitalWrite(M2,LOW);
}
void advance(char a,char b) //Move forward
{
analogWrite (E1,a); //PWM Speed Control
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void back_off (char a,char b) //Move backward
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void turn_L (char a,char b) //Turn Left
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void turn_R (char a,char b) //Turn Right
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void setup(void)
{
int i;
for(i=4;i<=7;i++)
pinMode(i, OUTPUT);
Serial.begin(19200); //Set Baud Rate
Serial.println("Run keyboard control");
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void loop(void)
{
if(Serial.available()){
char val = Serial.read();
if(val != -1)
{
switch(val)
{
case 'w'://Move Forward
advance (255,255); //move forward in max speed
break;
case 's'://Move Backward
back_off (255,255); //move back in max speed
break;
case 'a'://Turn Left
turn_L (100,100);
break;
case 'd'://Turn Right
turn_R (100,100);
break;
case 'z':
Serial.println("Hello");
break;
case 'x':
stop();
break;
}
}
else stop();
}
}
Part 2: Adding Voice Control
#include <Voice.h> #define SUM 2 //variable no greater than 50 uint8 nAsrStatus=0; char sRecog[SUM][80] = {"turn on lights", "turn off lights"};//variable not greater than 79, user can modify int state=7; //status indicator int led=8; //control digital port void finally (unsigned char n) { switch(n) //array corresponding keyword serial number, such as arrays recognise the first keyword is “turn on lights” and corresponding sequence number is 0; { case 0: Serial.println( "turn on lights"); Serial.println( " "); digitalWrite(led,LOW); break; case 1: Serial.println( "turn off lights"); digitalWrite(led,HIGH); break; default: Serial.println( "error"); Serial.println( " "); break; } } void ExtInt0Handler () { Voice.ProcessInt0(); //send an interrupt signal } void setup() { Serial.begin(9600); Voice.Initialise(MIC,VoiceRecognitionV1);//Initialise mode MIC or MONO,default is MIC //VoiceRecognitionV1 is VoiceRecognitionV1.0 shield //VoiceRecognitionV2 is VoiceRecognitionV2.1 module attachInterrupt(0,ExtInt0Handler,LOW); pinMode(led,OUTPUT); pinMode(state,OUTPUT); digitalWrite(state,HIGH); digitalWrite(led,HIGH); } void loop() { uint8 nAsrRes; nAsrStatus = LD_ASR_NONE; while(1) { switch(nAsrStatus) { case LD_ASR_RUNING: case LD_ASR_ERROR: break; case LD_ASR_NONE: { nAsrStatus=LD_ASR_RUNING; if (Voice.RunASR(SUM,80,sRecog)==0) //incorrect identification { nAsrStatus= LD_ASR_ERROR; Serial.println( "ASR_ERROR"); } digitalWrite(state,LOW); Serial.println( "ASR_RUNING....."); break; } case LD_ASR_FOUNDOK: { digitalWrite(state,HIGH); nAsrRes =Voice. LD_GetResult();//once asr recognition process ends, pick up asr recognition results finally(nAsrRes); nAsrStatus = LD_ASR_NONE; break; } case LD_ASR_FOUNDZERO: default: { nAsrStatus = LD_ASR_NONE; break; } }// switch delay(500); }// while }
#include <Voice.h>
//#include <SoftwareSerial.h> //ruan chuankou
#define SUM 5 //SUM????????,?????50?
uint8 nAsrStatus=0;
//SoftwareSerial mySerial(10, 11); // RX, TX
char sRecog[SUM][80] = {"qian jin", "hou tui","zuo zhuan", "you zhuan","ting zhi"};//
int E1 = 5; //M1 Speed Control
int E2 = 6; //M2 Speed Control
int M1 = 4; //M1 Direction Control
int M2 = 7; //M1 Direction Control
void stop(void) //Stop
{
digitalWrite(E1,0);
digitalWrite(E2,0);
}
void advance(char a,char b) //Move forward
{
analogWrite (E1,a); //PWM Speed Control
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void back_off (char a,char b) //Move backward
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void turn_L (char a,char b) //Turn Left
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void turn_R (char a,char b) //Turn Right
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void finally (unsigned char n)
{
switch(n) //
{
case 0:
Serial.println( "qian jin");
Serial.println( " ");
advance (255,255);
break;
case 1:
Serial.println( "hou tui");
back_off (255,255);
break;
case 2:
Serial.println( "zuo zhuan");
turn_L (100,100);
break;
case 3:
Serial.println( "you zhuan");
turn_R (100,100);
break;
case 4:
Serial.println( "ting zhi");
stop();
break;
default:
Serial.println( "error");
Serial.println( " ");
break;
}
}
void ExtInt0Handler ()
{
Voice.ProcessInt0(); //
}
void setup(void)
{
int i;
for(i=4;i<=7;i++)
pinMode(i, OUTPUT);
Serial.begin(19200); //Set Baud Rate
Voice.Initialise(MIC,VoiceRecognitionV1);//Initialise mode MIC or MONO,default is MIC
//VoiceRecognitionV1 is VoiceRecognitionV1.0 shield
//VoiceRecognitionV2 is VoiceRecognitionV2.1 module
attachInterrupt(0,ExtInt0Handler,LOW);
}
void loop()
{
uint8 nAsrRes;
nAsrStatus = LD_ASR_NONE;
while(1)
{
switch(nAsrStatus)
{
case LD_ASR_RUNING:
case LD_ASR_ERROR:
break;
case LD_ASR_NONE:
{
nAsrStatus=LD_ASR_RUNING;
if (Voice.RunASR(SUM,80,sRecog)==0) //?????
{
nAsrStatus= LD_ASR_ERROR;
Serial.println( "ASR_ERROR");
}
Serial.println( "ASR_RUNING.....");
break;
}
case LD_ASR_FOUNDOK:
{
nAsrRes =Voice. LD_GetResult();//
finally(nAsrRes);
nAsrStatus = LD_ASR_NONE;
break;
}
case LD_ASR_FOUNDZERO:
default:
{
nAsrStatus = LD_ASR_NONE;
break;
}
}// switch
delay(500);
}// while
}Realize the function
Download the program on the board card to conduct test. When we say “advance”, the vehicle will call “advance”()function, then moves forward. It is the same with other movements. When it is working satisfactorily, we can unplug the USB cable and install a lithium battery so that the vehicle is more mobile.
Part 3: Combine voice recognition with MP3 module
Even with successful voice control, it seems like something is missing. What if the vehicle can respond to us? By adding an mp3 module to the setup, this will be possible. I used an mp3 player module – the DFRDuino player module.
One thing worth noticing is that you’d better put on a tape on the back of the module to prevent short circuit while using it. As for the wiring, mp3 module has a serial pin port, providing five pins including 5V, GND, RX, TX, OUT.
Wire the pins except for OUT to the mp3 wiring port of voice recognition module as follows:
5V corresponds to 5V. GND corresponds to GND. RX should be connected with TX and TX should be connected with RX as shown below:
We add a small speaker here.
Step 2: Programming
We need to program it after finishing the wiring. MP3’s player code will be added based on previous program. Audio files are stored on a micro SD card, you can use any that you want to correspond to each response. Perhaps you could record your own responses so that it sounds like you can chat with the Arduino robot). Please be noted that we are using serial port communication, so the button on the voice recognition module should point to UART instead of 12C.
The code I used is as follows:
void finally (unsigned char n) { switch(n) //n??????????????????sRecog?????????“kai deng”????????0? { case 0: Serial.println( "qian jin"); Serial.println("\qian"); advance (255,255); break; case 1: Serial.println( "hou tui"); Serial.println("\hou"); back_off (255,255); break; case 2: Serial.println( "zuo zhuan"); Serial.println("\zuo"); turn_L (100,100); break; case 3: Serial.println( "you zhuan"); Serial.println("\you"); turn_R (100,100); break; case 4: Serial.println( "ting zhi"); Serial.println("\zhi"); stop(); break; case 5: Serial.println( "chang ge"); Serial.println("\bo"); Serial.println("\2"); break; case 6: Serial.println( "zan ting bo"); Serial.println("\:p"); break; case 7: Serial.println( "ji xu bo"); Serial.println("\:s"); break; case 8: Serial.println( "xia yi shou"); Serial.println("\:n"); break; case 9: Serial.println( "shang yi shou"); Serial.println("\:u"); break; default: Serial.println( "error"); Serial.println( " "); break; } }