Requesting coding help....X-Plane flight sim

EAL727Capt

Well-known member
I'm attempting to write a code for a Teensy 4.1 board interfaced with my X-Plane 11 simulator and am running into brick walls.

Right now, I'm working on a function in my home cockpit called ENG FAIL / PRESS TO CANCEL
In a nutshell, if any engine fails, it illuminates two (2) lights on the glare shield, one (1) on each side, Captain and First Officer.
Pressing either of these lighted buttons will acknowledge the failure and extinguish the lights. I had previously written the code for the ENG FAIL portion, using an relay to control the 28VDC lights and it works beautifully but am having trouble in finding the solution for the PRESS TO CANCEL part, which kills the lights.

My latest attempt at this function is as follows:
N.B. I'm only testing Engine 1 (of 3) right now. If I can get Engine 1 to work, I'll add additional lines for Engines 2 and 3.



/*
TESTING ENG FAIL/PRESS TO CANCEL
When any engine fails, the affected engine's data ref goes to a state of '6'
When the PRESS TO CANCEL button is pressed, this resets the engine's data ref to a state of '0'
*/

int EngFailReset = 1;

FlightSimFloat Eng1Fail;

void setup() {
Serial.begin(9600);

pinMode(EngFailReset, INPUT_PULLUP);

Eng1Fail = XPlaneRef("sim/operation/failures/rel_engfai0");
}

void loop() {
FlightSim.update();

if (digitalRead(1) == HIGH); {
Eng1Fail = 0;
}
}


The above compiles without issue but when put into practice and tested in the sim, it doesn't do anything when either PRESS TO CANCEL button is pressed. I did check and do have continuity on both side's switch buttons, via a multimeter, so I know that the wiring is correct and working.

I've tried CONST INT, OUTPUT, digitalWRITE, etc., all to no avail and it is making me nuts!

Previously, I've had no issues with creating a code for a push button and have several switches and buttons that I've successfully interfaced thus far...but this one's got me wandering aimlessly in circles in the dark.

Any help and constructive suggestions are most certainly welcomed.

Thank you all very kindly.

Jay
 
How do you have your pushbuttons wired (using the information that you posted, one side of each switch should be connected to Pin 1, and the other side of each switch should be connected to GROUND) ?? With the sketch that you posted (BTW, it makes things much easier to read if you enclose your sketch in CODE tags, using the "</>" button right above the text composition area as you are creating your post) , does it work if you simply short Pin 1 to GROUND ??

Mark J Culross
KD5RXT
 
Start with the silly questions:
Did you try removing the ; after if (digitalRead(1) == HIGH) ?
You're currently completely ignoring that if and always setting EngFail to 0.

You enable the pullup on the input but then check for the input being high. If the pullup is enabled this would imply that the button pulls the pin low not high. Do you need to check for the input being LOW?

You did wire the buttons in parallel not series right?


Having tried those I think you also have a flaw in the basic logic.

When the button is pressed it resets the value of Eng1Fail to 0.
But I assume that in the flight sim the engine is still flagged as having failed. So what is to stop Eng1Fail from getting set back to 6 next time you update the flight sim status after someone has let go of the button?

If you want the light to stay off you need to remember that it's been turned off.

You probably want something like:
Code:
const int EngFailResetPin = 1;
const int engineOKValue = 0;
const int engineFailValue = 6;

bool clearE1Fail = false;


void loop() {
  FlightSim.update();
 
 
  if (digitalReadFast(EngFailResetPin) == HIGH) { // if button is pressed then flag the engine fail to be cleared.
    clearE1Fail = true;
  }
  if (Eng1Fail != engineFailValue) { // if the engine hasn't failed then reset the flag otherwise it'll stay set forever
    clearE1Fail = false;
  }
  if (clearE1Fail) {  // if this is true then engine currently has a non-0 status and the button has been pressed at some point after it changed from 0
    Eng1Fail = engineOKValue;
  }
 if (Eng1Fail == engineFailValue) {
    // Turn on light
 }
}

Also a couple of style notes -
I changed
int EngFailReset = 1;
to
const int EngFailResetPin = 1;
Making it a constant allows for more optimisation. And including Pin in the name makes it clear you mean the IO pin not the actual signal state. I know it's obvious now but when you look back at this in 6 months time it may not be.
And then use that everywhere, don't have the pin number as a number in some places and the constant in others.
Similarly use constants for the engine status values rather than numbers. It makes for more readable code.

If you are planning on supporting 3 engines then use arrays where possible for the statuses / flags / IO pins rather than Eng1Fail, Eng2Fail etc... It makes for cleaner code and easier to change if you want to say add support for a 4th engine later. Make sure you define the number of engines as a const int or # define and use that everywhere rather than hard coding the number.
 
Last edited:
Back
Top