Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello guys.. i wondering is there any wrong with my code since i try to run it just now and noting happen

code of arduino
C#
const int threhold = 100;
int sensorReading;
int irs=0;
void setup()
{
 pinMode(irs, INPUT);
Serial.begin(9600);
}
void loop()
{
  sensorReading = analogRead(irs);
  if(sensorReading > threhold)
  {
   Serial.write("1");
  }
  else
  {
   Serial.write("0");
  }
  delay(100);
}


code of vb
VB
Imports System.IO
Imports System.IO.Ports

Dim val As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Encoding = System.Text.Encoding.Default
        SerialPort1.Open()
        val = SerialPort1.ReadLine
        If val = 0 Then
            PictureBox1.Image = My.Resources.Button_Blank_Red_icon__1_
        ElseIf val = 1 Then
            PictureBox1.Image = My.Resources.Button_Blank_Green_icon_2
        End If
    End Sub
Posted
Comments
[no name] 18-Jun-13 8:13am    
Try sending a newline from the arduino side since that is what the serial port ReadLine is waiting for,
arnoldxx 19-Jun-13 5:53am    
can u gime an example bro.. sorry i m still new in this progress.. might need some help from you guys.. thanks..

1 solution

You are sending (Arduino side) string values and checking (on VB side) for integer values. Instead you should either:
  • send integer values (e.g. Serial.Write(1)) and check for integer values.

or
  • send string values and check for string values (e.g. if str = "0" ).
 
Share this answer
 
Comments
arnoldxx 18-Jun-13 6:57am    
ty for the feedback..
i have follow the step that u show it to me by changing the if val = 0 to if val = "0"
but it still won't work to me..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900