EDN Admin
Well-known member
Hi All. Scope: To send serial data from Arduino Mega 2560 to a VB.Net PGM, then the VB.NET pgm will read data then send to via FSUIPC (an Addin DLL) to ms flight simm 2004.
OK that said here is the question: how can I write or phrase or construct the proper syntex needed to 1-limit the data sent,or in reading the data-limit the data read?
I have enclosed the VB.NET code and the Arduino code. Please take a look. I am very new to both,but I try hard. all the code works but I get strange all most random data in VB. The FSUIPC end works OK, I can send to and receive from Ok.Its the VB pgm that
gets the data wrong some of the time. say 8 out of 10 it is OK then button x will also turn on or off another check box then do what was aked. It seems that I am not clearing the receive buffer so I get a last chr sent before.
I have tryed readline, readexisting,readbyte but I dont know how to phrase or construct any but the readline. I have tryed to flush the buffer in both codes but, well I am stuck. Any help as I have been at this several weeks. Ron.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports System.Windows.Forms
<span style="color:Blue; Imports System.IO.Ports
<span style="color:Blue; Imports FSUIPC <span style="color:Green; an addon DLL.
<span style="color:Blue; Imports System.Drawing.Drawing2D
<span style="color:Green; ********************************************* THIS IS THE COMM ROUTINE *********************************************************************
<span style="color:Blue; Public <span style="color:Blue; Class Form1
<span style="color:Blue; Dim I <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim avionics <span style="color:Blue; As Offset(Of <span style="color:Blue; Integer) = <span style="color:Blue; New FSUIPC.Offset(Of <span style="color:Blue; Integer)(&H2E80) <span style="color:Green; Basic integer read and write FSUIPC example
<span style="color:Green; Application started so try to open the connection to FSUIPC
<span style="color:Blue; Private <span style="color:Blue; Sub Form1_Load(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.Load
OpenFSUIPC()
<span style="color:Blue; Try
SerialPort1.Open()
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
MessageBox.Show(ex.ToString)
<span style="color:Blue; End <span style="color:Blue; Try
Console.WriteLine(<span style="color:#A31515; "Port open " + SerialPort1.IsOpen.ToString)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Delegate <span style="color:Blue; Sub AddText(<span style="color:Blue; ByVal Text <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; Private <span style="color:Blue; Sub SerialPort1_DataReceived(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As SerialDataReceivedEventArgs) <span style="color:Blue; Handles SerialPort1.DataReceived
Console.WriteLine(<span style="color:#A31515; "Bytes to read " + SerialPort1.BytesToRead.ToString)
TextBox1.Invoke(<span style="color:Blue; New AddText(<span style="color:Blue; AddressOf TextBox1.AppendText), SerialPort1.ReadLine + Environment.NewLine) <span style="color:Green; put text in textbox.
I = SerialPort1.ReadLine + Environment.NewLine <span style="color:Green; read data for checkbox check.
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Green; *********************************************** END OF COMM ROUTINE START FSUIPC ***********************************************************
<span style="color:Green; Opens FSUIPC - if all goes well then starts the timer to drive start the main application cycle If cant open display the error message.
<span style="color:Blue; Private <span style="color:Blue; Sub OpenFSUIPC()
<span style="color:Blue; Try
<span style="color:Green; Attempt to open a connection to FSUIPC (running on any version of Flight Sim)
FSUIPCConnection.Open()
<span style="color:Blue; Me.Timer1.Interval = 200
<span style="color:Blue; Me.Timer1.Enabled = <span style="color:Blue; True
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Green; Application is unloading so call close to cleanup the
<span style="color:Blue; Private <span style="color:Blue; Sub Form1_FormClosed(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.Windows.Forms.FormClosedEventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.FormClosed
FSUIPCConnection.Close()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Timer1_Tick(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles Timer1.Tick
<span style="color:Blue; Try
<span style="color:Green; Process the default group FSUIPC internal.
FSUIPCConnection.Process()
<span style="color:Blue; Me.SW5CB.Checked = (avionics.Value > 0) <span style="color:Green; this will read the FSUIPC data and check the checkbox (works fine)
<span style="color:Blue; Catch exFSUIPC <span style="color:Blue; As FSUIPCException
<span style="color:Blue; If exFSUIPC.FSUIPCErrorCode = FSUIPCError.FSUIPC_ERR_SENDMSG <span style="color:Blue; Then
FSUIPCConnection.Close()
<span style="color:Green; MessageBox.Show("The connection to Flight Sim has been lost.", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
<span style="color:Blue; Else
<span style="color:Blue; Throw exFSUIPC
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Green; ***************************************************** END FSUIPC ROUTINE *****************************************************************
<span style="color:Green; **************** this set or clears the (SWXCB) (CHECKBOX) **********************************************************
<span style="color:Blue; Private <span style="color:Blue; Sub TextBox1_TextChanged(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles TextBox1.TextChanged
<span style="color:Blue; If I = 2 <span style="color:Blue; Then <span style="color:Green; IF DATA REC.=2
<span style="color:Blue; If SW2CB.Checked <span style="color:Blue; Then <span style="color:Green; IF CB IS CHECKED
SW2CB.Checked = <span style="color:Blue; False <span style="color:Green; UN-CHECK IT
I = 0 <span style="color:Green; MY TRY TO CLEAR THE DATA (NOT WORKING)
<span style="color:Blue; Else
SW2CB.Checked = <span style="color:Blue; True <span style="color:Green; IF NOT CHECKED THEN CHECK IT
<span style="color:Blue; End <span style="color:Blue; If <span style="color:Green; NOTE Must be a better way....
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; If I = 3 <span style="color:Blue; Then
<span style="color:Blue; If SW3CB.Checked <span style="color:Blue; Then
SW3CB.Checked = <span style="color:Blue; False
I = 0
<span style="color:Blue; Else
SW3CB.Checked = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; If I = 4 <span style="color:Blue; Then
<span style="color:Blue; If SW4CB.Checked <span style="color:Blue; Then
SW4CB.Checked = <span style="color:Blue; False
I = 0
<span style="color:Blue; Else
SW4CB.Checked = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; If I = 5 <span style="color:Blue; Then
<span style="color:Blue; If SW5CB.Checked <span style="color:Blue; Then
SW5CB.Checked = <span style="color:Blue; False
<span style="color:Blue; Else
SW5CB.Checked = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub SW5CB_CheckedChanged(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles SW5CB.CheckedChanged
avionics.Value = IIf(SW5CB.Checked, 1, 0) <span style="color:Green; if checked send to FSUIPC the offset and the val (this works fine)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub SW2CB_CheckedChanged(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles SW2CB.CheckedChanged
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
Now the Arduino code
<div style="color:Black;background-color:White; <pre>
*
created 2011
by ron buchwald
<span style="color:Blue; this is to <span style="color:Blue; do 2 things.1- send the data via serialport
of button pushed and 2,
<span style="color:Blue; do a stateread IE.. count the times button was pushed to be able
to toggel the pinout button. not impmented as of now.
<span style="color:Blue; this is just a test bed to send serial to VB pgm.
*/
<span style="color:Green; // constants wont change. Theyre used here to
<span style="color:Green; // set pin numbers:
<span style="color:Blue; const <span style="color:Blue; int buttonPin2 = 2;
<span style="color:Blue; const <span style="color:Blue; int buttonPin3 = 3;
<span style="color:Blue; const <span style="color:Blue; int buttonPin4 = 4;
<span style="color:Blue; const <span style="color:Blue; int buttonPin5 = 5;
<span style="color:Green; // the number of the pushbutton pin
<span style="color:Blue; const <span style="color:Blue; int ledPin9 = 9; <span style="color:Green; // the number of the LED pin
<span style="color:Blue; const <span style="color:Blue; int ledPin10 = 10;
<span style="color:Blue; const <span style="color:Blue; int ledPin11 = 11;
<span style="color:Blue; const <span style="color:Blue; int ledPin12 = 12;
<span style="color:Green; // variables will change:
<span style="color:Blue; int buttonState2 = 0;
<span style="color:Blue; int buttonState3 = 0;
<span style="color:Blue; int buttonState4= 0;
<span style="color:Blue; int buttonState5 = 0;
<span style="color:Green; // variable for reading the pushbutton status
<span style="color:Blue; int buttonPushCounter = 0; <span style="color:Green; // counter for the number of button presses
<span style="color:Green; //int buttonState = 0; // current state of the button
<span style="color:Blue; int lastButtonState = 0; <span style="color:Green; // previous state of the button
<span style="color:Blue; void setup() {
Serial.begin(9600); <span style="color:Green; // keep it slow for testing
<span style="color:Green; // initialize the LED pin as an output:
pinMode(ledPin9, OUTPUT);
pinMode(ledPin10, OUTPUT);
pinMode(ledPin11, OUTPUT);
pinMode(ledPin12, OUTPUT);
<span style="color:Green; // initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
}
<span style="color:Blue; void loop(){
<span style="color:Green; // read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
{
<span style="color:Blue; if (buttonState2 == HIGH) {
digitalWrite(ledPin9, HIGH);
Serial.println(2);
Serial.flush(); <span style="color:Green; // my try to clear buffer after send(not working as I can tell)
delay(100); <span style="color:Green; // my try to keep from sending (2) too many times,(not working)
} <span style="color:Blue; else { <span style="color:Green; // I still get too many send over and over
digitalWrite(ledPin9, LOW);
}
}
<span style="color:Blue; if (buttonState3 == HIGH) {
<span style="color:Green; // turn LED on:
digitalWrite(ledPin10, HIGH);
Serial.println(3);
delay(100);
}
<span style="color:Green; //else {
<span style="color:Green; // turn LED off:
<span style="color:Green; // digitalWrite(ledPin10, LOW);
<span style="color:Green; //}
<span style="color:Blue; if (buttonState4 == HIGH) {
<span style="color:Green; // turn LED on:
digitalWrite(ledPin11, HIGH);
Serial.println(4);
delay(100);
}
<span style="color:Green; //else {
<span style="color:Green; // turn LED off:
<span style="color:Green; // digitalWrite(ledPin11, LOW);
<span style="color:Green; //}
<span style="color:Blue; if (buttonState5 == HIGH) {
<span style="color:Green; // turn LED on:
digitalWrite(ledPin12, HIGH);
Serial.println(5);
delay(100);
}
<span style="color:Green; //else {
<span style="color:Green; // turn LED off:
<span style="color:Green; // digitalWrite(ledPin12, LOW);
}
[/code]
END Question.<br/>
<br/>
View the full article
OK that said here is the question: how can I write or phrase or construct the proper syntex needed to 1-limit the data sent,or in reading the data-limit the data read?
I have enclosed the VB.NET code and the Arduino code. Please take a look. I am very new to both,but I try hard. all the code works but I get strange all most random data in VB. The FSUIPC end works OK, I can send to and receive from Ok.Its the VB pgm that
gets the data wrong some of the time. say 8 out of 10 it is OK then button x will also turn on or off another check box then do what was aked. It seems that I am not clearing the receive buffer so I get a last chr sent before.
I have tryed readline, readexisting,readbyte but I dont know how to phrase or construct any but the readline. I have tryed to flush the buffer in both codes but, well I am stuck. Any help as I have been at this several weeks. Ron.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports System.Windows.Forms
<span style="color:Blue; Imports System.IO.Ports
<span style="color:Blue; Imports FSUIPC <span style="color:Green; an addon DLL.
<span style="color:Blue; Imports System.Drawing.Drawing2D
<span style="color:Green; ********************************************* THIS IS THE COMM ROUTINE *********************************************************************
<span style="color:Blue; Public <span style="color:Blue; Class Form1
<span style="color:Blue; Dim I <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim avionics <span style="color:Blue; As Offset(Of <span style="color:Blue; Integer) = <span style="color:Blue; New FSUIPC.Offset(Of <span style="color:Blue; Integer)(&H2E80) <span style="color:Green; Basic integer read and write FSUIPC example
<span style="color:Green; Application started so try to open the connection to FSUIPC
<span style="color:Blue; Private <span style="color:Blue; Sub Form1_Load(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.Load
OpenFSUIPC()
<span style="color:Blue; Try
SerialPort1.Open()
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
MessageBox.Show(ex.ToString)
<span style="color:Blue; End <span style="color:Blue; Try
Console.WriteLine(<span style="color:#A31515; "Port open " + SerialPort1.IsOpen.ToString)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Delegate <span style="color:Blue; Sub AddText(<span style="color:Blue; ByVal Text <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; Private <span style="color:Blue; Sub SerialPort1_DataReceived(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As SerialDataReceivedEventArgs) <span style="color:Blue; Handles SerialPort1.DataReceived
Console.WriteLine(<span style="color:#A31515; "Bytes to read " + SerialPort1.BytesToRead.ToString)
TextBox1.Invoke(<span style="color:Blue; New AddText(<span style="color:Blue; AddressOf TextBox1.AppendText), SerialPort1.ReadLine + Environment.NewLine) <span style="color:Green; put text in textbox.
I = SerialPort1.ReadLine + Environment.NewLine <span style="color:Green; read data for checkbox check.
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Green; *********************************************** END OF COMM ROUTINE START FSUIPC ***********************************************************
<span style="color:Green; Opens FSUIPC - if all goes well then starts the timer to drive start the main application cycle If cant open display the error message.
<span style="color:Blue; Private <span style="color:Blue; Sub OpenFSUIPC()
<span style="color:Blue; Try
<span style="color:Green; Attempt to open a connection to FSUIPC (running on any version of Flight Sim)
FSUIPCConnection.Open()
<span style="color:Blue; Me.Timer1.Interval = 200
<span style="color:Blue; Me.Timer1.Enabled = <span style="color:Blue; True
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Green; Application is unloading so call close to cleanup the
<span style="color:Blue; Private <span style="color:Blue; Sub Form1_FormClosed(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.Windows.Forms.FormClosedEventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.FormClosed
FSUIPCConnection.Close()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub Timer1_Tick(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles Timer1.Tick
<span style="color:Blue; Try
<span style="color:Green; Process the default group FSUIPC internal.
FSUIPCConnection.Process()
<span style="color:Blue; Me.SW5CB.Checked = (avionics.Value > 0) <span style="color:Green; this will read the FSUIPC data and check the checkbox (works fine)
<span style="color:Blue; Catch exFSUIPC <span style="color:Blue; As FSUIPCException
<span style="color:Blue; If exFSUIPC.FSUIPCErrorCode = FSUIPCError.FSUIPC_ERR_SENDMSG <span style="color:Blue; Then
FSUIPCConnection.Close()
<span style="color:Green; MessageBox.Show("The connection to Flight Sim has been lost.", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
<span style="color:Blue; Else
<span style="color:Blue; Throw exFSUIPC
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Green; ***************************************************** END FSUIPC ROUTINE *****************************************************************
<span style="color:Green; **************** this set or clears the (SWXCB) (CHECKBOX) **********************************************************
<span style="color:Blue; Private <span style="color:Blue; Sub TextBox1_TextChanged(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles TextBox1.TextChanged
<span style="color:Blue; If I = 2 <span style="color:Blue; Then <span style="color:Green; IF DATA REC.=2
<span style="color:Blue; If SW2CB.Checked <span style="color:Blue; Then <span style="color:Green; IF CB IS CHECKED
SW2CB.Checked = <span style="color:Blue; False <span style="color:Green; UN-CHECK IT
I = 0 <span style="color:Green; MY TRY TO CLEAR THE DATA (NOT WORKING)
<span style="color:Blue; Else
SW2CB.Checked = <span style="color:Blue; True <span style="color:Green; IF NOT CHECKED THEN CHECK IT
<span style="color:Blue; End <span style="color:Blue; If <span style="color:Green; NOTE Must be a better way....
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; If I = 3 <span style="color:Blue; Then
<span style="color:Blue; If SW3CB.Checked <span style="color:Blue; Then
SW3CB.Checked = <span style="color:Blue; False
I = 0
<span style="color:Blue; Else
SW3CB.Checked = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; If I = 4 <span style="color:Blue; Then
<span style="color:Blue; If SW4CB.Checked <span style="color:Blue; Then
SW4CB.Checked = <span style="color:Blue; False
I = 0
<span style="color:Blue; Else
SW4CB.Checked = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; If I = 5 <span style="color:Blue; Then
<span style="color:Blue; If SW5CB.Checked <span style="color:Blue; Then
SW5CB.Checked = <span style="color:Blue; False
<span style="color:Blue; Else
SW5CB.Checked = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub SW5CB_CheckedChanged(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles SW5CB.CheckedChanged
avionics.Value = IIf(SW5CB.Checked, 1, 0) <span style="color:Green; if checked send to FSUIPC the offset and the val (this works fine)
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub SW2CB_CheckedChanged(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles SW2CB.CheckedChanged
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
Now the Arduino code
<div style="color:Black;background-color:White; <pre>
*
created 2011
by ron buchwald
<span style="color:Blue; this is to <span style="color:Blue; do 2 things.1- send the data via serialport
of button pushed and 2,
<span style="color:Blue; do a stateread IE.. count the times button was pushed to be able
to toggel the pinout button. not impmented as of now.
<span style="color:Blue; this is just a test bed to send serial to VB pgm.
*/
<span style="color:Green; // constants wont change. Theyre used here to
<span style="color:Green; // set pin numbers:
<span style="color:Blue; const <span style="color:Blue; int buttonPin2 = 2;
<span style="color:Blue; const <span style="color:Blue; int buttonPin3 = 3;
<span style="color:Blue; const <span style="color:Blue; int buttonPin4 = 4;
<span style="color:Blue; const <span style="color:Blue; int buttonPin5 = 5;
<span style="color:Green; // the number of the pushbutton pin
<span style="color:Blue; const <span style="color:Blue; int ledPin9 = 9; <span style="color:Green; // the number of the LED pin
<span style="color:Blue; const <span style="color:Blue; int ledPin10 = 10;
<span style="color:Blue; const <span style="color:Blue; int ledPin11 = 11;
<span style="color:Blue; const <span style="color:Blue; int ledPin12 = 12;
<span style="color:Green; // variables will change:
<span style="color:Blue; int buttonState2 = 0;
<span style="color:Blue; int buttonState3 = 0;
<span style="color:Blue; int buttonState4= 0;
<span style="color:Blue; int buttonState5 = 0;
<span style="color:Green; // variable for reading the pushbutton status
<span style="color:Blue; int buttonPushCounter = 0; <span style="color:Green; // counter for the number of button presses
<span style="color:Green; //int buttonState = 0; // current state of the button
<span style="color:Blue; int lastButtonState = 0; <span style="color:Green; // previous state of the button
<span style="color:Blue; void setup() {
Serial.begin(9600); <span style="color:Green; // keep it slow for testing
<span style="color:Green; // initialize the LED pin as an output:
pinMode(ledPin9, OUTPUT);
pinMode(ledPin10, OUTPUT);
pinMode(ledPin11, OUTPUT);
pinMode(ledPin12, OUTPUT);
<span style="color:Green; // initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
}
<span style="color:Blue; void loop(){
<span style="color:Green; // read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
{
<span style="color:Blue; if (buttonState2 == HIGH) {
digitalWrite(ledPin9, HIGH);
Serial.println(2);
Serial.flush(); <span style="color:Green; // my try to clear buffer after send(not working as I can tell)
delay(100); <span style="color:Green; // my try to keep from sending (2) too many times,(not working)
} <span style="color:Blue; else { <span style="color:Green; // I still get too many send over and over
digitalWrite(ledPin9, LOW);
}
}
<span style="color:Blue; if (buttonState3 == HIGH) {
<span style="color:Green; // turn LED on:
digitalWrite(ledPin10, HIGH);
Serial.println(3);
delay(100);
}
<span style="color:Green; //else {
<span style="color:Green; // turn LED off:
<span style="color:Green; // digitalWrite(ledPin10, LOW);
<span style="color:Green; //}
<span style="color:Blue; if (buttonState4 == HIGH) {
<span style="color:Green; // turn LED on:
digitalWrite(ledPin11, HIGH);
Serial.println(4);
delay(100);
}
<span style="color:Green; //else {
<span style="color:Green; // turn LED off:
<span style="color:Green; // digitalWrite(ledPin11, LOW);
<span style="color:Green; //}
<span style="color:Blue; if (buttonState5 == HIGH) {
<span style="color:Green; // turn LED on:
digitalWrite(ledPin12, HIGH);
Serial.println(5);
delay(100);
}
<span style="color:Green; //else {
<span style="color:Green; // turn LED off:
<span style="color:Green; // digitalWrite(ledPin12, LOW);
}
[/code]
END Question.<br/>
<br/>
View the full article