Advertisement
safwan092

easyVR-test-code-from-youtube-test1-SA

Feb 21st, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.11 KB | None | 0 0
  1. #include "Arduino.h"
  2. #if !defined(SERIAL_PORT_MONITOR)
  3.   #error "Arduino version not supported. Please update your IDE to the latest version."
  4. #endif
  5.  
  6. #if defined(SERIAL_PORT_USBVIRTUAL)
  7.   // Shield Jumper on HW (for Leonardo and Due)
  8.   #define port SERIAL_PORT_HARDWARE
  9.   #define pcSerial SERIAL_PORT_USBVIRTUAL
  10. #else
  11.   // Shield Jumper on SW (using pins 12/13 or 8/9 as RX/TX)
  12.   #include "SoftwareSerial.h"
  13.   SoftwareSerial port(12, 13);
  14.   #define pcSerial SERIAL_PORT_MONITOR
  15. #endif
  16.  
  17. #include "EasyVR.h"
  18.  
  19. #pragma once   //Stops multiple opening of same .h files.
  20.  
  21. EasyVR easyvr(port);
  22.  
  23. //Groups and Commands
  24. enum Groups
  25. {
  26.   GROUP_0  = 0,
  27.   GROUP_1  = 1,
  28. };
  29.  
  30. enum Group0
  31. {
  32.   G0_LED = 0,
  33. };
  34.  
  35. enum Group1
  36. {
  37.   G1_ON = 0,
  38.   G1_OFF = 1,
  39. };
  40.  
  41.  
  42. int8_t group, idx;
  43.  
  44.  
  45.  
  46.  
  47. //******************************************************************************
  48. void setup()
  49. {
  50.  
  51. pinMode(11,OUTPUT); //Initialize LED.
  52. digitalWrite(11,LOW); //Turn LED off.
  53.  
  54.   // setup PC serial port
  55.   pcSerial.begin(9600);
  56.  
  57.   // bridge mode?
  58.   int mode = easyvr.bridgeRequested(pcSerial);
  59.   switch (mode)
  60.   {
  61.   case EasyVR::BRIDGE_NONE:      //This is the case statement used.
  62.     // setup EasyVR serial port
  63.     port.begin(9600);
  64.     // run normally
  65.     pcSerial.println(F("---"));
  66.     pcSerial.println(F("Bridge not started!"));
  67.     Serial.println("*** case EasyVR::BRIDGE_NONE: ***");
  68.     break;
  69.    
  70.   case EasyVR::BRIDGE_NORMAL:
  71.     // setup EasyVR serial port (low speed)
  72.     port.begin(9600);
  73.     // soft-connect the two serial ports (PC and EasyVR)
  74.     easyvr.bridgeLoop(pcSerial);
  75.     // resume normally if aborted
  76.     pcSerial.println(F("---"));
  77.     pcSerial.println(F("Bridge connection aborted!"));
  78.      Serial.println("*** case EasyVR::BRIDGE_NORMAL: ***");
  79.     break;
  80.    
  81.   case EasyVR::BRIDGE_BOOT:
  82.     // setup EasyVR serial port (high speed)
  83.     port.begin(115200);
  84.     // soft-connect the two serial ports (PC and EasyVR)
  85.     easyvr.bridgeLoop(pcSerial);
  86.     // resume normally if aborted
  87.     pcSerial.println(F("---"));
  88.     pcSerial.println(F("Bridge connection aborted!"));
  89.     Serial.println("*** case EasyVR::BRIDGE_BOOT: ***");
  90.     break;
  91.  
  92.   } //end case.
  93.  
  94.  
  95.  
  96.   while (!easyvr.detect())
  97.   {
  98.     Serial.println("EasyVR not detected!");
  99.     delay(1000);
  100.   } //end while
  101.  
  102.   easyvr.setPinOutput(EasyVR::IO1, LOW);
  103.   Serial.println("EasyVR detected!");
  104.   easyvr.setTimeout(5); //Set the number of seconds to listen for each command.
  105.   easyvr.setLanguage(0); //Set language to English
  106.  
  107.   group = EasyVR::TRIGGER; //<-- start group (customize)
  108.  
  109.  easyvr.playSound(10, EasyVR::VOL_FULL); //Play woman asking asking question.
  110.  Serial.println("Now exit setup...");
  111.  
  112. } //end setup.
  113.  
  114.  
  115.  
  116.  
  117. void action();
  118.  
  119.  
  120. //******************************************************************************
  121. void loop()
  122. {
  123.  
  124.  Serial.println("Now in loop...");
  125.  
  126.   easyvr.setPinOutput(EasyVR::IO1, HIGH); // LED on (listening)
  127.  
  128.  
  129.  
  130.   Serial.print("Say a command in Group ");
  131.   Serial.println(group);
  132.   easyvr.recognizeCommand(group); //Recognition of a built-in word.
  133.  
  134.   do
  135.   {
  136.     // can do some processing while waiting for a spoken command
  137.   }
  138.   while (!easyvr.hasFinished());//Polls the status of on-going recogntion,training or asynchronous playback tasks.
  139.  
  140.   easyvr.setPinOutput(EasyVR::IO1, LOW); // LED off
  141.  
  142.      easyvr.setMicDistance(3);//Set mic operating distance. 1 nearest - 3 farthest.
  143.      easyvr.setLevel(1);//Set strictness level for recognition of custom commands.
  144.      easyvr.setKnob(0);//Set confidence threshold for recognition of built-in words.
  145.  
  146.  
  147.  
  148.   idx = easyvr.getWord();//Get recognised word index from built-in sets or custom grammars.
  149.   if (idx >= 0)
  150.   {
  151.     // built-in trigger (ROBOT)
  152.     // group = GROUP_X; <-- jump to another group X
  153.  
  154.     return;
  155.   } //end if.
  156.  
  157.   idx = easyvr.getCommand();//Get the recognised command index if any.
  158.  
  159.   if (idx >= 0)
  160.   {
  161.     // print debug message
  162.  
  163.     uint8_t train = 0;
  164.     char name[32];
  165.     Serial.print("Command: ");
  166.     Serial.print(idx);
  167.     if (easyvr.dumpCommand(group, idx, name, train)) //Retrieves the name and training data of a custom command.
  168.     //Group = 0-16 target group or values in #Groups.
  169.     //idx = 0-31 index of the command within the selected group.
  170.     //name = points to array of at least 32 characters that holds command label when function returns.
  171.    
  172.     {
  173.       Serial.print(" = ");
  174.       Serial.println(name);
  175.     } //end if.
  176.     else
  177.       Serial.println();
  178.     easyvr.playSound(0, EasyVR::VOL_FULL); //Play bell sound.
  179.  
  180.  
  181.    
  182.     // perform some action
  183.    
  184.     action(); //Turn LED on or off.
  185.  
  186.   } //end if.
  187.  
  188.   else // errors or timeout
  189.   {
  190.     if (easyvr.isTimeout())
  191.       Serial.println("Timed out, try again...");
  192.     int16_t err = easyvr.getError(); //Gets last error code. 0-255.
  193.     if (err >= 0)
  194.     {
  195.       Serial.print("Error ");
  196.       Serial.println(err, HEX);
  197.      
  198.     } //end if.
  199.  
  200.   } //end else.
  201.  
  202. } //end loop.
  203.  
  204.  
  205. //******************************************************************************
  206. void action()
  207. {
  208.     Serial.println("Now in action function...");
  209.    
  210.     switch (group)
  211.     {
  212.     case GROUP_0:
  213.       switch (idx)
  214.       {
  215.       case G0_LED:
  216.         // write your action code here
  217.          group = GROUP_1; //<-- or jump to another group X for composite commands
  218.         break;
  219.  
  220.       } //end case GROUP_0.
  221.       break;
  222.      
  223.     case GROUP_1:
  224.       switch (idx)
  225.       {
  226.       case G1_ON:
  227.         // write your action code here
  228.         digitalWrite(11, HIGH);   // set the LED on
  229.         // group = GROUP_X; <-- or jump to another group X for composite commands
  230.            easyvr.playSound(0, EasyVR::VOL_FULL); //Play bell sound.
  231.         break;
  232.  
  233.       case G1_OFF:
  234.         // write your action code here
  235.         digitalWrite(11, LOW);    // set the LED off
  236.         // group = GROUP_X; <-- or jump to another group X for composite commands
  237.            easyvr.playSound(11, EasyVR::VOL_FULL); //Play Dave sound.
  238.         break;
  239.  
  240.       } //end idx case GROUP_1.
  241.  
  242.       break;
  243.    
  244.     } //end group case stmt.
  245.  
  246. } //end action.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement