This is my code below, the most important thing that wasn't too obvious to me is that you need to short pins 2 & 4 and upload this code to actually train SD commands, then short pins 2 & 3 when you want to actually use the commands.
I commented specifically on functionality using (Comment), so read that, ignore the other stuff.
I'll eventually clean up this code and get rid of everything not important to SD commands that I'm using, but as for right now it's sloppy altered code.
#include <SoftwareSerial.h>
#include "protocol.h"
uint8_t _receivePin;
uint8_t _transmitPin;
long _baudRate;
int _bitPeriod;
void setup(){
// PC UART Init
pinMode(0, INPUT); // sets the digital pin as input
pinMode(1, OUTPUT); // sets the digital pin as output
// VRbot UART Init
pinMode(12, INPUT); // sets the digital pin as input
pinMode(13, OUTPUT); // sets the digital pin as output
// Input Output Init
pinMode(2,INPUT); // sets the digital pin as input
pinMode(3,OUTPUT); // sets the digital pin as output
pinMode(4,OUTPUT); // sets the digital pin as output
pinMode(9,OUTPUT); // sets the digital pin as output (My own pin hooked to a 1k ohm resistor and red LED for testing purposes)
digitalWrite(9,LOW); // Set pin 9 low (LED off)
digitalWrite(3,HIGH); // Set pin 3 high
digitalWrite(4,LOW); // Set pin 4 low
// connect Di2 to Di3 to enter Normal mode - Di2 is high
// connect Di2 to Di4 to enter Bridge mode - Di2 is low
// bridge mode allow direct communication between VRbot module and VRbot GUI software
if (digitalRead(2)==LOW){ // if Di2 is LOW enter Bridge mode
while(1)
{
int pc2vr = digitalRead(0);
digitalWrite(13, pc2vr);
int vr2pc = digitalRead(12);
digitalWrite(1, vr2pc);
}
}
// if Di2 is HIGH enter Normal mode
Serial.begin(9600);
delay(200);
Serial.println("Arduino 2009 VRBot control program");
Serial.println("Normal Mode");
// Set up and detect VRbot
VRbot_setup();
if (!VRbot_Detect())
Serial.println("VRbot NA");
else {
Serial.println("VRbot detected");
// Set VRbot timeout to 5 seconds
Serial.println("Setting timeout to: 5 seconds");
VRbot_SetTimeout(5);
// Set VRbot language to English
Serial.println("Setting Language to: English");
VRbot_SetLanguage(0);
}
}
void loop(){
// implement speaker independent recognition
//SI_Recognition();
// or speaker dependent recognition Comment out 2 lines above to not use SI sets, and the only thing in the loop should be the one line below
SD_Recognition();
// according to your need
}
/*******************************************************************************
- Speaker Independent recognition function:
-
- Wait for trigger word 'Robot'
- Set wordset 1
- Wait for command MOVE or command TURN, errors or other commands are ignored
- Set wordset 2
- Wait for command FOWRWARD or command BACWARD for MOVE, errors or other commands are ignored
- or
- Wait for command LEFT or command RIGHT for TURN, errors or other commands are ignored
-
********************************************************************************/
void SI_Recognition()
{
int cmd;
Serial.println("Say Trigger Word!");
digitalWrite(9,LOW);
VRbot_RecognizeSI(0); // start SI trigger word recognition and wait for trigger
cmd = VRbot_CheckResult(); // check result
if( cmd == -1) // timeout
{
Serial.println("Timeout");
return;
}
if( cmd == -2) // error
{
Serial.println("Error Trigger");
return;
}
Serial.println("Wordset 1");
Serial.println("Say a command!");
//digitalWrite(4,LOW); // LED ON
VRbot_RecognizeSI(1); // start SI recognition of wordset 1
cmd = VRbot_CheckResult(); // check recognition result
digitalWrite(9,HIGH);
switch (cmd){
case -2: // Error
Serial.println("Error");
break;
case -1: // Timoeut
Serial.println("Timeout");
break;
case 1: // MOVE
Serial.println("Commmand: MOVE");
digitalWrite(9,HIGH);
delay (200);
Serial.println("Wordset 2");
Serial.println("Say a command!");
VRbot_RecognizeSI(2); // start SI recognition wordset 2
cmd = VRbot_CheckResult(); // check recognition result
switch (cmd){
case -2: // Error
Serial.println("Error");
break;
case -1: // Timoeut
Serial.println("Timeout");
break;
case 4: // FORWARD
Serial.println("Commmand: FWD");
break;
case 5: // BACWARD
Serial.println("Commmand: BWD");
break;
default: // Other command
Serial.println("Commmand: other!");
break;
}
break;
case 2: // TURN
Serial.println("Commmand: TURN");
delay (200);
Serial.println("Wordset 2");
Serial.println("Say a command!");
VRbot_RecognizeSI(2); // start SI recognition wordset 2
cmd = VRbot_CheckResult(); // check recognition result
switch (cmd){
case -2: // Error
Serial.println("Error");
break;
case -1: // Timoeut
Serial.println("Timeout");
break;
case 0: // LEFT
Serial.println("Commmand: LEFT");
break;
case 1:
Serial.println("Commmand: RIGHT");
break;
default: // Other command
Serial.println("Commmand: other!");
break;
}
break;
default: // Other command
Serial.println("Commmand: other!");
delay(200);
break;
}
}
// Speaker Dependent recognition function
// Wait for user trigger word set by user with VRBot GUI
// Set wordset Group 1
// Wait for command 0 or command 1 set by user with VRBot GUI, other commands are ignored
void SD_Recognition()
{
int cmd;
Serial.println("Say Trigger!");
//VRbot_RecognizeSD(0); // start SD trigger word recognition and wait for triggerI commented this out as I did not want to use a trigger word
//cmd = VRbot_CheckResult(); // check recognition resultThis too, cuz there's nothing to check for
//if( cmd == -1) // timeout
//{
// Serial.println("Timeout");
// return;
//}
//if( cmd == -2) // error
//{
// Serial.println("Error Trigger");
// return;
//}
//Serial.println("Group 1");
//Serial.println("Say a command!");
VRbot_RecognizeSD(1); // start SD recognition group 1 and wait for a commandThis uses SD commands from group 1, which is the first group of custom words that aren't trigger words
cmd = VRbot_CheckResult(); // check recognition result
switch (cmd){
case -2: // Error
Serial.println("Error");
break;
case -1: // Timoeut
Serial.println("Timeout");
break;
case 0: // USER SD WORD 0 When word 0 is detected, turn the LED on
digitalWrite(9,HIGH);
delay(1000); Delayed one second, probably not neccesary
break;
case 1: // USER SD WORD 1
digitalWrite(9,LOW); When word 1 is detected, turn LED off
delay(1000);
break;
default: // Other command
Serial.println("Commmand: error!");
break;
}
******** Everything below this you can ignore, it's setup and code to detect spoken words********
}
/*******************************************************************************/
void VRbot_setup()
{
_baudRate = 9600;
_bitPeriod = 1000000 / _baudRate;
_receivePin = 12;
_transmitPin = 13;
digitalWrite(_transmitPin, LOW);
delayMicroseconds( _bitPeriod);
}
unsigned char VRbot_read(void)
{
uint8_t val = 0;
int bitDelay = _bitPeriod - clockCyclesToMicroseconds(100);
// one byte of serial data (LSB first)
// ...--\ /--/--/--/--/--/--/--/--/--...
// --/--/--/--/--/--/--/--/--/
// start 0 1 2 3 4 5 6 7 stop