Hello, i am using the following basic VB program to turn a light on my Arduino BT via serial communication. When i click “on” in the program, the light wont turn on. Not exactly sure what i’m doing wrong. I tested it with my diecimilia and it works fine. Is there something special i need to do to make it work with my Arduino 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
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 = "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