Reading multiple sensor using visual basic 2008

I am trying to read data using 2 sensors from Arduino to Visual Basic 2010. So far i am able to read for 1 sensor using timer in visual basic 2010. If i add reading sensor 2 at timer 2 reading only show at timer 1 and nothing happen at timer 2. What is the problem....? Below are may codes vb and arduino.

Arduino program will send "a" character and arduino will give me the sensor value...if "b" character send to arduino and arduino will give me second sensor value.

Rx = "a"
Tx = (sensor 1 value)

Rx = "b"
Tx = (sensor 2 value)

This is may code vb2010 :

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick // timer 1
Try
SerialPort1.Write("a") // vb 2010 will send a character to arduino
System.Threading.Thread.Sleep(250)
Dim i As Integer = SerialPort1.ReadExisting() //vb 2010 read value from sensor1
Chart1.Series("Series1").Points.AddY(i)
Label1.Text = "" + i.ToString()
Me.Refresh()
Catch ex As Exception

End Try

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick // timer 2
Try
SerialPort1.Write("b") // vb 2010 send b character to arduino
System.Threading.Thread.Sleep(250)
Dim i2 As Integer = SerialPort1.ReadExisting() // read value from arduino
Chart1.Series("Series2").Points.AddY(i2)
Label1.Text = "" + i2.ToString()
Catch ex As Exception

End Try

Arduino code :

int temp_value;
int rainana_value;
#define SERIESRESISTOR 1000
#define tempsensor 0
#define raingauge 1

void setup()
{
Serial.begin(9600);
}

void loop()
{
char on_sensor = 'n';

if(Serial.available()>0)
{
on_sensor = Serial.read();

}
if (on_sensor=='a')
{
temp_value = analogRead(tempsensor);
temp_value = (5* temp_value * 100)/1024;
Serial.println (temp_value);

}

else if (on_sensor=='b')
{
rainana_value = analogRead(raingauge);
Serial.println (rainana_value);
}

}

Thank you all

How come the timer1 tick function includes a Refresh, while the timer2 tick function does not?
Why is the labels showing the number of characters read, instead of something useful, like the characters read? Why are there not two labels?

What are the timer intervals? Try making timer1's interval a lot longer than timer2's interval.

How come the timer1 tick function includes a Refresh, while the timer2 tick function does not?
Why is the labels showing the number of characters read, instead of something useful, like the characters read? Why are there not two labels?

What are the timer intervals? Try making timer1's interval a lot longer than timer2's interval.

Reply :
Timer setting :

Try
SerialPort1.Open()
Timer1.Interval = 1000
Timer1.Enabled = True
Timer2.Interval = 5000
Timer2.Enabled = False
Catch ex As Exception
End Try

i put timer setting in connect button. if i press connect button system will connect to com port according to the com setting and start timer 1 and timer 2. actually still trying with timer 2 so still not include Refresh at timer 2. what did you mean labels showing number of characters read...?

can you give some example, tutorial or book using two timers in vb 2010...

thank you for fast reply... :slight_smile:

i put timer setting in connect button. if i press connect button system will connect to com port according to the com setting and start timer 1 and timer 2. actually still trying with timer 2 so still not include Refresh at timer 2.

Without the refresh, how do you know whether the Timer2_Tick function got called, or not?

I already add Me.Refresh() at timer 2 and nothing show at label3. By the way label3 use for show second sensor. In my gui label1 show sensor 1 value and label3 show sensor 2 value.

Hello,
I am trying to do the same , and using your example/problem I think get something , please give it a try and let me know:

arduino code:

int temp_value;
int wind_value;
#define SERIESRESISTOR 1000
#define tempsensor 0 // DEFINE PIN ENTRADA DATO
#define windsensor 1 // DEFINE PIN ENTRADA DATO

void setup()
{
Serial.begin(9600);
}

void loop()
{
char on_sensor = 'n';

if(Serial.available()>0)
{
on_sensor = Serial.read();

}
if (on_sensor=='a')
{
temp_value = analogRead(tempsensor);
//temp_value = (5* temp_value * 100)/1024;
Serial.println (temp_value);

}

else if (on_sensor=='b')
{
wind_value = analogRead(windsensor);
Serial.println (wind_value);
}

}

I use 2 label , one for temp and second for wind , no buttons , and only one timer set to 1000 milisecons
Visual Basic 2010 code:

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

Public Class Form1
Dim i
Dim i2

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = "com3" '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

Timer1.Start()

End Sub

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

SerialPort1.Open()
SerialPort1.Write("a") ' vb 2010 will send a character to arduino
System.Threading.Thread.Sleep(250)
Dim i As Integer = SerialPort1.ReadExisting() 'vb 2010 read value from sensor1
Label1.Text = i
SerialPort1.Close()

SerialPort1.Open()
SerialPort1.Write("b") ' vb 2010 will send a character to arduino
System.Threading.Thread.Sleep(250)
Dim i2 As Integer = SerialPort1.ReadExisting() 'vb 2010 read value from sensor1
Label2.Text = i2
SerialPort1.Close()

End Sub
End Class

, please let me know if it works for you or if you found other solution.
Regards