Pages: [1]   Go Down
Author Topic: Arduino BT + Serial Communication using VB  (Read 580 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 616
Posts: 49444
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 616
Posts: 49444
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-razz

Thanks anyway!
Logged

Pages: [1]   Go Up
Jump to: