Introduction: 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: http://www.galilmc.com/
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 https://www.instructables.com/id/Arduino-Visual-Basic-6-Light-Controller/ , 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.)
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
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
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
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
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
If InStr(Command, "L") = 0 Then 'If command does not prompt for Location
TextLength = Len(Command)
TextLength = LocWordLength
.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
If (Err.Description) Then
MsgBox "Invalid Command!"
Private Sub Log(Text As String)
On Error GoTo ERRR
tBox.Text = Text & vbCrLf & tBox.Text
MsgBox "Error while logging: " & Err.Description
'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
If TextLength = LocWordLength Then 'wait for remaining characters to reach buffer
' 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
If Len(InString) > 0 Then 'Output echo onto textbox
tBox.Text = InString & tBox.Text
If MComm.PortOpen Then MComm.PortOpen = False 'close port after receiving reply
MsgBox "Receive Error!"
Public Sub ExitServo()
If MComm.PortOpen Then MComm.PortOpen = False 'If port is open, close if before exit
'maybe can add to home servo before exiting
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)
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...
We have a be nice policy.
Please be positive and constructive.