Arduino BT + Serial Communication using VB

I am using a basic VB program to turn a light on and off on my Arduino BT, however it does not seem to be working. I tested the program with my Diecimilia and it worked fine. Im not sure what im doing wrong, please help! Also note that i was able to load the program onto the BT.

Arduino Code:

void setup(){
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}

void loop(){
  int val;
  if (Serial.available()) {
  
  delay(100);
  
  while (Serial.available() > 0) {
       val=Serial.read();
    
       if(val=='1') { digitalWrite(13,HIGH); }
       else if (val=='0') {digitalWrite(13,LOW);
     }
   }
 }
}

VB Code:

mports 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 = "COM14" '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 btnOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOn.Click
        picOn.Visible = True
        SerialPort1.Open()
        SerialPort1.Write("1")
        SerialPort1.Close()
    End Sub

    Private Sub btnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOff.Click
        picOn.Visible = False
        SerialPort1.Open()
        SerialPort1.Write("0")
        SerialPort1.Close()
    End Sub
End Class

Every time you open the serial port, the Arduino resets. Every time you close the serial port, the Arduino resets.

PaulS:
Every time you open the serial port, the Arduino resets. Every time you close the serial port, the Arduino resets.

So your saying that i should just leave the port open? (Sorry if newb question, still kinda new to arduino)

So your saying that i should just leave the port open?

You should have icons that trigger callbacks in which you open or close the serial port. In the "send some data" callbacks, do not open and close the port.

LOL I figured out what i did wrong. Instead of having the serial at 115200 baud, i left it a 9600. After reading the Arduino BT page, I realised it HAS to be set to 115200 baud. Helps to read the instructions sometimes :stuck_out_tongue:

Thanks anyway!