Programming Motor-Bee in Visual Basic


  Although Motor-Bee comes with it's own software (Motor-Way) to allow the beginner to start using it in home automation projects very quickly, it also comes with a DLL interface to allow the intermediate and advanced user to write their own programs for it. The DLL provides a general purpose interface that greatly simplifies the task of writing programs for a USB device. It can be tricky manipulating the USB comms into sending and receiving messages to and from a device which can easily be plugged and unplugged at any time. The DLL eliminates all of these headaches by simplifying the task into three library functions.

InitMotoBee( )
SetMotors (on1, speed1, on2, speed2, on3, speed3, on4, speed4, servo)
Digital_IO(inputs, outputs)

InitMotoBee() is called somewhere near the start of your program and takes care of all of the USB comms initialisation and prepares the Motor-Bee for receiving messages.

SetMotors (on1, speed1, on2, speed2, on3, speed3, on4, speed4, servo)    can then be called at any time during your program to configure the motors.
on1 - on4 are simply boolean values that specify whether the motor is on or off.
speed1 - speed4 are integers in the range 0-255 which specify the motor speed 0 - 100%
servo is an integer that specifies the servo position in approx 1 degree increments. i.e.  128 is midway,   0 is -90degrees and  255 is +90 degrees

Digital_IO(inputs, outputs) can also be called at any time during your program to set the 4 digital outputs and read the state of the 6 digital inputs.
outputs is a 16 bit integer value. bit0 corresponds to output 1, bit 1 to output2, etc.... In each case a logic value of 1 turns the output on and a value of 0 turns it off. For example the statement below would turn on the first three outputs...
Digital_IO(inputs,7)      since 7 in binary is 00000111

inputs is an integer passed by reference. When the function call returns, the first 6 bits of this integer correspond to the on/off state of the 6 digital inputs. For example with inputs 1 and 4 on and the others off, inputs would contain the value 9... since 9 in binary is 00001001.

                                                  What could be simpler……………..

  The only other thing that a VB program must do is to declare the functions that it is going to use within the DLL and the name of the DLL itself. This must be done at the start of your program or at least before any references to the three functions are made. The following is a program excerpt showing how this is done...

Declare Function InitMotoBee Lib "mtb.dll" () As Boolean

Declare Function Digital_IO Lib "mtb.dll" (ByRef inputs As Integer, ByVal outputs As Integer) As Boolean

Declare Function SetMotors Lib "mtb.dll" (ByVal on1 As Integer, ByVal speed1 As Integer, ByVal on2 As Integer, ByVal speed2 As Integer, ByVal on3 As Integer, ByVal speed3 As Integer, ByVal on4 As Integer, ByVal speed4 As Integer, ByVal servo As Integer) As Boolean

    The first declaration states that the function InitMotoBee has no parameters, is found in mtb.dll and returns a boolean value. The second states that Digital_IO has one integer parameter passed by reference and one passed by value, is found in mtb.dll and also returns a boolean value. The third states that SetMotors has 7 integer parameters all passed by value, is found in mtb.dll and, again, returns a boolean value. It should be noted that the
                             Lib "mtb.dll"
                                                   lets the program know where to find the mtb.dll file. When written like this it assumes, since there is no path information, that the mtb.dll file can be found in the windows system directory ( c:\windows\system32 ) If you like you can copy the file mtb.dll on the installation disk to the system32 directory and the above statement will work perfectly. Alternatively you can copy the file to some other location and give that location in the declaration as in the example below...

Declare Function InitMotoBee Lib "c:\mylibrary\mtb.dll" () As Boolean


Source Code   To speed up your development of software for the Motor-Bee a complete working example is available for download below. It is called VBmotor and creates a very simple form based program that has individual buttons for various functions such as initialising the Motor-Bee, setting motor speeds, servo position, digital outputs and displaying digital inputs. The main (and only) screen is shown on the right.  This has been written using Microsoft Visual Studio .net and the download files contain the full workspace (solution) details  to allow you to immediately open and start editing or running this application.

   Even if you don't have Visual Studio, the source code is virtually self explanatory with the most relevant sections being in "Form1.vb" which can even be opened in a simple text editor such as notepad. The files are zipped for convenience and may be downloaded by right clicking on the link below and choosing "save target as" to begin the download.


Download Files


.      .


© Copyright 2008