Created
December 6, 2013 06:57
-
-
Save e-Gizmo/7819670 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Demonstration Sketch for e-Gizmo Voice Recognition Module * | |
* by e-Gizmo Mechatronix Central | |
* | |
*/ | |
// connection guide | |
/* | |
This Demo requires a LED Shield | |
VR TX -> gizDuino RX | |
VR RX -> gizDuino TX | |
VR GND -> gizDuino GND | |
VR VCC -> gizDuino +5V | |
Record Switch is S2 (A3) | |
*/ | |
/* | |
This code is written for gizDuino+ Controller | |
To use with gizDuino, remove all "1" in the Serial1 object | |
e.g. Serial1.available to Serial.available | |
*/ | |
#define WAITING 00 | |
#define COMPACT 0x37 | |
#define QUERY 0x24 | |
#define CLR1 0x01 | |
#define CLR2 0x02 | |
#define CLR3 0x031 | |
#define REC1 0x11 | |
#define REC2 0x12 | |
#define REC3 0x13 | |
#define LOAD1 0x21 | |
#define LOAD2 0x22 | |
#define LOAD3 0x23 | |
#define ERROR 0xE0 | |
//pin Usage | |
#define LEDPIN 13 | |
#define RECSW 17 | |
#define VR1 8 | |
#define VR2 9 | |
#define VR3 10 | |
#define VR4 11 | |
#define VR5 12 | |
byte rxchar; | |
byte initvr=0; | |
byte teachnum=0; | |
void setup(){ | |
pinMode(LEDPIN,OUTPUT); | |
pinMode(VR1,OUTPUT); | |
pinMode(VR2,OUTPUT); | |
pinMode(VR3,OUTPUT); | |
pinMode(VR4,OUTPUT); | |
pinMode(VR5,OUTPUT); | |
//Serial.begin(9600); // Used for debugging | |
Serial1.begin(9600); | |
// LED Test | |
digitalWrite(VR1,HIGH); | |
digitalWrite(VR2,HIGH); | |
digitalWrite(VR3,HIGH); | |
digitalWrite(VR4,HIGH); | |
digitalWrite(VR5,HIGH); | |
digitalWrite(LEDPIN,HIGH); | |
delay(500); | |
send_VR(COMPACT); // switch to compact mode | |
delay(500); | |
// enable pull up to switches | |
digitalWrite(RECSW,HIGH); | |
ledOFF(); | |
digitalWrite(LEDPIN,LOW); | |
} | |
void loop(void) | |
{ | |
if(digitalRead(RECSW)==HIGH) | |
voice_command(1); | |
else | |
teach_command(1); | |
} | |
/* Turn OFF voice indicators */ | |
void ledOFF(void){ | |
digitalWrite(VR1,LOW); | |
digitalWrite(VR2,LOW); | |
digitalWrite(VR3,LOW); | |
digitalWrite(VR4,LOW); | |
digitalWrite(VR5,LOW); | |
} | |
/* This routine checks the serial input * | |
* returns 1 if no message is available | |
* returns 0 and message on rxchar if available | |
*/ | |
byte read_buffer(void){ | |
if(Serial1.available()>0){ | |
rxchar=Serial1.read(); // Read one Rxed character | |
Serial.println((byte) rxchar,HEX); | |
return(0); | |
} | |
else | |
return(1); | |
} | |
/* This function sends the VR command | |
* with 0xAA header | |
*/ | |
void send_VR(byte message){ | |
Serial1.write(0xAA); //Header | |
Serial1.write(message); | |
} | |
/* Teach a Group of Commands */ | |
byte teach_command(byte group){ | |
send_VR(WAITING); | |
ledOFF(); | |
delay(500); | |
// flush serial buffer | |
read_buffer(); | |
if((group>3) | (group==0)) return(0); | |
// Erase previous content | |
send_VR(group); | |
delay(500); | |
if(rxchar==ERROR){ | |
return(1); // error occured | |
} | |
// start teaching | |
teachnum=VR1; | |
send_VR(group+0x10); | |
// Monitor and wait until recording is complete | |
do{ | |
while(read_buffer()==1); | |
digitalWrite(teachnum,LOW); | |
// flash on board LED if a recording is successful | |
if(rxchar==0x45){ | |
teachnum++; | |
digitalWrite(LEDPIN,HIGH); | |
delay(500); | |
digitalWrite(LEDPIN,LOW); | |
} | |
// turn ON teach number pin when ready to accept voice | |
if(rxchar==0x40) digitalWrite(teachnum,HIGH); | |
if(rxchar==0x42){ | |
digitalWrite(LEDPIN,HIGH); | |
delay(100); | |
digitalWrite(LEDPIN,LOW); | |
} | |
}while (rxchar != (0x45+group)); | |
digitalWrite(LEDPIN,HIGH); | |
delay(1000); | |
digitalWrite(LEDPIN,LOW); | |
ledOFF(); | |
initvr=0; | |
} | |
/* Set to Voice Recognition mode */ | |
void voice_command(byte group){ | |
// put to Voice Recognition Mode | |
if(initvr==0){ | |
send_VR(WAITING); | |
delay(500); | |
// flush serial buffer | |
read_buffer(); | |
send_VR(0x20+group); | |
initvr=1; | |
read_buffer(); | |
} | |
group *= 0x10; | |
if(read_buffer()==0){ | |
ledOFF(); | |
if(rxchar==group+1) digitalWrite(VR1,HIGH); | |
if(rxchar==group+2) digitalWrite(VR2,HIGH); | |
if(rxchar==group+3) digitalWrite(VR3,HIGH); | |
if(rxchar==group+4) digitalWrite(VR4,HIGH); | |
if(rxchar==group+5) digitalWrite(VR5,HIGH); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment