Introduction: Using VB to Control Arduino
I know there are many instructables on this topic, but I couldn't find one which doesn't required edit the code to add the serial port.
So, I made one program that will ask the serial port and baud rate to use on startup.
Step 1: Requirements
- Any Arduino (I am using Arduino Uno)
Visual Basic Express 2010
You can download VB from here
Step 2: Making the VB Program
In this instructable, we will be making a program to control leds, using 4 buttons. Follow the following steps.
- Click on New Project
- Select New Windows From Application
- Enter a name for your application
- Drag & drop 4 Buttons from the Toolbox onto the Form1
- Drag & drop one SerialPort also
- Double click on the form. You will see the coding area.
- Copy-paste the following code there.
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = InputBox("Please enter the COM port")
SerialPort1.BaudRate = InputBox("Please enter the baudrate")
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
SerialPort1.Write("1")
SerialPort1.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
SerialPort1.Write("2")
SerialPort1.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
SerialPort1.Write("3")
SerialPort1.Close()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
SerialPort1.Write("4")
SerialPort1.Close()
End Sub
End Class
Step 3: Verifying the VB Code and Building the Program
Press F5 to start debugging.
If everything is correct, an input box will appear, asking you to enter the COM port. Type your port, for example COM1. Note that you have to type com1, not just 1.
After pressing OK, next input box will ask for baud rate. Enter the baud rate that you are using in the arduino.
Then you will see the 4 buttons. Do not press any buttons, as you will get an error "The port 'com21' does not exist.", as we haven't connected the arduino yet.
Then select Build from Debug menu. Go to the location shown in the Output box to get your program.
Step 4: Arduino Sketch
Upload the following code to your arduino.
int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 5;
int ledPin5 = 6;
void setup(){
Serial.begin(9600);//set serial speed. Enter this value for baud rate
}
void loop(){
while (Serial.available() == 0); // do nothing if nothing sent
int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number
if (val == 1){//for button 1
digitalWrite(ledPin1,HIGH);
delay(500);
digitalWrite(ledPin2,HIGH);
delay(500);
digitalWrite(ledPin3,HIGH);
delay(500);
digitalWrite(ledPin4,HIGH);
delay(500);
digitalWrite(ledPin5,HIGH);
delay(500);
}
else if (val == 2){//for button 2
digitalWrite(ledPin5,LOW);
delay(500);
digitalWrite(ledPin4,LOW);
delay(500);
digitalWrite(ledPin3,LOW);
delay(500);
digitalWrite(ledPin2,LOW);
delay(500);
digitalWrite(ledPin1,LOW);
delay(500);
}
else if (val == 3){//for button 3
digitalWrite(ledPin1,HIGH);
digitalWrite(ledPin2,HIGH);
digitalWrite(ledPin3,HIGH);
digitalWrite(ledPin4,HIGH);
digitalWrite(ledPin5,HIGH);
} else if (val == 4){//for button 4
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
} else{//if not one of above command, do nothing
}
Serial.flush(); // clear serial port
}
Step 5: Testing
Open the VB program that you created.
Enter the serial port and speed.
Click on the buttons to see what happens.
For button1, leds 1-5 sequentially light up afer 0.5 seconds.
For button2, leds 5-1 sequentially turn off after 0.5 seconds.
For button3, all leds light up together.
For button4, all leds turn off together.