Introduction: Voice Controlled Rover Robot

This instructable is about controlling any serial controlled robot by using your voice! I will show you the Arduino sketch used for the rover robot and the Microsoft Visual Studio C# windows forms application. This is a very simple task and I will attach my code for you to play with.

Step 1: What You Will Need:

You will only need a few things for this.

1. A robot controlled by a COM port.

2. A computer running Microsoft Visual Studio and your code editing software (in this case mine is Arduino)

3. A USB cable to upload the sketch to the robot

Step 2: Starting With the Robot

I have attached the code below but, if your not using the Arduino software I have posted it below. This is what I used for my rover robot for RobotShop.com. I took a simple WASD sketch and modified it to allow a program to control it "remotely". Once you have copied the code you can upload it to your robot using a USB cable or whatever means you use to upload to your robot.

//Setting motor variables
int motorSpeed = 6; int motor2Speed = 5; int motor1 = 8; int motor2 = 7;
void setup() {
 int i;
 for(i=5;i<=8;i++)
 pinMode(i, OUTPUT);
 Serial.begin(9600); //Start Serial Communication
}
void loop() {
 //waiting for any serial communication. If any is received conduct the switch statement. 
 char data = Serial.read();
 //Setting speed. 255 is max speed, you can change the values below to slow it down if you want.
 int leftspeed = 255;  
 int rightspeed = 255;
 switch (data) {
   
   case '0': //If the arduino receives a 0 then it will run the halt command which is defined below.
   halt ();
   break;
   
   case '1':
   forward (leftspeed, rightspeed);
   break;
   
   case '2':
   reverse (leftspeed, rightspeed);
   break;
   
   case '3':
   left (rightspeed, leftspeed);
   break;
   
   case '4':
   right (rightspeed, leftspeed);
   break;
 }
}
void halt(void)
{
 digitalWrite(motorSpeed, LOW);
 digitalWrite(motor2Speed, LOW); 
}
void forward(char a, char b)
{
 analogWrite(motorSpeed, a);  //releasing the "brake"
 digitalWrite(motor1, LOW);   //Applying full power to the pin. This would typically be HIGH but, my wires are hooked up backwards so I just switched the command.
 analogWrite(motor2Speed, b);
 digitalWrite(motor2, LOW); 
}
void reverse (char a, char b)
{
 analogWrite(motorSpeed, a);
 digitalWrite(motor1, HIGH);
 analogWrite(motor2Speed, b);
 digitalWrite(motor2, HIGH); 
}
void left (char a,char b)
{
 analogWrite (motorSpeed, a);
 digitalWrite(motor1, HIGH);
 analogWrite (motor2Speed, b);
 digitalWrite(motor2, LOW);
}
void right (char a,char b)
{
 analogWrite (motorSpeed, a);
 digitalWrite(motor1, LOW);
 analogWrite (motor2Speed, b);
 digitalWrite(motor2, HIGH);
}

Step 3: Microsoft Visual Studio C# Application

Now it is time to start up Microsoft Visual Studio. We start by creating a C# windows form application. I starting by creating the UI. This includes 5 labels, 1 rich textbox, and 2 buttons. These can be added to the form using the toolbox on the left hand side of the screen.

Once you have added the items above, you can double click on the top border of your application. This will bring up the C# code window behind your program. In the code attached and below I have tried to comment a lot of it, it should be pretty easy to follow. If not I have attached a "released" version of the program and the Visual Studio project file.

You can edit the COM port to whatever your robot is on. My robot was on COM5.

