Results 1 to 4 of 4
  1. #1
    25+ Posting Member
    Join Date
    Sep 2013
    Location
    Lahore
    Posts
    33
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    help with arduino code

    hi. im a complete newbie to programming. after following arduino tutorials and jim's tutorial, i understood a few things but not the complete code. i tried adding lines to the code, but i just cant get it to go. can anybody explain what im doing wrong? it would help me understand things a lot better.

    using jim's starter 2 code, everything works fine. i have 7 led's on my breadboard connected to the arduino pins 1 to 7. using fs2link, when i lower the landing gear in fsx, it the led for "gear in transition" starts blinking. then the nose led lights up, followed by main gear. same for flaps.

    so here's what i tried to do to understand the code. i added pin 8. and wanted to use it for autopilot on light. as soon as i turn on autopilot, the light should turn on. in reality, it blinks... very very dim. doesnt stay on. i understand that i can add delay perhaps?

    anyway.. here's the code.. highlight the changes i made in red.

    (sorry for posting the whole code)






    /*

    This example code is in the public domain.
    */

    int xx; // sets up a variable for the receive cycle.
    int pinState; // sets up variable for hoding the "pin state" on a read of the pin("Keys" part of program)
    int pinNo; // sets up variable for hoding the "pin number" on the read of the pin ("Keys" part of program)
    int doutpin; // sets up a variable for the setup loop that initialises the pins mode (Pin as input or output)


    String flaps, flapsold; // Sets up 2 string variables ,,, one for the new reading ,, one for the old.
    // The reason why there is a new and old reading is to stop the code repeating the commands.(It only act's on change)
    // The reason they are strings and not bytes (or other) is, it's easier for a novice to mentally visualize the
    // difference in adding (1+2+3=6) versus concatentations (1+2+3=123).
    String oldpinStateSTR, pinStateSTR, stringnewstate,stringoldstate; // sets up strings used in "keys" section
    String gearN, gearNold, gearL, gearLold, gearR, gearRold; //sets up the variables for the gear nose, gear left, gear right along with their "old" reading variable.
    String AP, APold;




    void setup() {
    // Setup the pins 2 to 7 as outputs for the LED's
    for (int doutPin = 2; doutPin <= 8; doutPin++) //pin 7 was used here. changed it to 8
    {
    pinMode(doutPin, OUTPUT);
    digitalWrite(doutPin, HIGH);
    }

    stringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111"; // Gives the "keys old" string a value
    // Setup the pins as inputs as used by "Keys" (For the "Keys" program avoid actually using Pin 13 as an input ,, long story)
    for (int doutPin = 9; doutPin <= 19; doutPin++) // if you have a Mega card and want to use all the pins ,, change the 19 to 69
    {
    pinMode(doutPin, INPUT);
    digitalWrite(doutPin, HIGH);
    }
    Serial.begin(115200); // sets up the baud rate for the serial coms.
    }


    void loop() { // this first bit is for the "Keys" read.
    // oldpinStateSTR = (""); // makes this string "" (Nothing)
    //pinStateSTR = (""); // makes this string "" (Nothing)
    stringnewstate = ""; // makes this string "" (Nothing)
    for (int pinNo = 9; pinNo <= 19; pinNo++){ // for reading the pins 2 to 19 (For a mega card change the 19 to 69)
    pinStateSTR = String(digitalRead(pinNo)); //gets the pins state
    oldpinStateSTR = String(stringoldstate.charAt(pinNo - 9));// changes that pins status within the "old" string

    if (pinStateSTR != oldpinStateSTR) //checks if there is a "change of state" then if there is ,,,,
    {
    Serial.print ("D"); //sends a "D" out the serial port ,,,
    if (pinNo < 10) Serial.print ("0"); //adds a zero if pin No. less than 10 (to keep the data length at 4)
    Serial.print (pinNo); // followed by the pin number ,,,,,
    Serial.print (pinStateSTR); // followed byt the pin state. (Now high or low)
    }
    stringnewstate += pinStateSTR; // Updates the
    }


    stringoldstate = stringnewstate; // updates the "old" string


    if (Serial.available() > 0) { //if there is a charator in the serial receive buffer then ,,,,
    xx = Serial.read(); // read it.
    if (xx == 'M'){ // Found the reading "Flaps"
    delay (11); // It seems to need a delay here
    flaps = "";
    flaps += char(Serial.read()); //Read the first charactor sent
    flaps += char(Serial.read()); //Read the second charactor sent and add it to the first
    flaps += char(Serial.read()); //Read the third charactor sent and add it to the other two.)
    if (flaps != flapsold){ // checks to see if its different to the "old" reading from the last loop
    if (flaps == "000") {digitalWrite(6, LOW);} //if its "000" then turn OFF the LED
    if (flaps > "000") { // if its greater than "000" then ,,,
    digitalWrite(6, HIGH); // set the LED on
    delay(50); // wait for 50 milliseconds
    digitalWrite(6, LOW); // set the LED off
    delay(50); // wait for 50 millisecon
    } // end of "moving" sequence. When there is no new change, the LED will go out.
    if (flaps > "030") {digitalWrite(7, HIGH);} else {digitalWrite(7, LOW);} //if flaps over "030" degree's then turn ON the LED

    flapsold = flaps; // Makes the old reading the same as the new reading.(So on the next reading it can check to see if it's different)

    } // end of the "yes it's different to the old reading" sequence
    } //end of "found the "flaps" (M) read
    if (xx == 'I'){ // Found the reading "Gear Nose"
    delay (11); // It seems to need a delay here
    gearN = "";
    gearN += char(Serial.read()); //Read the first charactor sent
    gearN += char(Serial.read()); //Read the second charactor sent and add it to the first
    gearN += char(Serial.read()); //Read the third charactor sent and add it to the other two.)
    if (gearN != gearNold){ // checks to see if its different to the "old" reading from the last loop
    if (gearN == "100") {digitalWrite(2, HIGH);} else {digitalWrite(2, LOW);} //if its "100" then turn ON the LED ,, anything else, turn it OFF
    gearNold = gearN; // Makes the old reading the same as the new reading.(So on the next reading it can check to see if it's different)
    } // end of the "yes it's different to the old reading" sequence
    } //end of "found the gear NOSE" (I) read

    if (xx == 'J'){ // Found the reading "Gear LEFT"
    delay (11); // It seems to need a delay here
    gearL = "";
    gearL += char(Serial.read()); //Read the first charactor sent
    gearL += char(Serial.read()); //Read the second charactor sent and add it to the first
    gearL += char(Serial.read()); //Read the third charactor sent and add it to the other two.)
    if (gearL != gearLold){ // checks to see if its different to the "old" reading from the last loop
    if (gearL == "100") {digitalWrite(3, HIGH);} else {digitalWrite(3, LOW);} //if its "100" then turn ON the LED ,, anything else, turn it OFF
    gearLold = gearL; // Makes the old reading the same as the new reading.(So on the next reading it can check to see if it's different)
    } // end of the "yes it's different to the old reading" sequence
    } //end of "found the gear LEFT" (J) read

    if (xx == 'K'){ // Found the reading "Gear RIGHT"
    delay (11); // It seems to need a delay here
    gearR = "";
    gearR += char(Serial.read()); //Read the first charactor sent
    gearR += char(Serial.read()); //Read the second charactor sent and add it to the first
    gearR += char(Serial.read()); //Read the third charactor sent and add it to the other two.)
    if (gearR != gearRold){ // checks to see if its different to the "old" reading from the last loop
    if (gearR == "100") {digitalWrite(4, HIGH);} else {digitalWrite(4, LOW);} //if its "100" then turn ON the LED ,, anything else, turn it OFF
    gearRold = gearR; // Makes the old reading the same as the new reading.(So on the next reading it can check to see if it's different)
    } // end of the "yes it's different to the old reading" sequence
    } //end of "found the gear RIGHT" (K) read






    if (gearN != "000" && gearN != "100" || gearL != "000" && gearL != "100" ||gearR != "000" && gearR != "100" )// checks if any gear is not totally up or down
    {digitalWrite(5, HIGH);} //if any gear is NOT totally up or totally down ,, turns LED ON ,, indicating moving.
    else
    {digitalWrite(5, LOW);} // Turns LED OFF if all the gear is totally UP or DOWN.


    if (xx == 'b'){ // Found the reading "Gear RIGHT"
    delay (11); // It seems to need a delay here
    AP = "";
    AP += char(Serial.read()); //Read the first charactor sent
    AP += char(Serial.read()); //Read the second charactor sent and add it to the first
    AP += char(Serial.read()); //Read the third charactor sent and add it to the other two.)
    if (AP != APold){ // checks to see if its different to the "old" reading from the last loop
    if (AP == "0") {digitalWrite(8, LOW);} //if its "000" then turn OFF the LED
    if (AP > "0") {digitalWrite(8, HIGH);
    APold = AP;

    { if (AP!= "0")
    {digitalWrite(8, LOW);}
    else
    {digitalWrite(8, HIGH);}
    }


    } // end of the "yes it's different to the old reading" sequence
    }



    } // end of "Yes" there is a serial read.


    }


    }

  2. #2
    150+ Forum Groupie
    Join Date
    Apr 2011
    Location
    United States of America
    Posts
    232
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: help with arduino code

    Hello Jahanzeb.
    It seems you have a pretty good idea of what's happening.

    With that said, you are reading too many characters for AP active. AP active only sends one character - either a 1 or 0.
    For a similar explanation on Link2FS serial reads, start at post11.

    Code:
    if (xx == 'b'){ // Found the reading "Gear RIGHT"
    delay (11); // It seems to need a delay here
    AP = "";
    AP += char(Serial.read()); //Read the first charactor sent
    Deleted extra serial reads
    if (AP != APold){ // checks to see if its different to the "old" reading from the last loop
    if (AP == "0") {digitalWrite(8, LOW);} //if its "000" then turn OFF the LED
    if (AP > "0") {digitalWrite(8, HIGH); 
    Pin8 would light up the LED if (AP > "0") 
    APold = AP; 
    
    != means NOT; Below reads - If AP does NOT equal "0" then turn pin8 off.
    Thus, pin8 was turned ON from the code above: if (AP > "0") {digitalWrite(8, HIGH); 
    But, immediately turned OFF (hence, the dim blink) due to the code below:
    { 
    if (AP!= "0")
    {digitalWrite(8, LOW);}
    else
    {digitalWrite(8, HIGH);}
    } 
    So, the corrected (untested) code:
    Code:
    /*
    
    This example code is in the public domain.
    */
    
    int xx; // sets up a variable for the receive cycle.
    int pinState; // sets up variable for hoding the "pin state" on a read of the pin("Keys" part of program)
    int pinNo; // sets up variable for hoding the "pin number" on the read of the pin ("Keys" part of program)
    int doutpin; // sets up a variable for the setup loop that initialises the pins mode (Pin as input or output)
    
    
    String flaps, flapsold; // Sets up 2 string variables ,,, one for the new reading ,, one for the old.
    // The reason why there is a new and old reading is to stop the code repeating the commands.(It only act's on change)
    // The reason they are strings and not bytes (or other) is, it's easier for a novice to mentally visualize the 
    // difference in adding (1+2+3=6) versus concatentations (1+2+3=123).
    String oldpinStateSTR, pinStateSTR, stringnewstate,stringoldstate; // sets up strings used in "keys" section
    String gearN, gearNold, gearL, gearLold, gearR, gearRold; //sets up the variables for the gear nose, gear left, gear right along with their "old" reading variable.
    String AP, APold;
    
    
    
    
    void setup() {
    // Setup the pins 2 to 7 as outputs for the LED's 
    for (int doutPin = 2; doutPin <= 8; doutPin++) //pin 7 was used here. changed it to 8
    { 
    pinMode(doutPin, OUTPUT);
    digitalWrite(doutPin, HIGH); 
    }
    
    stringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111"; // Gives the "keys old" string a value
    // Setup the pins as inputs as used by "Keys" (For the "Keys" program avoid actually using Pin 13 as an input ,, long story)
    for (int doutPin = 9; doutPin <= 19; doutPin++) // if you have a Mega card and want to use all the pins ,, change the 19 to 69
    { 
    pinMode(doutPin, INPUT);
    digitalWrite(doutPin, HIGH); 
    }
    Serial.begin(115200); // sets up the baud rate for the serial coms. 
    }
    
    
    void loop() { // this first bit is for the "Keys" read.
    // oldpinStateSTR = (""); // makes this string "" (Nothing)
    //pinStateSTR = (""); // makes this string "" (Nothing)
    stringnewstate = ""; // makes this string "" (Nothing)
    for (int pinNo = 9; pinNo <= 19; pinNo++){ // for reading the pins 2 to 19 (For a mega card change the 19 to 69)
    pinStateSTR = String(digitalRead(pinNo)); //gets the pins state
    oldpinStateSTR = String(stringoldstate.charAt(pinNo - 9));// changes that pins status within the "old" string
    
    if (pinStateSTR != oldpinStateSTR) //checks if there is a "change of state" then if there is ,,,, 
    {
    Serial.print ("D"); //sends a "D" out the serial port ,,,
    if (pinNo < 10) Serial.print ("0"); //adds a zero if pin No. less than 10 (to keep the data length at 4)
    Serial.print (pinNo); // followed by the pin number ,,,,,
    Serial.print (pinStateSTR); // followed byt the pin state. (Now high or low)
    }
    stringnewstate += pinStateSTR; // Updates the 
    }
    
    
    stringoldstate = stringnewstate; // updates the "old" string
    
    
    if (Serial.available() > 0) { //if there is a charator in the serial receive buffer then ,,,,
    xx = Serial.read(); // read it.
    if (xx == 'M'){ // Found the reading "Flaps"
    delay (11); // It seems to need a delay here
    flaps = "";
    flaps += char(Serial.read()); //Read the first charactor sent
    flaps += char(Serial.read()); //Read the second charactor sent and add it to the first
    flaps += char(Serial.read()); //Read the third charactor sent and add it to the other two.) 
    if (flaps != flapsold){ // checks to see if its different to the "old" reading from the last loop
    if (flaps == "000") {digitalWrite(6, LOW);} //if its "000" then turn OFF the LED
    if (flaps > "000") { // if its greater than "000" then ,,,
    digitalWrite(6, HIGH); // set the LED on
    delay(50); // wait for 50 milliseconds
    digitalWrite(6, LOW); // set the LED off
    delay(50); // wait for 50 millisecon
    } // end of "moving" sequence. When there is no new change, the LED will go out.
    if (flaps > "030") {digitalWrite(7, HIGH);} else {digitalWrite(7, LOW);} //if flaps over "030" degree's then turn ON the LED
    
    flapsold = flaps; // Makes the old reading the same as the new reading.(So on the next reading it can check to see if it's different)
    
    } // end of the "yes it's different to the old reading" sequence 
    } //end of "found the "flaps" (M) read
    if (xx == 'I'){ // Found the reading "Gear Nose"
    delay (11); // It seems to need a delay here
    gearN = "";
    gearN += char(Serial.read()); //Read the first charactor sent
    gearN += char(Serial.read()); //Read the second charactor sent and add it to the first
    gearN += char(Serial.read()); //Read the third charactor sent and add it to the other two.) 
    if (gearN != gearNold){ // checks to see if its different to the "old" reading from the last loop
    if (gearN == "100") {digitalWrite(2, HIGH);} else {digitalWrite(2, LOW);} //if its "100" then turn ON the LED ,, anything else, turn it OFF
    gearNold = gearN; // Makes the old reading the same as the new reading.(So on the next reading it can check to see if it's different)
    } // end of the "yes it's different to the old reading" sequence 
    } //end of "found the gear NOSE" (I) read 
    
    if (xx == 'J'){ // Found the reading "Gear LEFT"
    delay (11); // It seems to need a delay here
    gearL = "";
    gearL += char(Serial.read()); //Read the first charactor sent
    gearL += char(Serial.read()); //Read the second charactor sent and add it to the first
    gearL += char(Serial.read()); //Read the third charactor sent and add it to the other two.) 
    if (gearL != gearLold){ // checks to see if its different to the "old" reading from the last loop
    if (gearL == "100") {digitalWrite(3, HIGH);} else {digitalWrite(3, LOW);} //if its "100" then turn ON the LED ,, anything else, turn it OFF
    gearLold = gearL; // Makes the old reading the same as the new reading.(So on the next reading it can check to see if it's different)
    } // end of the "yes it's different to the old reading" sequence 
    } //end of "found the gear LEFT" (J) read 
    
    if (xx == 'K'){ // Found the reading "Gear RIGHT"
    delay (11); // It seems to need a delay here
    gearR = "";
    gearR += char(Serial.read()); //Read the first charactor sent
    gearR += char(Serial.read()); //Read the second charactor sent and add it to the first
    gearR += char(Serial.read()); //Read the third charactor sent and add it to the other two.) 
    if (gearR != gearRold){ // checks to see if its different to the "old" reading from the last loop
    if (gearR == "100") {digitalWrite(4, HIGH);} else {digitalWrite(4, LOW);} //if its "100" then turn ON the LED ,, anything else, turn it OFF
    gearRold = gearR; // Makes the old reading the same as the new reading.(So on the next reading it can check to see if it's different)
    } // end of the "yes it's different to the old reading" sequence 
    } //end of "found the gear RIGHT" (K) read 
    
    
    
    
    
    
    if (gearN != "000" && gearN != "100" || gearL != "000" && gearL != "100" ||gearR != "000" && gearR != "100" )// checks if any gear is not totally up or down
    {digitalWrite(5, HIGH);} //if any gear is NOT totally up or totally down ,, turns LED ON ,, indicating moving.
    else
    {digitalWrite(5, LOW);} // Turns LED OFF if all the gear is totally UP or DOWN. 
    
    
    if (xx == 'b'){ // Found the reading "Gear RIGHT"
    delay (11); // It seems to need a delay here
    AP = "";
    AP += char(Serial.read()); //Read the ONLY character sent
    
    if (AP != APold){ // checks to see if its different to the "old" reading from the last loop
    if (AP == "0") {digitalWrite(8, LOW);} //if its "000" then turn OFF the LED
    if (AP == "1") {digitalWrite(8, HIGH); 
    APold = AP; 
    
    { 
    // Deleted if (AP!= "0") ...
    
    } 
    
    
    } // end of the "yes it's different to the old reading" sequence 
    } 
    
    
    
    } // end of "Yes" there is a serial read.
    
    
    } 
    
    
    }
    
    Hope this helps & good luck!

  3. Thanks Jahanzeb thanked for this post
  4. #3
    25+ Posting Member
    Join Date
    Sep 2013
    Location
    Lahore
    Posts
    33
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: help with arduino code

    first of all, thank you so much for reading through and debugging it. especially explaining what i was doing wrong. it was a great learning experience.
    the code worked like it should

    a bit confued though about one point.

    i thought

    if (AP > "0")

    means the same as

    if (AP == "1")

    because they're both greater than 0? any value greater than 0 should light up the LED no?


    secondly, after testing it on fsx, the 8 pin would only light up once and turn off once. was'nt a repeated task. i read through the code, and eliminated the last line which read,
    APold = AP;

    i realized that i was setting the old value to the new value after turning the light off once. that solved the problem.

    thanks again
    cheers

  5. #4
    150+ Forum Groupie
    Join Date
    Apr 2011
    Location
    United States of America
    Posts
    232
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: help with arduino code

    Yup.
    if (AP > "0") means the same as if (AP == "1")
    I just wanted to emphasize the values could only be "0" or "1"; i.e, not "2", "3", etc.

    Glad you got it rolling.
    cheers

  6. Thanks Jahanzeb thanked for this post