Arduino FSX APU 7 Segment Display

All things Arduino!
Post Reply
admin
Site Admin
Posts: 21
Joined: Thu Feb 20, 2014 3:01 am

Arduino FSX APU 7 Segment Display

Post by admin »

Code for FSX 7 segment display:

Code: Select all

/* 
My Youtube Channel  : http://www.youtube.com/user/Shadow5549
Many Learjet 45 series home cockpit build videos and Arduino projects can be found on my channel

This is my first attempt at a 7 segment display and is operation on a 4 digit version.  This will display the APU amp reading (Battery amps in Link2fs)
from FSX (which is not the best but works)  I would suggest scaling and delaying the values appropriately for your preference.
Simply use Link2fs program and on the extractions tab- check the box for ?J Battery Amps- this will display the APU AMPS
Note: Main bus amps also seems to paralell this data- As we all know FSX did not model the APU accurately
Also of note for future builds if you try to use the data "as-is"  - 
The variable actually drops in amp reading down to approximately 442  if you turn off the aircraft engine generators with the APU 
running and drops even further as you add electrical loads such as pitot heat, Nav Lights, or turn Avionics off (so sad Aces... haha) 
Looks like they got the loading backwards.  We can fix this though. 


7 segment display code was gathered here:
http://www.instructables.com/id/Arduino-4-digit-7-segment-display/

Pin Layout for the display can be found here:
http://arduino.fisch.lu/Uploads/arduino/circuit_TYC-365GWB.png

For now- this code will work to display the actual values from FSX.  Cheers!

From Jim's original Link2fs code:
    This code is in the public domain
    For use with "Link2fs_Multi"
    Jimspage.co.nz
    My thanks to the Guys that gave me snippets of code. 
*/

int CodeIn;// used on all serial reads
int KpinNo; 
int Koutpin;
String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;


int aPin = 2;  //         A
int bPin = 3;  //      ________
int cPin = 4;  //     |        |
int dPin = 5;  //   F |        |  B
int ePin = 6;  //     |    G   |
int fPin = 7;  //     |________|
int gPin = 8;  //     |        |
int GND1 = 9;  //     |        |
int GND2 = 10; //   E |        |   C
int GND3 = 11; //     |________|
int GND4 = 12; //        
int num;       //         D
int dig1;
int dig2 = 0;
int dig3 = 0;
int dig4 = 0;
int DTime = 2;


void setup()
{
  pinMode(aPin, OUTPUT);
  pinMode(bPin, OUTPUT);
  pinMode(cPin, OUTPUT);
  pinMode(dPin, OUTPUT);
  pinMode(ePin, OUTPUT);  
  pinMode(fPin, OUTPUT);
  pinMode(gPin, OUTPUT);
  pinMode(GND1, OUTPUT);
  pinMode(GND2, OUTPUT);
  pinMode(GND3, OUTPUT);
  pinMode(GND4, OUTPUT);
  Serial.begin(115200);
  Serial.setTimeout(20);// HA!! This is the KEY to getting the parseInt to work- otherwise you get a 1 second delay
/*
From : http://forum.arduino.cc/index.php?topic=99336.0
Michael Margolis' "Arduino Cookbook" (2nd edition, page 113) to the rescue!
It turns out that the Serial class (which extends the Stream class) 
uses the Stream.setTimeout() function. The default value for the timeout is 1000 (one second).
 I just set it to something smaller, like 50 ms, and the sketch is nice and zippy. 
Here's the complete example sketch. Turn off line endings in the serial monitor...
*/


}
 void loop()
{
digitalWrite( GND1, HIGH);
digitalWrite( GND2, HIGH);
digitalWrite( GND3, HIGH);
digitalWrite( GND4, HIGH);

 if (Serial.available()) {
    CodeIn = getChar();
    //if (CodeIn == '=') {EQUALS();} // The first identifier is "="
    //if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<"
    if (CodeIn == '?') {QUESTION();}// The first identifier is "?"
    //if (CodeIn == '/') {SLASH();}// The first identifier is "/" (Annunciators)
}
digitalWrite( GND4, LOW);    //digit 4
  pickNumber(dig4);
  delay(DTime);
  digitalWrite( GND4, HIGH);
  
  digitalWrite( GND3, LOW);    //digit 3
  pickNumber(dig3);
  delay(DTime);
  digitalWrite( GND3, HIGH);
  
  digitalWrite( GND2, LOW);   //digit 2
  pickNumber(dig2);
  delay(DTime);
  digitalWrite( GND2, HIGH);
  
  digitalWrite( GND1, LOW);   //digit 1
  pickNumber(dig1);
  delay(DTime);
  digitalWrite( GND1, HIGH);
 
}
 char getChar()// Get a character from the serial buffer
{
  while(Serial.available() == 0);// wait for data
  return((char)Serial.read());// Thanks Doug
}
 
 void QUESTION(){    // The first identifier was "?"
 CodeIn = getChar(); // Get another character
  switch(CodeIn) {// Now lets find what to do with it
    case 'J'://Found the second identifier  Battery Amps from Simconnect
     
   num = Serial.parseInt();
  //Serial.println(num);
  dig1 = num / 1000;
  num = num - (dig1 * 1000);
  dig2 = num / 100;
  num = num - (dig2 * 100);
  dig3 = num / 10;
  dig4 = num - (dig3 *10);  
}
 }
