Arduino Voice Control and code

View previous topic View next topic Go down

Arduino Voice Control and code

Post by jamied_uk on 3rd August 2015, 18:39

http://voxcommando.com/mediawiki/index.php?title=Arduino


https://youtu.be/sz6_1Igfx-s






Here are some examples using the python plugin for VC to talk to Arduino.
You must enable the python plugin to use the examples.

Arduino code

    Here is the Arduino code:
    You can change which GPIO pins will be used by changing the first 4 lines of code
Code:

int relay1=40;
int relay2=42;
int relay3=44;
int relay4=46;

void setup()
{               
  // initialize the digital pin as an output.
  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, HIGH);   
  pinMode(relay2, OUTPUT); 
  digitalWrite(relay2, HIGH);   
  pinMode(relay3, OUTPUT);
  digitalWrite(relay3, HIGH);   
  pinMode(relay4, OUTPUT); 
  digitalWrite(relay4, HIGH);   
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  delay(50);
  if (Serial.available() >= 2)
  {
    char numRelay = Serial.read();
    char newState = Serial.read();

    switch (numRelay) {
    case '1':     
      adjustRelay(relay1,newState);
      break;
    case '2':
      adjustRelay(relay2,newState);
      break;
    case '3':
      adjustRelay(relay3,newState);
      break;   
    case '4':
      adjustRelay(relay4,newState);
      break;
    }
  }
}

void adjustRelay(int whichPin, char setState)
{
  if (setState=='0')
  {
    Serial.write(" OFF");
    digitalWrite(whichPin, HIGH);
  }
  else if(setState=='1')
  {   
    Serial.write(" ON");
    digitalWrite(whichPin, LOW);   
  }    
}






~ end of code (line ends a line above this code and remove all space)! ~



Python code

    Save the following code to the file: VoxCommando\PY\ArduinoStartup.py
    Be sure to adjust the com port to match the port that your Arduino is connected to. Mine is connected to "COM4"
    Note that in this example the serial port will be opened when VC starts and will remain open until VC closes. If you prefer you can edit the code to have it open the port, send the data, and close the port each time the arduinoWrite function is called. However, with certain versions of Arduino, opening the serial port can cause the Arduino to reset!
Code:

import clr
clr.AddReference('System')
from System import *
serialPort = IO.Ports.SerialPort("COM4")
serialPort.BaudRate = 9600
serialPort.DataBits = 8
serialPort.Open()

def arduinoWrite(str):
    serialPort.Write(str)



~ end of code (line ends a line above this code and remove all space)! ~



XML voice commands

    Copy and paste the following xml into your VoxCommando command tree.
    The "startup" command loads your ArduinoStartup.py python file when VC first starts up (using the VC.Loaded event trigger). Therefore, you should re-start VC before testing the voice commands.


Code:
<?xml version="1.0" encoding="utf-16"?>
<commandGroup open="True" name="ArduinoRelays" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="270" name="startup" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecFile</cmdType>
      <cmdString>PY\ArduinoStartup.py</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <event>VC.Loaded</event>
  </command>
  <command id="277" name="Turn Relay {1} on" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <cmdString>arduinoWrite("{1}1")</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>Turn relay</phrase>
    <payloadRange>1,4</payloadRange>
    <phrase>on</phrase>
  </command>
  <command id="305" name="Turn Relay {1} off" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <cmdString>arduinoWrite("{1}0")</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>Turn relay</phrase>
    <payloadRange>1,4</payloadRange>
    <phrase>off</phrase>
  </command>
  <command id="301" name="Turn all relays OFF" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <cmdString>arduinoWrite("10203040")</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>Turn all relays OFF</phrase>
  </command>
  <command id="314" name="Turn all relays ON" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <cmdString>arduinoWrite("11213141")</cmdString>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>Turn all relays ON</phrase>
  </command>
</commandGroup>





~ end of code (line ends a line above this code and remove all space)! ~



Sample #2

Serial communication in both directions. Code and explanation in this forum thread: http://voxcommando.com/forum/index.php?topic=1452.msg12742#msg12742
avatar
jamied_uk
Admin

Posts : 2268
Join date : 2010-05-09
Age : 34
Location : UK

http://address-shortner.co.uk

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum