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