Home > Projects > Other Projects > Arduino & Visual Basic 6 Light Controller

Arduino & Visual Basic 6 Light Controller

Summary of Arduino & Visual Basic 6 Light Controller


This project links an Arduino UNO to a Visual Basic 6 (VB6) application via USB serial to control six LEDs (using PWM-capable pins), demonstrating string parsing, file-based command loading, and Gmail-based remote command retrieval. It includes Arduino sketches, VB6 project files with class modules for modularity, and Fritzing schematics, adapted parsing routines, and functions for reading text files, serial command parsing, and Gmail inbox polling.

Parts used in the Arduino & Visual Basic 6 Light Controller:

  • Arduino UNO
  • USB cable for serial connection
  • LEDs (6)
  • Current-limiting resistors for each LED
  • Breadboard or PCB for wiring
  • Jumper wires
  • Fritzing schematic file (project resource)
  • Arduino sketch archive Light.rar (software resource)
  • VB6 project archive VBLight.rar (software resource)

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.

Arduino & Visual Basic 6 Light Controller

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.

Step 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.

 

Arduino & Visual Basic 6 Light Controller circuit

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

Quick Solutions to Questions related to Arduino & Visual Basic 6 Light Controller:

  • What microcontroller is used in the project?
    The project uses an Arduino UNO as the microcontroller.
  • How many LED outputs are controlled?
    Six output pins were utilized to control LEDs, chosen to take advantage of PWM functionality.
  • Can commands be loaded from a file?
    Yes, VB6 includes a FileToArray function to read text files and load their contents into an array for commands.
  • Does the project include schematics for wiring?
    Yes, a Fritzing schematic was created and included as a reference for hardware assembly.
  • How does the VB6 program retrieve remote commands?
    The VB6 program can load commands from a Gmail inbox by polling the Gmail feed and parsing messages.
  • Are parsing routines original to the project?
    Parsing logic was adapted from an online resource by pwillard to handle string manipulation for serial communication.
  • Is modularity addressed in the VB6 code?
    Yes, some functions were encapsulated as class modules to allow creation of DLLs for reusability.
  • Are example code and project files provided?
    Yes, the Arduino sketch (Light.rar) and VB6 project files (VBLight.rar) are attached as resources.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top