Difference between revisions of "Arduino"

From VoxCommando
Jump to: navigation, search
m (1 revision)
 
m (1 revision)
 
(No difference)

Latest revision as of 11:24, 13 January 2021

Samples

Here are some examples using the python plugin for VC to talk to Arduino.

You must enable the python plugin to use the examples.

Sample #1

Using python to communicate with Arduino on serial port. A simple example of controlling GPIO pins on an Arduino Mega. In this case we are setting digital output pins to either HIGH or LOW in order to control a relay board.

See it in action:

Arduino code

  • Here is the Arduino code:
  • You can change which GPIO pins will be used by changing the first 4 lines of 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);    
  }     
}


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!
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)

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.
<?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>

Sample #2

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