IAN LANG ELECTRONICS

Which is a long title but means that you can control your Arduino via the USB port from your computer. To do this, you need a programming solution for your computer and fortunately those nice chaps at Microsoft have given us Visual Studio which includes Visual Basic Express and that's what we'll use. The latest version is VB 2010 and you can get it from the link on the left.

Now, there is a utility out there called Firmata which does what we are going to do here without putting us through a steep learning curve or spending our weekends and afternoons developing a serial communications protocol. If you like it, use it, but for my money it's a bit clunky, complex and it doesn't always do what I want it to. The link is on the left if you want to have a look.

 

For my money it doesn't cut it because it's hard to re-work the interface and expects certain kinds of boards to be plugged in. The library is very heavy on the Arduino and I want something that leaves minimal processing to the Arduino and does the bulk of the work on the PC. The Arduino is capable of receiving serial communication and VB is capable of throwing it out. There's a happy coincidence. Now all we need is some way of making them work together.

 

Now, to do this we need to invent some sort of communications protocol. The best way to think of a communications protocol is as a set of commonly agreed instructions between the two devices, and in that respect it's no different from a language. If an Englishman, a Frenchman and a German were standing together at a party, not knowing each others' languages, then neither of them would be able to tell any other that their trousers had fallen down, which would be somewhat embarassing all round. Similarly, we need to send out an instruction from the computer to the Arduino to say what we want it to do, though of course the Arduino's trousers won't have fallen down as it doesn't wear any, unless of course you've made it a little pair of Ardu-britches. I know I haven't. I'm not about to either.

 

Anyway, where were we? Oh yes, a communications protocol. First things first. In Visual Basic 2010 we need to make a form consisting of two buttons and a trackbar. Mine looks like this:

Controlling your Arduino by Host PC

Go Back www.arduino.cc/playground/Interfacing/Firmata Visual Basic

To test this once you've finished coding up you will need to start debugging. Either go to the debug menu and click start debugging or press f5. To stop debugging, go to the debug menu and click stop debugging.

 

To make it independent of the programming environment and stand alone as an executable application, go to the build menu. Build it, and then publish it to a location you know. The executable file will be in this location. You should be able to share with other machines now, providing they have the right components installed.

The buttons and the trackbar are known as objects and they each have names and properties. It doesn't matter what you name them, as long as you refer to them subsequently by that name, but I've named the button that says "Blinky" as blinky and the other one is just  Button 1.

The trackbar I've named tracker. I haven't changed the properties of the buttons other than the texts, but the trackbar properties you need to change to make this work the way it does here are:

Maximum  9   and Minimum  0.      What this does is changes the parameters of the track; at the left its value will be zero and at the right nine.

The fourth thing you'll need to add to the project is a component, a Serial Port.  This is dragged out of the toolbox and put at a tray at the bottom. Again it doesn't matter what name you give it as long as you refer to it by that name throughout. I've called mine comport, and it's the bit that's going to talk to the Arduino. You'll need to change the following properties on this: Baud rate 300; DTRenable False (if it isn't already); Portname to whatever COM your Arduino is on (mine is COM8, yours may well not be) and RTSenable True. For those of you with an enquiring mindset, DTR looks for a modem and sends a signal to it. If your Arduino gets that signal, it resets. RTS is request to send.

 

What we are going to do here is to use one pin of the Arduino to experiment with, Pin 6. We are going to attach an LED to pin 6, and make it switch off and on when we click button 1, blink when we click Blinky, and get brighter or dimmer when we move the tracker.  Here's how we wire:

 

It's a pretty simple construct, and shouldn't take a minute.

But it's no good having our hardware and interfaces made up if there's no code to back it up. So first let's look at the VB code. In Visual Basic, the program is event driven rather than line-driven. Now, whenever I say this, the following conversation usually happens.

 

Other Person:  "Event Driven?"

Me: "Yes".

OP:  "Is it? "

Me "Yes it is".

OP: "Is it really?"

Me:  "It is really".

OP: "Oh good".

Me: "Yes".

OP: "Yes".

Me: "You don't know what I'm talking about do you?"

OP: "Didn't like to say, but no, I haven't the foggiest notion, Old Boy. I thought you were a Bulgarian or something and didn't want to appear rude".

 

So, event driven means it doesn't do anything until the user does something with the GUI, and that's not Bulgarian either, it's the Graphical User Interface which is the form with the buttons and tracker bar as we made above.

We put the code attached to the objects, so that in the case of the buttons it executes whenever we click on one of them, and in the case of the tracker, whenever we move the slider. Button 1 Has the following code behind it:

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 

        On Error Resume Next

        tracker.Value = 0

        Dim onstate, outstring

        onstate = 0

        If Button1.Text = "Pin 6 on" Then

            Button1.Text = "Pin 6 off"

            onstate = 1

            outstring = "!"

        End If

        If Button1.Text = "Pin 6 off" And onstate = 0 Then

            Button1.Text = "Pin 6 on"

            outstring = "$"

        End If

        If comport.IsOpen() = True Then

            comport.Close()

        End If

        comport.Open()

        comport.Write(outstring)

comport.Close()

    End Sub

 

The top and bottom lines are already in there by default. Blinky (the other button) has this code:

 

 Private Sub blinky_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles blinky.Click

 

        tracker.Value = 0

        Button1.Text = "Pin 6 off"

        Dim outstring

        outstring = "%"

        If comport.IsOpen() = True Then

            comport.Close()

        End If

        comport.Open()

        comport.Write(outstring)

        comport.Close()

 

    End Sub

 

and tracker (the trackbar):

 

 Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tracker.Scroll

 

        On Error Resume Next

        Button1.Text = "Pin 6 off"

        Dim outstring

        outstring = tracker.Value

        If tracker.Value > 0 Then

            Button1.Text = "Pin 6 off"

        Else

            Button1.Text = "Pin 6 on"

        End If

        If comport.IsOpen() = True Then

            comport.Close()

        End If

        comport.Open()

        comport.Write(outstring)

        comport.Close()

 

    End Sub

 

All of this code is three independent modules dedicated to SENDING data. The Arduino is coded up to RECEIVE the data via the USB port like so:

 

char lastincoming;

void setup(){

  Serial.begin(300);

  pinMode(6,OUTPUT);

}

 

void loop(){

  char incoming=Serial.read();

  if (incoming>-1){

    lastincoming=incoming;}

if (lastincoming==33){

digitalWrite(6,1);}

if (lastincoming==36){

digitalWrite(6,0);}  

if (lastincoming==37){

digitalWrite(6,0);delay(100);digitalWrite(6,1);delay(100);}

for (int t=48;t<59;t++){

  if (lastincoming==t){

    int n=0;

  n=t-48;n=n*25;analogWrite(6,n);}}

   

}

 

So as you can see more processing goes on at the VB end than does at the Arduino. You can also see the difference between an event-driven program (the GUI does nothing until it's told to) and line-driven (the Arduino goes around the loop all the time regardless of whether it's told to or not). You can't really convert event-driven to line-driven or vice versa but you can lock out bits of code or bring in such so that it has the same effect. Which is handy, because that's what we're going to do.

 

Over the page then, we'll start looking at the Arduino receiving code and how it's affected by the serial communications from the computer.

More