using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO.Ports; using System.Speech; using System.Speech.Recognition; using System.Speech.Synthesis;
namespace Voice_Controlled_Rover
{
    public partial class Form1 : Form
    {
        private SerialPort myport;
        SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine(); //starting the ability for the computer to recognize voice
        SpeechSynthesizer synth = new SpeechSynthesizer(); //starting the abilty to have the computer talk back
        public Form1()
        {
            InitializeComponent();
            init(); //runs starting parameters setting the baud rate, com port, etc. everything is below.
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            button2.Enabled = false; //greys out the disable button when the program starts
            Choices commands = new Choices();
            commands.Add(new string[] { "forward", "reverse", "left", "right", "halt" }); //Setting which commands will be recognized by the voice recognition.
            GrammarBuilder gBuilder = new GrammarBuilder();
            gBuilder.Append(commands);
            Grammar grammar = new Grammar(gBuilder);
            recEngine.LoadGrammarAsync(grammar);
            recEngine.SetInputToDefaultAudioDevice();
            recEngine.SpeechRecognized += RecEngine_SpeechRecognized;
        }
        private void RecEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            switch (e.Result.Text)
            {
                case "halt":
                    //sending a 0 to the robot.
                    myport.WriteLine("0");
                    //changing the colors of the labels to display what is happening.
                    label5.ForeColor = System.Drawing.Color.Green;
                    label1.ForeColor = System.Drawing.Color.DarkRed;
                    label2.ForeColor = System.Drawing.Color.DarkRed;
                    label3.ForeColor = System.Drawing.Color.DarkRed;
                    label4.ForeColor = System.Drawing.Color.DarkRed;
                    //computer will say "Halt"
                    synth.SpeakAsync("Halt");
                    //Halt will display on a new line in the Rich Textbox.
                    richTextBox1.Text += "\nHalt";
                    break;
                case "forward":
                    myport.WriteLine("1");
                    label1.ForeColor = System.Drawing.Color.Green;
                    label2.ForeColor = System.Drawing.Color.DarkRed;
                    label3.ForeColor = System.Drawing.Color.DarkRed;
                    label4.ForeColor = System.Drawing.Color.DarkRed;
                    label5.ForeColor = System.Drawing.Color.DarkRed;
                    synth.SpeakAsync("Forward");
                    richTextBox1.Text += "\nForward";
                    break;
                case "reverse":
                    myport.WriteLine("2");
                    label1.ForeColor = System.Drawing.Color.DarkRed;
                    label2.ForeColor = System.Drawing.Color.Green;
                    label3.ForeColor = System.Drawing.Color.DarkRed;
                    label4.ForeColor = System.Drawing.Color.DarkRed;
                    label5.ForeColor = System.Drawing.Color.DarkRed;
                    synth.SpeakAsync("Reverse");
                    richTextBox1.Text += "\nReverse";
                    break;
                case "left":
                    myport.WriteLine("3");
                    label1.ForeColor = System.Drawing.Color.DarkRed;
                    label2.ForeColor = System.Drawing.Color.DarkRed;
                    label3.ForeColor = System.Drawing.Color.Green;
                    label4.ForeColor = System.Drawing.Color.DarkRed;
                    label5.ForeColor = System.Drawing.Color.DarkRed;
                    synth.SpeakAsync("left");
                    richTextBox1.Text += "\nLeft";
                    break;
                case "right":
                    myport.WriteLine("4");
                    label1.ForeColor = System.Drawing.Color.DarkRed;
                    label2.ForeColor = System.Drawing.Color.DarkRed;
                    label3.ForeColor = System.Drawing.Color.DarkRed;
                    label4.ForeColor = System.Drawing.Color.Green;
                    label5.ForeColor = System.Drawing.Color.DarkRed;
                    synth.SpeakAsync("right");
                    richTextBox1.Text += "\nRight";
                    break;
            }
        }
        public void init()
        {
            try
            {
                myport = new SerialPort();
                myport.BaudRate = 9600;  //default baud rate for my robot
                myport.PortName = "COM5"; //setting which COM port to use
            }
            catch (Exception)
            {
                MessageBox.Show("Can not open COM Port");
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            //this is the enable button. When pressed it will activate the voicce recognition and open the COM port.
            recEngine.RecognizeAsync(RecognizeMode.Multiple);
            button2.Enabled = true;
            myport.Open();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            //this is the disable button. When pressed it will deactivate the voice recognition and close the COM port.
            recEngine.RecognizeAsyncStop();
            button2.Enabled = false;
            myport.Close();
        }
    }
}

Step 4: Now It Is Time to Put It All Together!

The moment we have been waiting for! Start your robot and your program. Once everything is started up click enable and say your first command! Mine worked out well, sometimes the program can be a little weird and repeat the commands multiple times. This has never bothered me so I just accept it. I hope you check out the video and let me know if this has helped you control your robot! Have a good day!

Robotics Contest

Participated in the
Robotics Contest

Tech Contest

Participated in the
Tech Contest