I want to show 4 datas from different IR sensor from my arduino in Visual studio GUI, but it cant work properly, anyone can help me what should I do with my program? thanks.
(Pleas use Code Tags in future posts please!)
Here is my program for arduino:
const int sensorread = A0;
const int sensorread1 = A1;
const int sensorread2 = A2;
const int sensorread3 = A3;
void setup()
{
Serial.begin(9600);
pinMode(sensorread, INPUT);
pinMode(sensorread1, INPUT);
pinMode(sensorread2, INPUT);
pinMode(sensorread3, INPUT);
}
void loop()
{
int counter = 0;
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
byte state;
byte state1;
byte state2;
byte state3;
byte laststate;
byte laststate1;
byte laststate2;
byte laststate3;
laststate = digitalRead(sensorread); // Pre-load last state
laststate1 = digitalRead(sensorread1);
laststate2 = digitalRead(sensorread2);
laststate3 = digitalRead(sensorread3);
Serial.println("Begin...");
unsigned long startTime = millis(); // Get current chip time
while((startTime + 30000) > millis())
{
state = digitalRead(sensorread);
state1 = digitalRead(sensorread1);
state2 = digitalRead(sensorread2);
state3 = digitalRead(sensorread3);
if (state != laststate)
{
counter++; // Increments twice per drop so divide by 2 to get true count
laststate = state;
delay (30); // Debounce
}
if (state1 != laststate1)
{
counter1++; // Increments twice per drop so divide by 2 to get true count
laststate1 = state1;
delay (30); // Debounce
}
if (state2 != laststate2)
{
counter2++; // Increments twice per drop so divide by 2 to get true count
laststate2 = state2;
delay (30); // Debounce
}
if (state3 != laststate3)
{
counter3++; // Increments twice per drop so divide by 2 to get true count
laststate3 = state3;
delay (30); // Debounce
}
}
char on_sensor = 'n';
if (Serial.available()>0)
{
on_sensor = Serial.read();
}
if (on_sensor == 'a')
{
Serial.println (counter/2);
}
if (on_sensor == 'b')
{
Serial.println (counter1/2);
}
if (on_sensor == 'c')
{
Serial.println (counter2/2);
}
if (on_sensor == 'd')
{
Serial.println (counter3/2);
}
}
and here is my program in Visual studio 2010:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Dim i
Dim i2
Dim i3
Dim i4
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = "com5"
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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
SerialPort1.Open()
SerialPort1.Write("a")
System.Threading.Thread.Sleep(250)
Dim i As Integer = SerialPort1.ReadLine()
Label1.Text = i
SerialPort1.Close()
SerialPort1.Open()
SerialPort1.Write("b")
System.Threading.Thread.Sleep(250)
Dim i2 As Integer = SerialPort1.ReadLine()
Label2.Text = i2
SerialPort1.Close()
SerialPort1.Open()
SerialPort1.Write("c")
System.Threading.Thread.Sleep(250)
Dim i3 As Integer = SerialPort1.ReadLine()
Label3.Text = i3
SerialPort1.Close()
SerialPort1.Open()
SerialPort1.Write("d")
System.Threading.Thread.Sleep(250)
Dim i4 As Integer = SerialPort1.ReadLine()
Label4.Text = i4
SerialPort1.Close()
End Sub
End Class