This is part 3 of the Creating a Visual Basic Interface for Arduino Tutorial.
In the last part of the tutorial, we’re going to program that Windows form we design in the previous part of the tutorial. If you haven’t already downloaded the code, you can grab it at GitHub.
Understanding the Process
Before we start taking a look at the code behind our form controls, let’s lay out what a typical use of the program should roughly look like:
- User selects COM port and clicks Connect button.
- Program tries to connect to Arduino. If it can’t, display an error message. If it can, display a success message and remove that Panel that was hiding our controls.
- User selects a pin number in a ComboBox, and moves around a TrackBar to select a position/speed for the servo. This value is displayed in a textbox.
- Regardless of any input from the user, the VB program sends the values to the Arduino every 100ms, which is the interval of the Timer.
The program also offers a way to set a zero position… The home position of a 180º servo, or the stop value of a continuous rotation servo motor. When the user clicks the Button, “btnZero”, change the variable, “Zero”, to the current value. When the user clicks another Button, “btnSetZero”, set the TrackBar and “txtSpeed” TextBox to the value of the variable “Zero”. The Arduino gets the zero value because it’s sent shortly after because of the Timer.
Hopefully you’re still following. Let’s take a look at the actual code.
frmMain_Load (When the program starts)
cboxCOMport.SelectedItem = “COM3”
cboxPin.SelectedItem = “11”
panelHide.Visible = True
txtSpeed.Text = Zero
txtZero.Text = Zero
These fives lines in frmMain_Load set some default values, as well as make sure the Panel to hide the controls is visible. The first two lines set the default values for the drop down ComboBoxes for the COM port and servo pin number. The third line makes sure the Panel is visible. The fourth and fifth line sets the speed and zero TextBoxes to the value of the Zero variable, which is declared globally.
My Arduino typically connects via COM port 3 and I was using pin 11 for the servo, so you can change these to fit your setup.
btnConnect_Click (When the Connect button, “btnConnect”, is clicked)
Dim ConnectSuccess As Boolean = True
SerialPort.PortName = cboxCOMport.Text
SerialPort.BaudRate = 9600
SerialPort.DataBits = 8
SerialPort.Parity = Parity.None
SerialPort.StopBits = StopBits.One
SerialPort.Handshake = Handshake.None
SerialPort.Encoding = System.Text.Encoding.Default
ConnectSuccess = False
Before the Try-Catch statement, we initialized the boolean variable, “ConnectSuccess” as true. If there is an error executing the code between Try and Catch, the code in Catch will change the variable to false and close the serial connection. If there is no problem, the serial connection remains open.
If ConnectSuccess = True Then
btnConnect.Enabled = False
btnConnect.Text = “Connected!”
MsgBox(“Connected to Arduino using ” & cboxCOMport.Text & “!” & vbNewLine, MsgBoxStyle.Information, “Success”)
panelHide.Visible = False
Timer1.Enabled = True
If the serial connection was a success, disable the Connect button and change the text in the button to read, “Connected!”. Display a message box that says the connection was successful. Make the Panel that was hiding the controls invisible. Finally, enable the Timer that is used to continuously send data to the Arduino.
MsgBox(“Could not connect using ” & cboxCOMport.Text & “!” & vbNewLine & “Please try again!”, MsgBoxStyle.Exclamation, “Error”)
If the serial connection failed, display an error message.
Timer1_Tick (Whenever the Timer interval elapses)
For good practice, clear the serial buffers, before writing the pin number and speed TrackBar value to the serial port. PadLeft() is to make sure there is always 5 characters sent in total. Otherwise, any value under 100 would cause the Arduino to think there’s an error because it would only receive 3 or 4 characters.
trkSpeed_Scroll (Whenever the TrackBar is moved)
txtSpeed.Text = trkSpeed.Value
The TrackBar is the only way to change the servo speed/position value, but we have a TextBox to display exactly what value we have on the TrackBar. This line of code puts the value of the TrackBar into the TextBox.
btnZero_Click (Whenever the button labelled “Set current value as 0” is clicked)
Zero = trkSpeed.Value
txtZero.Text = Zero
The first line adjusts the variable “Zero” to whatever the current speed/position value is set. The second line changes the text in a TextBox that displays the Zero value, “txtZero”, to the new Zero value.
btnSetZero_Click (Whenever the button labelled “0” is clicked)
trkSpeed.Value = Zero
txtSpeed.Text = Zero
This code sets the TrackBar and the current speed TextBox, “txtSpeed”, to the value of the variable, “Zero”. Basically, it homes a 180º servo motor or stops a continuous rotation servo motor.
frmMain_FormClosing (When the user quits the program)
If (SerialPort.IsOpen) Then
Just before the program closes completely, we want to send the Zero value so that it will home or stop the servo motor. We should only try sending it if there is an open serial connection. Close the serial connection.
I hope you enjoyed this tutorial! If you have any questions, please feel free to leave a comment in the appropriate post. I know all of this code can be overwhelming for some, so if there is any part that needs more clarification, I’ll be happy to help!
Thanks for visiting!