Somewhat new to all this, but I don't understand how to write the code in arduino to tell my VB.Net pgm which analog I want to read at any given time. I can write to VB using Serial.println and I do see it in VB.Net but each time I start the pgm or reset the arduino the results come up different. by that I mean the order of the analoges to VB. IE value1 is not allways the first. See adruino code below, followed by VB.Net code. Any help?. Thanks, Ron.
/*
Adruino Analogs for FS 2004
By Ron Buchwald 1/12/2012
REV 1
A1 = Eng1, A2 = Eng2, A3 = Flaps, A4 = Spoilers, A5 = Elevator Trim
*/
int sensorPin1 = A1; //SET ANALOG PINS AS INPUT
int sensorPin2 = A2;
int sensorPin1 = A3;
int sensorPin2 = A4;
int sensorPin1 = A5;
int sensorValue1 = 0; //STORE READ VALUE
int sensorValue2 = 0;
int sensorValue3 = 0;
int sensorValue4 = 0;
int sensorValue5 = 0;
void setup() {
Serial.begin(9600);
}
void loop(){
Serial.flush;
sensorValue1 = analogRead(sensorPin1); //READ ANALOG PINS
sensorValue2 = analogRead(sensorPin2);
sensorValue3 = analogRead(sensorPin3);
sensorValue4 = analogRead(sensorPin4);
sensorValue5 = analogRead(sensorPin5);
//PRINT TO VB.NET 2010
Serial.println(sensorValue1*17); // *17 TO GET 0-17391 FOR FS ANALOGS
Serial.println(sensorValue2*17);// WOULD LIKE TO GET -4096-16383 BUT
Serial.println(sensorValue3*17);//DON'T KNOW HOW SO I DO THAT IN
Serial.println(sensorValue4*17);//VB.NET PGM
Serial.println(sensorValue5*17);
Serial.flush;
}
// END RON BUCHWALD 1/12/2012
Now the VB code
Option Explicit On
Option Strict Off
Option Infer Off
Imports System.Windows.Forms
Imports System.IO.Ports
Imports FSUIPC ' an addon DLL.
Imports System.Drawing.Drawing2D
Public Class Form1 'THESE ARE THE FS OFFSETS TO CONTROL
Dim ENG1 As Offset(Of Short) = New Offset(Of Short)(&H88C)
Dim ENG2 As Offset(Of Short) = New Offset(Of Short)(&H924)
Dim FLAPS As Offset(Of Short) = New Offset(Of Short)(&HBDC)
Dim SPOILERS As Offset(Of Short) = New Offset(Of Short)(&HBD0)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Open()
OpenFSUIPC() 'THE FSUIPC CALL TO THE DLL
SerialPort1.DiscardInBuffer()
End Sub
Delegate Sub ProcessDataDelegate(ByVal I As Short, ByVal J As Short, ByVal K As Short, ByVal L As Short)
'READ THE SERIAL PORT
Private Sub SerialPort1_DataReceived(ByVal sender As Object, _
ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Try
' READ THE SERIAL PORT ID.
Dim I As Short = Val(SerialPort1.ReadLine())
Dim J As Short = Val(SerialPort1.ReadLine())
Dim K As Short = Val(SerialPort1.ReadLine())
Dim L As Short = Val(SerialPort1.ReadLine())
Me.Invoke(New ProcessDataDelegate(AddressOf ProcessData), I, J, K, L)
Catch ex As Exception
'MessageBox.Show(ex.ToString)
' SerialPort1.Close()
SerialPort1.DiscardInBuffer()
End Try
End Sub
Sub ProcessData(ByVal I As Short, ByVal J As Short, ByVal K As Short, ByVal L As Short)
TextBox1.Text = ENG1.Value
TextBox2.Text = ENG2.Value
TextBox3.Text = FLAPS.Value
TextBox4.Text = SPOILERS.Value
' THE READ VALUE TO USE
ENG1.Value = I
ENG2.Value = J
FLAPS.Value = K
SPOILERS.Value = L
If ENG1.Value <= 0 Then
ENG1.Value = ENG1.Value - 4096 ' TO GET ENGIN REVERSE
End If
If ENG2.Value <= 0 Then
ENG2.Value = ENG2.Value - 4096 ' TO GET ENGIN REVERSE
End If
End Sub
Private Sub OpenFSUIPC()
Try
' Attempt to open a connection to FSUIPC (running on any version of Flight Sim)
FSUIPCConnection.Open()
Me.Timer1.Interval = 200
Me.Timer1.Enabled = True
Catch ex As Exception
End Try
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
' Process the default group FSUIPC internal.
FSUIPCConnection.Process()
Catch exFSUIPC As FSUIPCException
If exFSUIPC.FSUIPCErrorCode = FSUIPCError.FSUIPC_ERR_SENDMSG Then
FSUIPCConnection.Close()
'MessageBox.Show("The connection to Flight Sim has been lost.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
Throw exFSUIPC
End If
Catch ex As Exception
End Try
End Sub
Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
SerialPort1.DiscardInBuffer()
SerialPort1.Close()
FSUIPCConnection.Close()
End Sub
End Class ' RON BUCHWALD 1/12/2012