Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 19 of 19

Thread: XPlane interfacing coding challenge...

  1. #1
    Member
    Join Date
    Dec 2021
    Location
    Las Vegas, NV
    Posts
    30

    XPlane interfacing coding challenge...

    Hello!

    Am re-working a sketch which was sent to me and I cannot get it to read/compile correctly.

    Background: I am interfacing OEM aircraft radios, gauges, instruments, etc. and have many, many unopened Teensys on my shelf....3.2, 4.1. For the project interface I'm presently working, its the aircraft transponder which radar identifies your aircraft to air traffic controllers.
    You'll dial-in a code specified by the controller so he/she may 'see' you on radar.

    This code 'coding' on my unit works flawlessly.

    I'm having challenges attempting to code the POWER/MODE button. There are three (3) possible modes for my transponder unit:
    0 = OFF
    1 = STAND-BY
    2 = ON
    Additionally, the other challenge I'm having is with the IDENT button. When air traffic control requests you to "squawk code 3214 and ident," dialing-in 3214 and activating this button will cause your radar target to blossom momentarily, making it easier for the controller to 'see' and identify you.
    There are two (2) conditions:
    0 = Off (by default)
    1 = Ident

    My code, thus far, is as follows:

    Code:
    //Transponder  TEST
    
    const int digit1A = 2;  //J1-2  'B' GND ON PIN 8
    const int digit1B = 3;  //J1-3  'C' GND ON PIN 8
    const int digit1C = 4;  //J1-4  'D' GND ON PIN 8
    const int digit2A = 5;  //J1-5  'E' GND ON PIN 8
    const int digit2B = 6;  //J1-6  'F' GND ON PIN 8
    const int digit2C = 7;  //J1-7  'G' GND ON PIN 8
    const int digit3A = 15; //J1-15 'J' GND ON PIN 8
    const int digit3B = 16; //J1-16 'K' GND ON PIN 8
    const int digit3C = 17; //J1-17 'L' GND ON PIN 8
    const int digit4A = 19; //J1-19 'N' GND ON PIN 8
    const int digit4B = 20; //J1-20 'O' GND ON PIN 8
    const int digit4C = 8;  //J1-28 'P' GND ON PIN 8
    const int ident = 9;    //J1-14     GND ON PIN 8
    const int xpdrON = 1;   //J1-23     GND ON PIN 8
    
    FlightSimInteger xpdrCode;
    FlightSimInteger xpdrMode;
    FlightSimInteger xpdrIdent;
    
    //Function to build the ATC code
    int getAtcFreq()
    {
      int freq = 0;
      int digit1 = 0;
      int digit2 = 0;
      int digit3 = 0;
      int digit4 = 0;
    
      bool a1 = !digitalRead(digit1A);
      bool b1 = !digitalRead(digit1B);
      bool c1 = !digitalRead(digit1C);
      digit1 = ((a1 + b1 * 2 + c1 * 4) * 1000);
    
      bool a2 = !digitalRead(digit2A);
      bool b2 = !digitalRead(digit2B);
      bool c2 = !digitalRead(digit2C);
      digit2 = ((a2 + b2 * 2 + c2 * 4) * 100);
    
      bool a3 = !digitalRead(digit3A);
      bool b3 = !digitalRead(digit3B);
      bool c3 = !digitalRead(digit3C);
      digit3 = ((a3 + b3 * 2 + c3 * 4) * 10);
    
      bool a4 = !digitalRead(digit4A);
      bool b4 = !digitalRead(digit4B);
      bool c4 = !digitalRead(digit4C);
      digit4 = (a4 + b4 * 2 + c4 * 4);
    
      freq = (digit1 + digit2 + digit3 + digit4);
      return freq;
    }
    long previousMillis = 0;
    long interval = 1000;
    
    void setup()
    {
      Serial.begin(38400);
      pinMode(digit1A, INPUT_PULLUP);
      pinMode(digit1B, INPUT_PULLUP);
      pinMode(digit1C, INPUT_PULLUP);
      pinMode(digit2A, INPUT_PULLUP);
      pinMode(digit2B, INPUT_PULLUP);
      pinMode(digit2C, INPUT_PULLUP);
      pinMode(digit3A, INPUT_PULLUP);
      pinMode(digit3B, INPUT_PULLUP);
      pinMode(digit3C, INPUT_PULLUP);
      pinMode(digit4A, INPUT_PULLUP);
      pinMode(digit4B, INPUT_PULLUP);
      pinMode(digit4C, INPUT_PULLUP);
      pinMode(ident, INPUT_PULLUP);
      pinMode(xpdrON, INPUT_PULLUP);
    
    
      xpdrCode = XPlaneRef("sim/cockpit2/radios/actuators/transponder_code");
      xpdrMode = XPlaneRef("FJS/727/Radios/Transponder_ModeKnob");
      xpdrIdent = XPlaneRef("sim/cockpit/radios/transponder_id");
    }
    
    void loop()
    {
      FlightSim.update();
      unsigned long currentMillis = millis();
      if (currentMillis - previousMillis > interval) {
        previousMillis = currentMillis;
        xpdrCode = getAtcFreq();
    
        if (digitalRead(1) == HIGH) {
          xpdrMode = 0;   // OFF
        }    
        else if {
        xpdrMode = 1;}    // STBY
          else{
        xpdrMode = 2;}    // ON
    }
    
    if (digitalRead(9) == HIGH) {
        xpdrIdent = 0;   // OFF
      }   
      else {
        xpdrIdent = 1;   // ON...IDENT
      }   
    }

    Compiling error is on the 'else if' section expected '{' before '{' token. Adding or subtracting a { has no effect.

    When I remove that portion, it compiles and functions without issue, albeit without the STAND-BY option.

    Then second challenge I'm having is with the 'if (digitalRead(9) == HIGH)' section...
    It compiles and I'm expecting it to then work in X-Plane, but it doesn't....no interfacing at all with X-Plane for this portion of the sketch. The X-Plane dataref is correct.
    I have checked, double-checked and triple-checked every pin connection and they are all correct as is.
    I am probably missing something very elementary and simple and will probably hang my head in shame when pointed-out....LOL
    Its been baffling to me so after two (2) weeks of constantly working toward a solution, I'm reaching-out to the gurus of the bunch for any insights and guidance.

    Thank you in advance.

    Jay

  2. #2
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    705
    Code:
        if (currentMillis - previousMillis > interval) {
            previousMillis = currentMillis;
            xpdrCode = getAtcFreq();
    
            if (digitalRead(1) == HIGH) {
                xpdrMode = 0;   // OFF
            }
            else if {   // SHOULD BE else if (a=b) {    <<---- OR SOME OTHER TESTED CONDITION
                xpdrMode = 1;     // STBY
            }
            else {
                xpdrMode = 2;
            }    // ON
        }
    You have an else if without any testing condition on line 92.
    See my code snippet above.

  3. #3
    Member
    Join Date
    Dec 2021
    Location
    Las Vegas, NV
    Posts
    30
    Hiya and thank you for the reply.

    I'm not clear what you're suggesting here... what is 'a' and what is 'b' in this case? The conditions can only be a 0, 1 or 2 and they do not ever equal each other.

    Can you kindly explain?

  4. #4
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    705
    You have else if {.
    The syntax for if...else if....else is shown below
    if (condition1) {
    // block of code to be executed if condition1 is true
    } else if (condition2) {
    // block of code to be executed if the condition1 is false and condition2 is true
    } else {
    // block of code to be executed if the condition1 is false and condition2 is false
    }
    You are missing the (condition2) as in the example above.
    The (a=b) was just to show a condition statement.

  5. #5
    Member
    Join Date
    Dec 2021
    Location
    Las Vegas, NV
    Posts
    30
    Thank you, again, for the assist.

    Its still not compiling correctly.

    if (digitalRead(1) == HIGH) {
    xpdrMode = 0; // OFF
    } else if (xpdrMode = 1);{ // STBY
    } else (xpdrMode = 2);{ // ON
    }

  6. #6
    Senior Member BriComp's Avatar
    Join Date
    Apr 2014
    Location
    Cheltenham, UK
    Posts
    705
    Code:
       if (digitalRead(1) == HIGH) {
            xpdrMode = 0; // OFF
        }
        else if (xpdrMode = 1); { // STBY
        }
        else (xpdrMode = 2); { // ON
        }
    Above is your code snippet formatted in a code block.
    Remember the syntax for if...else if....else shown below
    if (condition1) {
    // block of code to be executed if condition1 is true
    } else if (condition2) {
    // block of code to be executed if the condition1 is false and condition2 is true
    } else {
    // block of code to be executed if the condition1 is false and condition2 is false
    }
    Let's step through it a line at a time:-
    Code:
    Syntax                                                                                              your code                                              Notes
    if (condition1 ) {                                                                                  if (digitalRead(1) == HIGH) {
    // block of code to be executed if condition1 is true                                                   xpdrMode = 0; // OFF
    } else if (condition2) {                                                                            } else if (xpdrMode = 1);{ // STBY                    Take out the ";"
    // block of code to be executed if the condition1 is false and condition2 is true                                                                         NOTHING THERE
    } else {                                                                                            } else (xpdrMode = 2);{ // ON                         { brackets in the wrong place
    }
    // block of code to be executed if the condition1 is false and condition2 is false      
    }

  7. #7
    Member
    Join Date
    Dec 2021
    Location
    Las Vegas, NV
    Posts
    30
    Exactly what I did...

    if (digitalRead(1) == HIGH) {
    xpdrMode = 0; // OFF
    }
    else if (xpdrMode = 1); { // STBY
    }
    else (xpdrMode = 2); { // ON
    }

    And it errors out with....

    Transponder_TEST: In function 'void loop()':

    Transponder_TEST:95: error: 'else' without a previous 'if'

    else (xpdrMode = 2); { // ON

    ^

    Transponder_TEST:96: error: expected '}' at end of input
    }

    ^

    'else' without a previous 'if'

  8. #8
    Member
    Join Date
    Dec 2021
    Location
    Las Vegas, NV
    Posts
    30
    What's most frustrating is this code snipet works wonderfully with just the 0 (OFF) and 1 (ON).

    if (digitalRead(1) == HIGH) {
    xpdrMode = 0; // OFF
    } else xpdrMode = 2; { // ON
    }
    }

    It's the IF ELSE section that's giving fits and nightmares!!!

  9. #9
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    10,328
    There are a few problems with the code:
    Code:
     if (digitalRead(1) == HIGH) {
            xpdrMode = 0; // OFF
        }
        else if (xpdrMode = 1); { // STBY
        }
        else (xpdrMode = 2); { // ON
        }
    Look at line: else if (xpdrMode = 1); { // STBY

    first part (xprMode = 1) you are doing an asignment of 1 to xprMode... I am assuming you want to check for equality:
    else if (xpdrMode == 1); { // STBY

    Second part the ; ends the processing of the else if that is it says if it is == 1 don't do anything and the { is not part
    of an if/else if/else... clause so the else will not compile:

    Should probably be:
    Code:
    
    
    Code:
     if (digitalRead(1) == HIGH) {
            xpdrMode = 0; // OFF
        }
        else if (xpdrMode == 1) { // STBY
        }
        else (xpdrMode == 2) { // ON
        }

  10. #10
    Member
    Join Date
    Dec 2021
    Location
    Las Vegas, NV
    Posts
    30
    Hi, KurtE....

    Still erroring-out.....

    if (digitalRead(1) == HIGH) {
    xpdrMode = 0; // OFF
    }
    else if (xpdrMode == 1) { // STBY
    }
    else (xpdrMode == 2) { // ON
    }



    Transponder_TEST: In function 'void loop()':


    Transponder_TEST:95: error: expected ';' before '{' token

    else (xpdrMode == 2) { // ON

    ^

    Transponder_TEST:96: error: expected '}' at end of input

    }

    ^

    expected ';' before '{' token

  11. #11
    Member
    Join Date
    Dec 2021
    Location
    Las Vegas, NV
    Posts
    30
    For what its worth, I also tried several variations with
    if (digitalRead(1) == LOW) { etc.
    and just including the xpdrMode == 1 and xpdrMode == 2.....no go there, either...

    The challenge continues.

    I am confident there is a solution.... there has to be... only three (3) possible states.

  12. #12
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    10,328
    Sorry I have no idea what your code is trying to do... So again not sure how to fix...

    Code:
    if (digitalRead(1) == HIGH) {
    xpdrMode = 0; // OFF
    }
    else if (xpdrMode == 1) { // STBY
    }
    else (xpdrMode == 2) { // ON
    }
    That is I understand if the code was like:
    Code:
    if (digitalRead(1) == HIGH) {
        xpdrMode = 0; // OFF
    } else  { // ON}
        xpdrMode = 2;
    }
    That is if the digitalRead returns high then xprMode is assigned a zero else it is assigned a 2...

    But with your code with else if, was unclear of you are testing it here or there is some not defined test that if true assigns a 1 to that variable...
    example:
    Code:
    if (digitalRead(1) == HIGH) {
        xpdrMode = 0; // OFF
    } else if (digitalRead(2) == high) {
        xpdrMode = 1;
    } else  { // ON}
        xpdrMode = 2;
    }
    But again I don't know what logically triggers your 3 different states.

    Edit: Note: with digitalRead(1) there is only 2 states, either it is pressed or not processed.

  13. #13
    Member
    Join Date
    Dec 2021
    Location
    Las Vegas, NV
    Posts
    30
    Hi, Kurt...

    Quote Originally Posted by KurtE View Post
    But again I don't know what logically triggers your 3 different states.
    The 'trigger' is a dial switch on the actual radio unit.

    Based upon what you're saying (and what I'm now researching..again..) on the digitalRead() function, I'm convinced that this operation will not work with digitalRead.

    Given this, do you (or anyone else) have any suggestions or recommendations of how this could be accomplished?

    Jay

  14. #14
    Member
    Join Date
    Dec 2021
    Location
    Las Vegas, NV
    Posts
    30
    Maybe the SWITCH CASE function? Treat the MODE switch as a rotary switch, which it technically is.....?

    Yes?
    No?
    Maybe?

  15. #15
    Senior Member
    Join Date
    Apr 2014
    Location
    -
    Posts
    9,756
    What is this "dial switch" ?
    How is it connected?
    Please post photos.
    Or better something like a datasheet.

    You just can't read three different digital states with one digital pin.

    Also, it would be good to learn at least the basics of C. There are good books.

  16. #16
    Member
    Join Date
    Dec 2021
    Location
    Las Vegas, NV
    Posts
    30
    Quote Originally Posted by Frank B View Post
    What is this "dial switch" ?
    How is it connected?
    Please post photos.
    Or better something like a datasheet.

    You just can't read three different digital states with one digital pin.

    Also, it would be good to learn at least the basics of C. There are good books.
    I am learning C++ as we speak and have attempted various codings to no avail, thus the reason I turned to the forum.

    A photograph/datasheet won't help.
    Think of a rotary switch with three (3) different positions for ON, STAND-BY and OFF, each with their own separate values to be 'sent' to the X-Plane.

  17. #17
    Senior Member
    Join Date
    Apr 2014
    Location
    -
    Posts
    9,756
    How many pins?

  18. #18
    Member
    Join Date
    Dec 2021
    Location
    Las Vegas, NV
    Posts
    30
    Only one (1) pin, with three (3) possible 'states' to be read.

    0 = OFF
    1 = STAND-BY
    2 = ON

    I have successfully coded and operated for the '0' and '1' or '0' and '2' but cannot get it to read a third 'state' (position) at the same time. I've also tried the digitalRead() == LOW and it hadn't helped.
    It should be noted that X-Plane assigns these states based upon input. They cannot be altered in any way.

    Thus, the frustration and confusion.

  19. #19
    Member
    Join Date
    Dec 2021
    Location
    Las Vegas, NV
    Posts
    30
    Almost sorted!!

    Major frustration forced me to physically open up the unit itself and take a close look at the switch. Imagine my surprise when I discovered that this MODE switch is, indeed, a rotary switch but with a wafer, containing numerous contact points, similar to the four (4) CODE numbers.
    Now, no biggie…I’ll determine which combination of contacts equal the values, similar to the
    bool a1, b1, c1, a2, b2, c2, etc., as shown for the transponder code numbers.
    Whew!
    Also, I discovered some broken wires which are (or should be) connected to the unit’s jack pins. With a little soldering, all will be right as rain.

    Now, I just need to figure out the IDENT button function and I’m all set!!
    While I had the unit opened-up, I looked at the IDENT button—it is fairly simple….On or Off—-normal push button.
    Last edited by EAL727Capt; 01-23-2022 at 01:57 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •