Hi, I am trying to patch together a script to run three LCDs with Altitude,Airspeed & Heading, plus 16 switches & 9 LEDs.
I have some parts working but when I try to put them together, things go rather strange, keep playing with it but just getting more problems.
Any help appreciated, this is the script so far.



/*

For use with "Link2fs_Multi" (FSX)
Based on parts of Jims multi starter 2, Lights demo, Alt_Airspeed demo and Thomas Flummer - 4 lcd on 1 arduino.


This sets the Arduino Mega card to indicate gear position and flaps, using simconnect codes with
the remainder for inputs except pin 13.
LED for nose gear down onto pin 2 (Thru a resistor)
LED for left gear down onto pin 3 (Thru a resistor)
LED for right gear down onto pin 4 (Thru a resistor)
LED for nose gear in transition onto pin 5 (Thru a resistor)
LED for left gear in transition onto pin 6 (Thru a resistor)
LED for right gear in transition onto pin 7 (Thru a resistor)
LED for Flaps not fully up onto pin 8 (Thru a resistor)
LED for Flasps down onto pin 9 (Thru a resistor)

For the gear control, attach 2 buttons (or a double throw toggle switch) to pins 23 and 24
A button on pin 26 will raise the flaps 1 notch
A button on pin 25 will lower the flaps 1 notch

You need to tick "?Y" in "Link2fs_Multi" down the bottom of "systems2". (Extractions2 tab).
Also you need to tick " Attach a button to any pin, 23 upwards and program that pin in "Keys" section or the direct inputs section.
Everything to do with "Keys" starts with a "K" in this code.


*/
// include the library code:
#include

// initialize the library with the numbers of the interface pins,thanks Tom.31
LiquidCrystal lcd1(15,16,19,20,21,22);
LiquidCrystal lcd2(15,17,19,20,21,22);
LiquidCrystal lcd3(15,18,19,20,21,22);


int j1;
int j2;//not sure what this dose in the script i copyed.
int CodeIn;// used on all serial reads
int KpinState;//dont know if this is needed.
int KpinNo;
int Koutpin;

String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate, headi, headiold, airsp, airspold, altit, altitold;
String gearSimple, flaps, flapsOld;


void setup()
{
// set up the LCD's number of rows and columns:
lcd1.begin(16, 2);
lcd2.begin(16, 2);
lcd3.begin(16, 2);
// Print a message to the LCD.
delay (11);
lcd1.setCursor(4, 0);
delay (11);
lcd1.print("ALTITUDE");
delay (11);
lcd2.setCursor(4, 0);
delay (11);
lcd2.print("AIRSPEED");
delay (11);
lcd3.setCursor(4, 0);
delay (11);
lcd3.print("HEADING");
delay (11);
//the line below is for the mega card.
Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";
// Lets set the output pin modes (longhand)
pinMode(2, OUTPUT); // gear nose LED
pinMode(3, OUTPUT); // gear left LED
pinMode(4, OUTPUT); // gear right LED
pinMode(5, OUTPUT); // gear nose in transition LED
pinMode(6, OUTPUT); // gear left in transition LED
pinMode(7, OUTPUT); // gear right in transition LED
pinMode(8, OUTPUT); // Flaps not fully Up LED
pinMode(9, OUTPUT); // Flaps down LED
// now we will do all the inputs in a routine ,,,
for (int KoutPin = 23; KoutPin <= 69; KoutPin++)// Get all the pins ready for simconnect codes and "Keys"(all inputs)
{
pinMode(KoutPin, INPUT);
digitalWrite(KoutPin, HIGH);
}
Serial.begin(115200);
}

void loop() {
{INPUTS();} //Check the Simconnect and "keys" section for any input pins
{OTHER();}// Check for "Other" things to do. (Non extraction stuff)
if (Serial.available()) {
CodeIn = getChar();
if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<"
if (CodeIn == '?') {QUESTION();}// The first identifier is "?"
}
} // end of void loop (The main one)

char getChar()// Get a character from the serial buffer
{
while(Serial.available() == 0);// wait for data
return((char)Serial.read());// Thanks Doug
}// end of getchar void.


void OTHER(){
// set the cursor to column 4 line 1:
// note: line 1 is the second row, since counting begins with 0:
// read the oldest byte in the serial buffer:
if (Serial.available() > 0) {
j1 = Serial.read();
if (j1 == ' delay (11);// It seems to need a delay here
altit = "";
altit += char(Serial.read());
altit += char(Serial.read());
altit += char(Serial.read());
altit += char(Serial.read());
altit += char(Serial.read());
altit += (" "); //pads it up to 8 caracters
if (altit != altitold){ // checks to see if its different to the "old" reading
delay (11);
lcd1.setCursor(4, 1);//sets the cursor position for the first letter
delay (11);
lcd1.print(altit);//Prints the data to the LCD
altitold = altit;// Writes the current reading to the "old" string.
}
}

if (j1 == ' delay (11); // It seems to need a delay here
airsp = "";
airsp += char(Serial.read());
airsp += char(Serial.read());
airsp += char(Serial.read());
airsp += char(Serial.read());
airsp += char(Serial.read());
airsp += (" "); //pads it up to 8 caracters
if (airsp != airspold){ // checks to see if its different to the "old" reading
delay (11);
lcd2.setCursor(4, 1); //sets the cursor position for the first letter
delay (11);
lcd2.print(airsp); //Prints the data to the LCD
airspold = airsp; // Writes the current reading to the "old" string.
}
}

if (j1 == ' delay (11);// It seems to need a delay here
headi = "";
headi += char(Serial.read());
headi += char(Serial.read());
headi += char(Serial.read());
headi += char(Serial.read());
headi += char(Serial.read());
headi += (" "); //pads it up to 8 caracters
if (headi != headiold){ // checks to see if its different to the "old" reading
delay (11);
lcd3.setCursor(4, 1); //sets the cursor position for the first letter
delay (11);
lcd3.print(headi);//Prints the data to the LCD
headiold = headi;// Writes the current reading to the "old" string.
}
}//end of other void

}
}
void LESSTHAN(){ // The first identifier was "<"
CodeIn = getChar(); // Get the second identifier
switch(CodeIn) {// Now lets find what to do with it
case 'G'://Found the second identifier ("G" Flaps position)
flaps = "";
flaps += getChar();
flaps += getChar();
flaps += getChar();
int flapsINT = flaps.toInt(); // convert it to an integer (Thanks Phill)
if (flapsINT > 0){digitalWrite(8, HIGH);}else{digitalWrite(8, LOW);}//action the LED
if (flapsINT > 20){digitalWrite(9, HIGH);}else{digitalWrite(9, LOW);}//action the LED
break;
}// end of switch
}// end of lessthan void

