Arduino & Visual Basic 6.0 (Continuous Servo Motor)



This Instructable is almost similar to the Light Controller posted previously. It contains the same structure and pattern in controlling the Servo Motor. I used some ideas form Galil Motor Control structures to create my parser. Namely, SH (Servo Here) and some others. Galil is one of the motion control cards used in automation industries. For more info on Galil:

My application does not support multi-threading but it is achievable by using interrupts on the Arduino. I think multithreading refers to controlling motors in a parallel manner where they operate together. Instead of using the classes built on my previous post , i made it into DLL files which could be used in any other applications that could use its function.

Step 1: Visual Basic 6.0 (Making DLL)

When making a DLL in VB6.0, always create independent class. Inside the class, there could be as much Public (accessible by user) and Private (Only accessible by internal functions) functions. It is a good practice to create classes that has specific functions so it could also be used in future projects. Make sure you save the project and class file before making the DLL so you could modify it in the future. *A DLL cannot be modified* without its main project/class files. When creating DLL, make sure:

1. Class is independent, Declare variables as private (more privacy)
2. Do not accept objects into class. Instead, accept 'variants' then 'set' to a privately declared object.

                  Private MC as MSComm
                  Public sub InitializeClass (Byval fileName as String, MsComm as Variant)
'instead of
                  Public sub InitializeClass (Byval fileName as String, MsComm as MSComm)
    Then, Set MC = MsComm
3. Others??? (I have only encountered the problem stated in 2.)

My ServoFunction

Private The_File As String, The_Section As String
Private MComm As MsComm, tBox As TextBox
Private TextLength As Long, Command As String

Private Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Const LocWordLength = 20

Public Sub InitServo(PortSettingFileName As String, FileSection As String, MsComm As Variant, txtBoxFeedback As Variant)
The_File = PortSettingFileName
The_Section = FileSection
Set MComm = MsComm
Set tBox = txtBoxFeedback

'Output loaded COMPORT settings
Log "File: " & The_File & vbCrLf & "Section: " & The_Section

'Open ComPort and connect with Arduino
With MComm
If .PortOpen Then .PortOpen = False 'close and set the com port number
.CommPort = SimpleGet("comport")
.Settings = SimpleGet("settings")
.EOFEnable = True
Log "Com Port: " & .CommPort & vbCrLf & "Settings: " & .Settings
End With

SendData ("CO")
End Sub

Public Sub SendData(ByVal Commandx As String)
On Error GoTo Error
Command = Commandx
'check command first
Command = Trim(Command) 'get rid of extra spaces at the side
If (InStr(Command, " ") <> 0) Or (Len(Command) > 10) Then
GoTo Error 'do not send if command format is wrong
End If

Dim i As Integer, Char As String

For i = 1 To 2 'because length of ecpected character is 2 only
Char = Mid(Command, i, 1)

If (Char >= "A" And Char <= "Z") Then 'do nothing if first 2 characters are alphabets
GoTo Error
End If
Next i

If InStr(Char, "C") = 0 Then 'check for numerals for input not related to CT and CO
For i = 3 To Len(Command) 'make sure the rest of the command are integers
Char = Mid(Command, i, 1)

If (Char >= "0" And Char <= "9") Then 'do nothing if remaining characters are numerals
GoTo Error
End If
Next i
End If

If InStr(Command, "L") = 0 Then 'If command does not prompt for Location
TextLength = Len(Command)
TextLength = LocWordLength
End If

With MComm
.DTREnable = False
.RTSEnable = False 'disable request to send signal
If .PortOpen = False Then .PortOpen = True 'Open port
.Output = Commandx 'Send Text
.RThreshold = TextLength 'Save Sent String Length
End With 'leave port open to wait for echo signal to proceed

Exit Sub
If (Err.Description) Then
MsgBox Err.Description
MsgBox "Invalid Command!"
End If
End Sub

Private Sub Log(Text As String)
On Error GoTo ERRR
tBox.Text = Text & vbCrLf & tBox.Text
Exit Sub
MsgBox "Error while logging: " & Err.Description
Resume Next
End Sub

'Manual OnComm Function to detect echo of sent data
Private Sub ServoOnComm()
Dim InString As String

Loop Until MComm.CommEvent = comEvReceive And MComm.InBufferCount >= TextLength

Sleep (5)
If TextLength = LocWordLength Then 'wait for remaining characters to reach buffer
Sleep (30)
End If

' Retrieve all available data.
MComm.InputLen = 0

' Check for data.
If MComm.InBufferCount > 0 Then ' Read data.
InString = MComm.Input
'check if received data is as expected
If InStr(InString, Command) > 0 Then 'if command is an echo

ElseIf InStr(Command, "L") <> 0 Then 'if original command has L and echo is long
If Len(InString) >= LocWordLength Then

End If
GoTo Error
End If

If Len(InString) > 0 Then 'Output echo onto textbox
tBox.Text = InString & tBox.Text
End If
End If

If MComm.PortOpen Then MComm.PortOpen = False 'close port after receiving reply
Exit Sub
MsgBox "Receive Error!"
End Sub

Public Sub ExitServo()
SendData ("CT")
If MComm.PortOpen Then MComm.PortOpen = False 'If port is open, close if before exit

'maybe can add to home servo before exiting
End Sub

Public Function SimpleGet(VarName As String) As String
Static sLocalBuffer As String * 500
Dim l As Integer

l = GetPrivateProfileString(The_Section, VarName, vbNullString, sLocalBuffer, 500, The_File)
SimpleGet = Left$(sLocalBuffer, l)

End Function

Step 2: Run

The video shows how program is loaded and run on the Arduino and Servo. To fully utilize the Gmail loading, Open the Gmail.ini file and key in your username and password. Remember not to forget you've written your private data in a file.
The application will only load one unread message. If you have more than one unread message, it will display mail not available.
Additional functions that you can add on yourself is the timer (check mail automatically when timer is triggered)
Feel free to ask any questions. :)
Sorry for the bad quality video...



    • Tape Contest

      Tape Contest
    • Arduino Contest 2019

      Arduino Contest 2019
    • Trash to Treasure

      Trash to Treasure