Arduino interface with Visual Basic Express 2010

Hi.. I just get started to use Arduino interface with Visual Basic.. I successfully done basic tutorial with the click button on VB to light the LED on Arduino.. Using these code :

Arduino code :

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
}

VB code :

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 = "com10" 'change com port to match your Arduino port
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
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 Button2.Click
SerialPort1.Open()
SerialPort1.Write("0")
SerialPort1.Close()
End Sub


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Enter
MsgBox("Warning..!!!", 0 + 0, "Warning")
End Sub
End Class

So, my problem is, how to send input data from Arduino to be processed and display in VB? What I mean is, I'm attaching a push button connected to Arduino pin 2, and whenever I push the pushbutton (not the click button in VB) , the LED will light, and VB will display a notification window.. Thank you..

Serial.flush(); // clear serial port

Which version of the IDE are you using? There is a 99.999% chance that this code does NOT belong in your sketch.

Opening the serial port resets the Arduino. Closing the serial port resets the Arduino. Is it really necessary to reset the Arduino so often?

If Serial.read() is how you get serial data from the PC, what would you guess is the function to print or write data to the serial port for the PC to read?

Arduino 1.0.. Here is where I get the code.. http://www.instructables.com/id/Using-Visual-Basic-to-control-Arduino-Uno/

I did it this way:

Read Arduino in VB:
Public iSerialReceived(50) As Integer

Function SerialReceive() As Integer
Dim iIndex As Integer

On Error GoTo ErrHandler

If SerialPort1.BytesToRead > 0 Then
iIndex = 0 'Initialize

While SerialPort1.BytesToRead > 0
iSerialReceived(iIndex)= SerialPort1.ReadByte() ' Read next byte
iIndex = iIndex + 1
End While

...PROCESS DATA HERE OR SOMEWHERE ELSE...

ErrHandler:
'…PROCESS ERROR…
End Function

In Arduino:
You already send "LED ON"/"LED OFF" via Serial as far as i can see.

Here is where I get the code.

That figures. That site is the repository of the worlds worst projects driven by the worlds worst code.