void QUESTION(){ // The first identifier was "?"
CodeIn = getChar(); // Get the second identifier
switch(CodeIn) {// Now lets find what to do with it
case 'Y': // found the second identifier (the "Gear simple")
gearSimple = "";
gearSimple += getChar();// get first charactor (Nose gear)
if (gearSimple == "2"){digitalWrite(2, HIGH);}else{digitalWrite(2, LOW);}
if (gearSimple == "1"){digitalWrite(5, HIGH);}else{digitalWrite(5, LOW);}
gearSimple += getChar(); // get the second charactor (gear left)
if (gearSimple.endsWith("2")){digitalWrite(3, HIGH);}else{digitalWrite(3, LOW);}
if (gearSimple.endsWith("1")){digitalWrite(6, HIGH);}else{digitalWrite(6, LOW);}
gearSimple += getChar(); // get the third charactor (gear right)
if (gearSimple.endsWith("2")){digitalWrite(4, HIGH);}else{digitalWrite(4, LOW);}
if (gearSimple.endsWith("1")){digitalWrite(7, HIGH);}else{digitalWrite(7, LOW);}
break;
}
}// end of question void

void INPUTS(){ // Simconnect codes and "Keys" section
Kstringnewstate = "";
for (int KpinNo = 23; KpinNo <=69; KpinNo++){ //set this to the input pins. (pins 10 to 19)
KpinStateSTR = String(digitalRead(KpinNo)); //read the state of the pin
KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 23));// gets the 'old' state of the pin from string
if (KpinStateSTR != KoldpinStateSTR){// checks if it's different to the last reading of that pinNo
if (KpinNo != 13){ // avoid using pin 13 as an input unless you know the tricks.
if (KpinNo == 23 && KpinStateSTR == "0"){Serial.println ("C01");} //sets gear handle up
if (KpinNo == 24 && KpinStateSTR == "0"){Serial.println ("C02");} //sets gear handle down
if (KpinNo == 25 && KpinStateSTR == "0"){Serial.println ("C15");} //Decrements flap handle position
if (KpinNo == 26 && KpinStateSTR == "0"){Serial.println ("C14");} //Increments flap handle position
if (KpinNo == 27){if (KpinStateSTR == "1" ){Serial.println ("C410");}else {Serial.println ("C411");}}//Nav lights
if (KpinNo == 28{if (KpinStateSTR == "1" ){Serial.println ("C420");}else {Serial.println ("C421");}}//Beacon lights
if (KpinNo == 29){if (KpinStateSTR == "1" ){Serial.println ("C430");}else {Serial.println ("C431");}}//Landing lights
if (KpinNo == 30){if (KpinStateSTR == "1" ){Serial.println ("C440");}else {Serial.println ("C441");}}//Taxi lights
if (KpinNo == 31){if (KpinStateSTR == "1" ){Serial.println ("C450");}else {Serial.println ("C451");}}//Strobe lights
if (KpinNo == 32){if (KpinStateSTR == "1" ){Serial.println ("C460");}else {Serial.println ("C461");}}//Panel lights
if (KpinNo == 33){if (KpinStateSTR == "1" ){Serial.println ("C500");}else {Serial.println ("C501");}}//Cabin lights
if (KpinNo == 34){if (KpinStateSTR == "1" ){Serial.println ("E42");}else {Serial.println ("E43");}}//Start
if (KpinNo == 35){if (KpinStateSTR == "1" ){Serial.println ("E01");}else {Serial.println ("E04");}}//Mag1/right
if (KpinNo == 36){if (KpinStateSTR == "1" ){Serial.println ("E01");}else {Serial.println ("E04");}}//Mag2/left
if (KpinNo == 37){if (KpinStateSTR == "1" ){Serial.println ("A431");}else {Serial.println ("A430");}}//Master
if (KpinNo == 38{if (KpinStateSTR == "1" ){Serial.println ("E18");}else {Serial.println ("E17");}}//Battery
// Add more here but remember to also change the figure in the next line down.
// now the "Keys" bit ,,,
if (KpinNo > 38{ //Change pinNo number to same as the highest one used for simconnect codes.
Serial.print ("D");
if (KpinNo < 10) Serial.print ("0");
Serial.print (KpinNo);
Serial.println (KpinStateSTR);
}//end of 'its pinNo is greater than 14'
}//end of 'its not pin 13'
}//end of 'its different'
Kstringnewstate += KpinStateSTR;
}//end of 'for' loop (read the pins)
Kstringoldstate = Kstringnewstate;

delay (11);

}//end of INPUTS void

Hope I have sent this to the right place, very new to this.
Ivor Anthony Long.