void pickNumber(int x)
{
   switch(x){
     case 1: one(); break;
     case 2: two(); break;
     case 3: three(); break;
     case 4: four(); break;
     case 5: five(); break;
     case 6: six(); break;
     case 7: seven(); break;
     case 8: eight(); break;
     case 9: nine(); break;
     default: zero(); break;
   } 
}
void clearLEDs()
{   
  digitalWrite(  2, LOW); // A
  digitalWrite(  3, LOW); // B
  digitalWrite(  4, LOW); // C
  digitalWrite(  5, LOW); // D
  digitalWrite(  6, LOW); // E
  digitalWrite(  7, LOW); // F
  digitalWrite(  8, LOW); // G 
}

void one()
{
  digitalWrite( aPin, LOW); 
  digitalWrite( bPin, HIGH);
  digitalWrite( cPin, HIGH); 
  digitalWrite( dPin, LOW); 
  digitalWrite( ePin, LOW); 
  digitalWrite( fPin, LOW); 
  digitalWrite( gPin, LOW); 
}

void two()
{
  digitalWrite( aPin, HIGH); 
  digitalWrite( bPin, HIGH);
  digitalWrite( cPin, LOW); 
  digitalWrite( dPin, HIGH); 
  digitalWrite( ePin, HIGH); 
  digitalWrite( fPin, LOW); 
  digitalWrite( gPin, HIGH); 
}

void three()
{
  digitalWrite( aPin, HIGH); 
  digitalWrite( bPin, HIGH);
  digitalWrite( cPin, HIGH); 
  digitalWrite( dPin, HIGH); 
  digitalWrite( ePin, LOW); 
  digitalWrite( fPin, LOW); 
  digitalWrite( gPin, HIGH); 
}

void four()
{
  digitalWrite( aPin, LOW); 
  digitalWrite( bPin, HIGH);
  digitalWrite( cPin, HIGH); 
  digitalWrite( dPin, LOW); 
  digitalWrite( ePin, LOW); 
  digitalWrite( fPin, HIGH); 
  digitalWrite( gPin, HIGH); 
}

void five()
{
  digitalWrite( aPin, HIGH); 
  digitalWrite( bPin, LOW);
  digitalWrite( cPin, HIGH); 
  digitalWrite( dPin, HIGH); 
  digitalWrite( ePin, LOW); 
  digitalWrite( fPin, HIGH); 
  digitalWrite( gPin, HIGH); 
}

void six()
{
  digitalWrite( aPin, HIGH); 
  digitalWrite( bPin, LOW);
  digitalWrite( cPin, HIGH); 
  digitalWrite( dPin, HIGH); 
  digitalWrite( ePin, HIGH); 
  digitalWrite( fPin, HIGH); 
  digitalWrite( gPin, HIGH); 
}

void seven()
{
  digitalWrite( aPin, HIGH); 
  digitalWrite( bPin, HIGH);
  digitalWrite( cPin, HIGH); 
  digitalWrite( dPin, LOW); 
  digitalWrite( ePin, LOW); 
  digitalWrite( fPin, LOW); 
  digitalWrite( gPin, LOW); 
}

void eight()
{
  digitalWrite( aPin, HIGH); 
  digitalWrite( bPin, HIGH);
  digitalWrite( cPin, HIGH); 
  digitalWrite( dPin, HIGH); 
  digitalWrite( ePin, HIGH); 
  digitalWrite( fPin, HIGH); 
  digitalWrite( gPin, HIGH); 
}

void nine()
{
  digitalWrite( aPin, HIGH); 
  digitalWrite( bPin, HIGH);
  digitalWrite( cPin, HIGH); 
  digitalWrite( dPin, HIGH); 
  digitalWrite( ePin, LOW); 
  digitalWrite( fPin, HIGH); 
  digitalWrite( gPin, HIGH); 
}

void zero()
{
  digitalWrite( aPin, HIGH); 
  digitalWrite( bPin, HIGH);
  digitalWrite( cPin, HIGH); 
  digitalWrite( dPin, HIGH); 
  digitalWrite( ePin, HIGH); 
  digitalWrite( fPin, HIGH); 
  digitalWrite( gPin, LOW); 
}
Post Reply