What value is my Visual Basic application sending to the Arduino?

I have developed a small system whereby I am using an LM35 temperature sensor to monitor room temperature. I f the temperature goes below a user defined value, a red LED should light simulating a heater. If the temperature goes above the user defined value, a blue LED comes on simulating a cooler.
The user inputs the preffered room temperature in a textbox in a visual basic application. the problem is that whatever value is send to the serial port, only the heater comes on. It seems the arduino is receiving something different than what I am typing in the application. The system is however working perfectly when I use a stativ temperature value in the code. i.e without using the visual basic 2010 application. here is the code:

visual basic:

[Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

End Sub

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim value As Integer

SerialPort1.Open()
value = TextBox1.Text
SerialPort1.Write(value)
SerialPort1.Close()

MsgBox("Your system has been updated", MsgBoxStyle.OkOnly, Title:="updated")
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SerialPort1.Close()
SerialPort1.PortName = "com36"
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default

End Sub
End Class]

this is the arduino code:

[#include <SoftwareSerial.h>

const int lm35= A0;
SoftwareSerial mySerial(lm35,13);
const int fan = 8;
const int heater = 7;
int value ;

void setup(){
pinMode(fan, OUTPUT);
pinMode(heater, OUTPUT);
Serial.begin(9600);
// mySerial.begin(9600);
pinMode(lm35, INPUT);
}
void loop(){

int temp1= analogRead(lm35);
int temp=(5.0temp1100)/1024;
delay (10000);
//Serial.println(temp);

if (Serial.available()){
delay(1000);
while (Serial.available()>0){
value=Serial.read();
delay(100);

if(temp > value){
digitalWrite(fan,HIGH);
digitalWrite(heater, LOW);}

else if(temp<value){
digitalWrite(fan, LOW);
digitalWrite(heater,HIGH);}
else if(temp==value){

digitalWrite(fan, LOW);
digitalWrite(heater, LOW);}
}
}
}]

Please read the how to use this forum sticky and modify that post to use the correct code tags.

The arduino bit is all wrong.

while (Serial.available()>0){
  value=Serial.read();

Simply keeps over writing value with the ASCII representation of the digits you are sending from visual basic. So assume VB sends 45, then first you receive 0x34 and you go and do stuff on that, then you read 0x35 and do sutff with that. Which I suspect is not what you want to do.

so should I send a string from VB to the Arduino?

And yes I don't want the application to do that. The idea is if I type 23 in a text, the micro-controller receives 23 and actuates the appropriate mechanism.

wambui:
And yes I don't want the application to do that. The idea is if I type 23 in a text, the micro-controller receives 23 and actuates the appropriate mechanism.

Then you need to program the arduino to recieve and ASCII string and cope with the results. This involves either using one of the new call back functions or putting the recieved bytes one by one into a char array until you get a terminator then adding a null at the end. Only then can you start to extract the information from that string.

Ok . Thankyou I'll try that.

I modified my code to handle one ascii character at a time as shown below but it is still not working.

[

#include <SoftwareSerial.h>

const int lm35= A0;
const int fan = 8;
const int heater = 7;

unsigned long serialdata;
int inbyte;


void setup(){
 pinMode(fan, OUTPUT);
 pinMode(heater, OUTPUT);
 Serial.begin(9600);
pinMode(lm35, INPUT);
}

long getSerial()
{
  serialdata = 0;
  while (inbyte != '/')
  {
    inbyte = Serial.read();  
    if (inbyte > 0 && inbyte != '/')
    { 
      serialdata = serialdata * 10 + inbyte - '0';
      //Serial.println(serialdata);
    }
 
  
  return serialdata;
  inbyte = 0;

}}
void loop(){
getSerial();

  
 int temp1= analogRead(lm35);
 int temp=(5.0*temp1*100)/1024;

if(temp > serialdata){
  digitalWrite(fan,HIGH);//PWM: it is assumed that room temperature cannot go above 40 degrees celcius
   digitalWrite(heater, LOW);}
   
  else if(temp<serialdata){
 digitalWrite(fan, LOW);
digitalWrite(heater,HIGH);}//PWM: it is assumed room temperature cannot go below 0 degrees celcius
  else if(temp==serialdata){
    
  digitalWrite(fan, LOW);
  digitalWrite(heater, LOW);}
}

]

You are reading data from the serial port without checking or holding until there is data available. This results in you reading a lot of 0xff bytes as that is what serial read gives you when the input buffer is empty.

Also are you typing a single letter followed by a /

Thank you for all the advice since I first posted. I noticed that the previous code was having issues. I decided to use arrays to store the data as follows:

[

#include <SoftwareSerial.h>

const int lm35= A0;
const int fan = 8;
const int heater = 7;

dec inData[10];
int index;
boolean started = false;
boolean ended = false;
int inInt;

int temp1= analogRead(lm35);
 int temp=(5.0*temp1*100)/1024;

void setup(){
 pinMode(fan, OUTPUT);
 pinMode(heater, OUTPUT);
 Serial.begin(9600);
pinMode(lm35, INPUT);
}



void loop()
{
   while(Serial.available() > 0)
   {
       char aChar = Serial.read();
       if(aChar == '<')
       {
           started = true;
           index = 0;
           inData[index] = '\0';
       }
       else if(aChar == '<')
       {
           ended = true;
       }
       else if(started)
       {
           inData[index] = aChar;
           index++;
           inData[index] = '\0';
       }
   }

   if(started && ended)
   {
       // Convert the string to an integer
       int inInt = atoi(inData);

       // Use the value
       
}

       // Get ready for the next time
       started = false;
       ended = false;

       index = 0;
       inData[index] = '\0';
  

if(temp > inInt){
  digitalWrite(fan,HIGH);
   digitalWrite(heater, LOW);}
   
  else if(temp<inInt){
 digitalWrite(fan, LOW);
digitalWrite(heater,HIGH);}
  else if(temp==inInt){
    
  digitalWrite(fan, LOW);
  digitalWrite(heater, LOW);
}
}

]

The code is still not working as desired. It's left me quite frustrated I'm wondering if its actually possible to communicated to the Arduino Uno using Visual Basic. I may resort to this less ideal code I wrote, but was working perfectly:

[const int lm35= A0;
const int fan = 8;
const int heater = 7;
int temp;

 
void setup(){
 pinMode(fan, OUTPUT);
 pinMode(heater, OUTPUT);
pinMode(lm35, INPUT);
}
void loop(){
 int temp1= analogRead(lm35);
 temp=(5.0*temp1*100)/1024;
 
if(temp>25){
  digitalWrite(fan, HIGH);
   digitalWrite(heater, LOW);}
  else if(temp<25){
 digitalWrite(fan, LOW);
   digitalWrite(heater, HIGH);}
  else if(temp==25){
  digitalWrite(fan, LOW);
  digitalWrite(heater, LOW);
  
  
}}]
This particular one won't enable one to update the desired temperature via VB.

I'm wondering if its actually possible to communicated to the Arduino Uno using Visual Basic.

Yes it is.

Have you read this:-
http://arduino.cc/en/Tutorial/ReadASCIIString