Summary of Arduino & Visual Basic 6 Light Controller
This project demonstrates how to control an Arduino-based LED system using Visual Basic 6 (VB6) through serial communication over USB. It integrates hardware (Arduino UNO) programmed for PWM-based LED control with software developed in VB6 for reading command files, parsing instructions, and even fetching commands from a Gmail inbox for automated control. The project highlights legacy programming language use with modern hardware and includes examples of file parsing, serial data handling, and Gmail API interaction for command retrieval.
Parts used in the Arduino & Visual Basic 6 Light Controller:
- Arduino UNO
- LEDs (6 pieces)
- Connecting wires
- USB cable for serial communication
- Computer running Visual Basic 6.0
- Software libraries for serial communication (VB6 modules)
- Fritzing software for circuit schematic design
Arduino & Visual Basic 6 Light Controller
This project illustrates an early attempt at bridging Visual Basic 6 (VB6) with the Arduino platform through serial communication over a USB interface. While the concept originated as a beginner’s exercise in VB6 after only two weeks of learning, it highlights important principles of cross-platform interaction, string parsing, and hardware-software integration.
The work demonstrates not only how VB6 can be applied to microcontroller projects but also how external services, such as Gmail, can be interfaced programmatically to extend automation beyond the local system. This makes it both a practical learning exercise and an exploration of legacy programming languages in modern embedded contexts.
Step 1: Arduino Programming and Circuit Design
Development begins at the lowest hardware level with the Arduino UNO. The Arduino must first be coded to handle basic LED control using digital outputs. For this prototype, six output pins were utilized, chosen deliberately to take advantage of Pulse Width Modulation (PWM) functionality.
The schematic for the system was created using Fritzing, a popular open-source electronics design tool. This diagram ensures clarity in wiring and provides a reusable reference for hardware assembly.
A significant portion of the parsing logic was derived from an online resource shared by pwillard, whose work on the “Live for Speed” Racing Simulator included efficient string manipulation routines. These parsing techniques were adapted here to allow smooth serial communication and command interpretation between VB6 and the Arduino.
Attached resources include the Arduino sketch (Light.rar) and the VB6 project files (VBLight.rar), which can be used as direct references for replication or extension.
Light.rar2 KB
VBLight.rar427 KBStep 2: Visual Basic 6.0 Programming
On the software side, the focus shifts to Visual Basic 6.0. Though dated, VB6 still provides a relatively straightforward environment for serial communication tasks. To improve modularity, certain functions were encapsulated as class modules, which allows the creation of DLLs for reusability.
File Parsing Functions
One of the first functions implemented enables VB6 to read text files and load their contents into an array. This provides a simple mechanism for pre-loading command sets into the program for execution. Error handling is integrated to ensure robustness in the presence of invalid files or read failures.
Program Code
Public Function FileToArray(ByVal filename As String) As String On Error GoTo Error Dim items() As String, i As Integer ' Read the file's contents, and split it into an array of strings.(Exit here if any error occurs.) items() = Strings.Split(ReadTextFileContents(filename), vbCrLf) For i = LBound(items()) To UBound(items()) FileToArray = FileToArray & vbCrLf & items(i) Next MsgBox "Commands successfully loaded!" Exit Function Error: MsgBox "Error in FileToArray: " & Err.Description End Function 'read entire context in a file Public Function ReadTextFileContents(filename As String) As String Dim fnum As Integer, isOpen As Boolean On Error GoTo Error_Handler ' Get the next free file number. fnum = FreeFile() Open filename For Input As #fnum ' If execution flow got here, the file has been open without error. isOpen = True ' Read the entire contents in one single operation. ReadTextFileContents = Input(LOF(fnum), fnum) ' Intentionally flow into the error handler to close the file. Error_Handler: ' Raise the error (if any), but first close the file. If isOpen Then Close #fnum If Err Then Err.Raise Err.Number, , Err.Description End Function
End Function
After that, i found the program to load inbox messages from Gmail (http://www.j4mie.org/2008/02/15/how-to-make-a-physical-gmail-notifier/ ). I applied this function to enable loading commands from your Gmail inbox to run the Light Controller.
Program Code
Option Explicit
Private m_TheFile As String, m_TheSection As Variant
Private Username As String, Password As String, iTemp() As String
Private pForm As Form, pTimer As Timer, ptxtBox As TextBox, pInet As Inet
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 Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Sub initGmailAccount(TheFile As String, TheSection As Variant, fForm As Variant, fTimer As Variant, ftxtBox As Variant, fInet As Variant)
On Error GoTo ERRR
m_TheFile = TheFile
m_TheSection = TheSection
Set pForm = fForm: Set pTimer = fTimer: Set ptxtBox = ftxtBox: Set pInet = fInet
Log "INI: " & m_TheFile & vbCrLf & "Section: " & m_TheSection
pTimer.Enabled = False 'stop the timer!
pTimer.Interval = SimpleGet("interval") * 1000 'set the timer!
pTimer.Enabled = True 'start the timer!
Log "Interval: " & pTimer.Interval / 1000 & " seconds"
Username = SimpleGet("username")
Log "Username: " & Username
Password = SimpleGet("password")
Log "Password: **********"
Log "Settings Loaded..."
Exit Sub
ERRR:
Log "Error in LoadSettings: " & Err.Description
Resume Next
End Sub
Public Function CheckMail(ByVal ToTextFile As String) As Boolean
On Error GoTo ERRR 'error handling. a must.
Dim STRTemp As String 'in "strtemp" we put the whole web page
Dim mailCount As String, mailTitle As String, mailSummary As String
STRTemp = pInet.OpenURL("https://" & Username & ":" & Password & "@mail.google.com/gmail/feed/atom")
STRTemp = UCase(STRTemp)
mailCount = Right(STRTemp, Len(STRTemp) - InStr(1, STRTemp, "FULLCOUNT") - 9)
mailCount = Left(mailCount, InStr(1, mailCount, "<") - 1) mailTitle = Right(STRTemp, Len(STRTemp) - InStr(1, STRTemp, "TITLE>L") - 5)
mailTitle = Left(mailTitle, InStr(1, mailTitle, "<") - 1)
If StrComp(mailTitle = "LIGHTCONTROL", vbTextCompare) = 0 & mailCount = "1" Then
mailSummary = Right(STRTemp, Len(STRTemp) - InStr(1, STRTemp, "SUMMARY") - 7)
mailSummary = Left(mailSummary, InStr(1, mailSummary, "<") - 1)
'load message into public variable
iTemp() = Strings.Split(mailSummary, ";")
'save mail data into a textfile
Open ToTextFile For Output As #1
Dim i As Integer
For i = LBound(iTemp()) To UBound(iTemp())
Print #1, iTemp(i)
Next
Close #1
CheckMail = True
Else
Log "Mail not available!!!"
CheckMail = False
End If
Exit Function
ERRR:
Log "Error in CheckMail: " & Err.Description
Resume Next
End Function
Public Sub Log(Text As String)
On Error GoTo ERRR
ptxtBox.Text = Text & vbCrLf & ptxtBox.Text
Exit Sub
ERRR:
MsgBox "Error while logging: " & Err.Description
Resume Next
End Sub
Public Function SimpleGet(VarName As String) As String
Static sLocalBuffer As String * 500
Dim l As Integer
l = GetPrivateProfileString(m_TheSection, VarName, vbNullString, sLocalBuffer, 500, m_TheFile)
SimpleGet = Left$(sLocalBuffer, l)
End Function
Public Sub SimplePut(TheItem As Variant, TheVal As Variant)
Call WritePrivateProfileString(m_TheSection, CStr(TheItem), CStr(TheVal), m_TheFile)
'Flush buffer
Call WritePrivateProfileString(0, 0&, 0&, m_TheFile)
End Sub
For more detail: Arduino & Visual Basic 6 Light Controller


