w0lfiesmith

Arduino speech control demo with Automator actions

Feb 5th, 2013
2,247
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Sketch Name..:  Arduino Voice Control via Serial Demo
  2.  * Author.......:  James Bruce, for MakeUseOf.com
  3.  * Description..:  Monitor serial communications and turn on or
  4.                    off relay switches for external devices
  5.  * Date Created.: 5 February 2013
  6.  */
  7.  
  8. //Buffer serial data to accept strings
  9. char inData[20]; // Allocate some space for the string
  10. char inChar=-1; // Where to store the character read
  11. byte index = 0; // Index into array; where to store the character
  12.  
  13. void setup() {
  14.     Serial.begin(9600);
  15.  
  16. }
  17.  
  18. void loop() {
  19.  
  20.     if(Comp("lightsOn")==0){
  21.        //insert relay control code to turn on devices
  22.        Serial.println("turning on");
  23.     }
  24.     else if(Comp("lightsOff")==0){
  25.          
  26.        //insert your relay controls here to turn off
  27.        Serial.println("turning off");  
  28.     }
  29.     /* To create more commands, send a different string and listen for it with another
  30.     * else if statement:
  31.     * else if(Comp("ovenOn")==0){}
  32.     */
  33.    
  34. }
  35.  
  36. /* Serial buffer for reading Strings:
  37. * Credit to PaulS
  38. * on this forum thread http://arduino.cc/forum/index.php/topic,45629.0.html
  39. */
  40.  
  41. char Comp(char* This) {
  42.     while (Serial.available() > 0) // Don't read unless
  43.                                    // there you know there is data
  44.     {
  45.         if(index < 19) // One less than the size of the array
  46.         {
  47.             inChar = Serial.read(); // Read a character
  48.             inData[index] = inChar; // Store it
  49.             index++; // Increment where to write next
  50.             inData[index] = '\0'; // Null terminate the string
  51.         }
  52.     }
  53.  
  54.     if (strcmp(inData,This)  == 0) {
  55.         for (int i=0;i<19;i++) {
  56.             inData[i]=0;
  57.         }
  58.         index=0;
  59.         return(0);
  60.     }
  61.     else {
  62.         return(1);
  63.     }
  64. }
RAW Paste Data