Code assistance required....this is a bit of a challenge for me!

Hello, all.

I am using a Teensy 3.2 board interfaced with X-Plane 11.55 and am trying (desperately) to get two (2) particular 'functions' to work.

I am re-working a sketch sent to me by a friend.

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 (on, momentarily)

Initial coding is as follows:

//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
}
}

The front half works beautifully......for the four (4)-digit code inserted by rotating dials.

Its the MODE (Power) switch positions and the IDENT button position which are giving me fits.
I've just learned (from another forum) that I cannot have more than 2 cases on the digitalRead() function....which explains why only 2 switch positions on the MODE switch work at any given time.

It had been suggested to me to try this....but, still a no-go...

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

Can anyone please shed some light in my direction so I can stop having moments of uncontrollable cursing and get some decent sleep at night? LOL
Seriously, I had been trying numerous alternate codings in the past week or so, all to no avail.

Thank you so very much in advance.

Jay

P.S. In my original post, I neglected to mention that I will be using a shield with an Arduino Mega and Centipede Shield once I can determine the proper and correct coding.
My bad..

Big mess there because you didn't post using code tags. Please fix, using "copy for forum" in the IDE and then paste in your message...

or there might be some uncontrollable cursing on this end... :slight_smile:


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

Where did you get this syntax from ?

It's a semantic as well as a syntax error. It attempts to derive 3 different results from a binary condition.

It was in the original code sent to me by a colleague which I now find, ain't gonna work!

Thank you.

Any recommendations of how this should/could be coded?

Switch-case maybe??

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!

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.