Create an application with VB.NET to control the arduino
In this example, we are going to insert 2 buttons to control the LED via arduino
Create a new Project and insert a form then add
1 combo-box
3 buttons
Code for the combobox
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If (ComboBox1.SelectedItem <> "") Then
comPORT = ComboBox1.SelectedItem
End If
End Sub
Code for Button 1 (Connect Button)
Private Sub Button1_Click(ByVal
sender As Object, ByVal e As EventArgs) Handles Button1.Click
If (Button1.Text = "Connect") Then
If (comPORT <> "") Then
SerialPort1.PortName = comPORT
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default
SerialPort1.ReadTimeout = 10000
Button1.Text = "Dis-connect"
Else
MsgBox("Select a COM port first")
End If
Else
SerialPort1.Close()
Button1.Text = "Connect"
End If
End Sub
Code for ON button
Private Sub Button2_Click(ByVal
sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Open()
SerialPort1.Write("1")
SerialPort1.Close()
End Sub
Code for OFF Button
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SerialPort1.Open()
SerialPort1.Write("0")
SerialPort1.Close()
End Sub
Save the project and build it
Run Arduino IDE and load below program in it
int ledPin = 13; // the number of the LED pin
void setup() {
Serial.begin(9600); // set serial speed
pinMode(ledPin, OUTPUT); // set LED as output
digitalWrite(ledPin, LOW); //turn off LED
}
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) { // test for command 1 then turn on LED
Serial.println("LED on");
digitalWrite(ledPin, HIGH); // turn on LED
}
else if (val == 0) // test for command 0 then turn off LED
{
Serial.println("LED OFF");
digitalWrite(ledPin, LOW); // turn off LED
}
else // if not one of above command, do nothing
{
//val = val;
}
Serial.println(val);
Serial.flush(); // clear serial port
}
Connect PIN 13 to LED positive and other end ground
Run the VB project from either Debug/Release directory
Click Button 1 to turn LED ON and Button 2 to turn LED OFF
Download complete project from http://www.computeraidedautomation.com/