Baron 58 simulator standalone Arduino code development archive (2012-2013)

The Baron 58 panel simulator project was considered as platform for creating an effective control interface based on Arduino. Here you can see our early "pre-library" Arduino program code developments for Baron 58 cockpit before we switched to our later "library-plugin-based" interface.

Arduino controls for Baron 58 cockpit with 128-inputs extentsion board

Note: The input extension method described here was designed specifically for our Baron 58 project and should NOT be considered as the only possible or recommended option for your own cockpit project!

You may use our latest ArdSimX/SimVimCockpit interface without the need to program Arduino.

Control interface for Baron-58 panel is based on one Arduino Mega-1280 and one Ethernet Shield. For input extension DIY extension board for additional 128 inputs was made (it uses 12 digital pins of Arduino). Together with another Arduino pins this interface represents all of Baron controls. For annunciators the output extension board was used. It controls the 24 LEDs of annunciators panel, gear and flap lamps and audio panel LEDs.






The program for Baron-58 cockpit (or other GA plane) interface was initialy based on our program code for Arduino which is presented on this page below. Later it was rebuilt for XPData Library. We plan to use the B58 Panel as test platform for our latest SimVimX System Interface


Program

Working code version for Baron 58 Panel (July 2013).
The method UDP CMND is mainly used for all actuators (send commands to simulator instead of Datarefs used in previous vesions) The CMND function was added - comsend (char Data_com[]) The 3 functions are used for data transmission - comsend (for commands), drefsend ( for drefs), charsend (for chars).

The program reads each 16 ports of the expansion bus (one by one), checks each actuator position and, if in was changed it calls needed function for commands or datarefs and stores current input status in memory. Then the program reads next port (8 inputs)

Code block for UDP reading checks the presence of incoming UDP packet and, if the packet is received, it is looking each 36 byte for number of data groups #1,13,14,34,52,62,67,105,108,114,124,127 (time, Flaps, Brakes, RPM, Alt bus, Fuel, Gear, OMI, Autopilot on, Pitot, Bus volts, Gear warn).
Program reads values of each needed parameter in each group and sends them to LED indicators and servos.

/*
Interface for Baron 58 Simulator

Testing UDP protocol

Version with UDP CMND and DREF  packets sending  as main methods

July,  2013

Vlad
 
 */


#include <SPI.h>        
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Servo.h>


//-----------------------------
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 6);           // Arduino IP address
IPAddress ipx(192, 168, 1, 8);          // X-Plane IP address
IPAddress ipp(192, 168, 1, 5);         // Panel IP address
unsigned int localPort = 49001;       // port to listen to get UDP packets
unsigned int  XP = 49000;            // port to send UDP packets

EthernetUDP Udp;            // EthernetUDP instance to send/receive packets over UDP
byte DATA_buf[800];        // UDP.read buffer [size]
int packetSize;             // size of  packet

//--------------------------------------- Analog Inputs------------------
int Yoke_Pitch = 0;
int Yoke_Roll = 1;
int Rudder = 2;
int ThrottleL = 3;
int ThrottleR = 4;
int PropL = 5;
int PropR = 6;
int MixtureL = 7;
int MixtureR = 8;
int ToeBreakL = 9;
int ToeBreakR = 10;
int CowlL = 11;
int CowlR = 12;
int FltInstr = 13;
int EngInstr = 14;
int Flaps = 15;
//-------------  SERVO Gauges (Lower control Panel)-------------
Servo FuelLGauge;   
Servo FuelRGauge;  
Servo AltLGauge;
Servo AltRGauge;
Servo BusGauge;

//---------------- Input BUS prev values -  16 Ports x8 bit--------
byte Bus1_mem = 0;
byte Bus2_mem = 0;
byte Bus3_mem = 0;
byte Bus4_mem = 0;
byte Bus5_mem = 0;
byte Bus6_mem = 0;
byte Bus7_mem = 0;
byte Bus8_mem = 0;
byte Bus9_mem = 0;
byte Bus10_mem = 0;
byte Bus11_mem = 0;
byte Bus12_mem = 0;
byte Bus13_mem = 0;
byte Bus14_mem = 0;
byte Bus15_mem = 0;
byte Bus16_mem = 0;

//-----------------------------------------

float Gear1 = 1;                 //--- Gear #1 position 
float Gear2 = 1;                 //--- Gear #2 position 
float Gear3 = 1;                 //--- Gear #3 position 

float FuelL = -1;                //---  Left Fuel Tank quantity 
float FuelR = -1;               //---  Right Fuel Tank quantity 

int AltL;                       //---  Left Alternator Amps
int AltR;                       //---  Right Alternator Amps
int AP_FD;                      //---  Autopilot/Flight director status
int Cabin_light = 39;          //---  Flood light on/off, pin #39
//------------------------------------------------------------------------------------- 
// float FuelSelector_L = 0;
// float FuelSelector_R = 0;
//------------------------------------------------------------------------------------
int ThrottleL_pre = 0;
int ThrottleR_pre = 0;
int PropL_pre = 122;
int PropR_pre = 122;
int MixtureL_pre = 0;
int MixtureR_pre = 0;
int CowlL_pre;
int CowlR_pre;
int Flaps_pre;

boolean Throttle_send = true;
boolean Prop_send = true;
boolean Mixture_send = true;

//----------------------------------------- Encoders -----------------

int Com1=11800;                                    //--- Com1 Freq.
int Com1_st=12880;                                 //--- Com1 standby Freq.
int Com2=12880;                                    //--- Com2 Freq. 
int Com2_st=11800;                                 //--- Com2 standby Freq. 
// int Com3=11800;

int Nav1=10800;                                   //--- Nav1 Freq.
int Nav1_st=11800;                                //--- Nav1 standby Freq.
int Nav2=11800;                                   //--- Nav2 Freq. 
int Nav2_st=10800;                                //--- Nav2 standby Freq.

int Nav1_OBS = 90;                               //--- Nav1 OBS Course (0-360)
int Nav2_OBS = 90;                               //--- Nav2 OBS Course (0-360)
int Alt_select = 3000;                           //--- Preselected Altitude 
float Alt_baro = 29.92;                          //--- Alt Baro Hg Pilot)
float Alt_baro1 = 29.92;                         //--- Alt Baro Hg CoPilot)
int Alt_Radio = 300;                             //--- Radio Altitude set
int HSIbug = 180;                                //----HSI Bug position (0-360 grad)
int ADF1_Card;
int ADF1_st = 300;
int ADF1_act = 400;

//--------------------------------------------Audio panel ---------------------------------
byte Audio = 1;             // 8 Audio panel indicvators (Com1 initialy on)
int Spkr = 38;             // - Audio panel - Speaker/Headphone switch - pin #38
//----------------------------------
byte Lights[3];                            //--- 3 bytes (24 Indicators) output
byte Lights_mem[3];

//-------------------------------------------- DATA strings preset---------------------------------
byte Nul[] = {0, 0, 0, 0};
byte One[] = {0, 0, 128, 63};   
byte Noch[] = {0, 192, 121, 196};
//---------------------------------------
byte data110[] = { 110, 0, 0, 0 };    // fuel selectors
byte dataAxis[] = { 8,0,0,0 };
byte dataThrottle[] = { 25,0,0,0 };
byte dataTrim[] = { 13,0,0,0 };
byte dataProp[] = { 39, 0, 0, 0 };
byte dataMixture[] = { 29, 0, 0, 0 };
char dataAxis_end[] = "00000000000000000000";
char dataTrim_end[] = "0000000000000000";
//-----------------------------------------------------------------------------

char Ref_blank[470];            // ----- define empty 

int Var_temp;                   // ------- Temporary variables
byte Byte_temp;
float Fl_temp;
boolean bit_temp;

//----------------Unions allow the same location of memory to be accessed as different data types
   union {
        char bytevalue[4];                         
        float floatvalue;
        }  bf;
        
unsigned long time1;    // test for timing
unsigned long time2;        

//=================================  S E T U P  ===========================================
void setup()  { 
                                 // -- attach and initialize SERVO Gauges (Lower control Panel)----
FuelLGauge.attach(2);                              
FuelRGauge.attach(3); 
AltLGauge.attach(5);                              
AltRGauge.attach(6); 
BusGauge.attach(7); 
FuelLGauge.writeMicroseconds(1160);         
FuelRGauge.writeMicroseconds(1200);      
AltLGauge.writeMicroseconds(1200);     
AltRGauge.writeMicroseconds(1800);     
BusGauge.writeMicroseconds(1760);


//-----------------------------------   // Set 8 pins for 8-bit Input BUS --------------------------------- 
pinMode(22, INPUT_PULLUP);              
pinMode(23, INPUT_PULLUP);
pinMode(24, INPUT_PULLUP);
pinMode(25, INPUT_PULLUP);
pinMode(26, INPUT_PULLUP);
pinMode(27, INPUT_PULLUP);
pinMode(28, INPUT_PULLUP);
pinMode(29, INPUT_PULLUP);         
       //------------------------------- // Set 4 output pins for Address 16 ports
pinMode(33, OUTPUT);                  
pinMode(32, OUTPUT);
pinMode(31, OUTPUT);
pinMode(30, OUTPUT);
digitalWrite(33, LOW);
digitalWrite(32, LOW); 
digitalWrite(31, LOW); 
digitalWrite(30, LOW); 
       //---------------------------------// Set 2 output pins for serial output (24 Indicators)-----
pinMode(34, OUTPUT);
pinMode(35, OUTPUT);
       //---------------// Set 4 output pins for control Prop Heat Gauge and AUTO Surface De-Icingh Gauge
pinMode (8, OUTPUT);                      //-- (through DC motor driver IS)
pinMode (9, OUTPUT); 
pinMode (11, OUTPUT); 
pinMode (12, OUTPUT); 
digitalWrite (8, LOW); 
digitalWrite (9, LOW); 
digitalWrite (11, LOW); 
digitalWrite (12, LOW); 
       // Set  output pins for speaker/headset and Flood light
pinMode(Spkr, OUTPUT);  
pinMode(Cabin_light, OUTPUT);
//-----------------------------------------------------------------  


  Ethernet.begin(mac,ip); 
  Udp.begin(localPort); 

for (int i = 0; i < 470 ; i++) Ref_blank[i] = char(32);   

            bitWrite(Lights[2], 0, 1);    // Set Audio
            

 // Serial.begin(115200);            
              
   } 
 
//---------------------------------------
void loop() 
{
 //  while (FuelL == -1) goto simstart;
   
        time1 =  micros();  
   
  
//--------------- Axis ( 3.4 msec UDP packet) ------------
Udp.beginPacket(ipx, 49000);
Udp.write("DATA<"); 
for (int i = 0; i < 4; i++) Udp.write(dataAxis[i]); 

  Fl_temp = analogRead(Yoke_Pitch);
  Fl_temp = map(Fl_temp, 10, 1023, 1000, -1000);
      bf.floatvalue =  Fl_temp / 1000; 
for (int i = 0; i < 4; i++) Udp.write(bf.bytevalue[i]);  
  
  Fl_temp = analogRead(Yoke_Roll);
  if (Fl_temp < 671)  Fl_temp = map(Fl_temp, 120, 670, -1000, 0);
  if (Fl_temp > 670)    Fl_temp = map(Fl_temp, 671, 880, 1, 1000);  
    bf.floatvalue = Fl_temp / 1000;
for (int i = 0; i < 4; i++) Udp.write(bf.bytevalue[i]);       
                                      
  Fl_temp = analogRead(Rudder);
  Fl_temp = map(Fl_temp, 0, 1024, 900, -600);
      bf.floatvalue = Fl_temp / 1000;  
for (int i = 0; i < 4; i++) Udp.write(bf.bytevalue[i]);

Udp.write(dataAxis_end) ;                                                                                                                                                                                                                                               
Udp.endPacket();    
  
//------------------ Reading Bus (16 ports x 8bits) ----------------------
  
 //------------------- Port #1----------------     
digitalWrite(33, LOW); digitalWrite(32, LOW); digitalWrite(31, LOW); digitalWrite(30, LOW);

 for (int i = 0; i < 8; i++) {
      bit_temp = digitalRead(i+22);
  if (bit_temp ^ bitRead (Bus1_mem, i)) {
     switch (i) {
		case 0: if (bit_temp) comsend("sim/systems/avionics_on"); 
			else comsend("sim/systems/avionics_off");  break;     // Master Avionics
		case 1: if (bit_temp) comsend("sim/lights/nav_lights_on"); 
			else comsend("sim/lights/nav_lights_off");  break;          // NAV light 
		case 2: if (bit_temp) comsend("sim/lights/beacon_lights_on"); 
			else comsend("sim/lights/beacon_lights_off");  break;        // Beacon Light 
		case 3: if (bit_temp) comsend("sim/lights/strobe_lights_on"); 
			else comsend("sim/lights/strobe_lights_off");  break;     // Strobe Light 
		case 4: if (bit_temp) comsend("sim/lights/taxi_lights_on"); 
			else comsend("sim/lights/taxi_lights_off");  break;        // Taxi  light
		case 5: if (bit_temp) comsend("sim/electrical/battery_1_on"); 
			else comsend("sim/electrical/battery_1_off");  break;        // BATTERY  
		case 6: if (bit_temp) comsend("sim/electrical/generator_2_on"); 
			else comsend("sim/electrical/generator_2_off");  break;       // R Alt   
		case 7: if (bit_temp) comsend("sim/electrical/generator_1_on"); 
			else comsend("sim/electrical/generator_1_off");  break;       // L Alt       
                    }
                bitWrite (Bus1_mem, i, bit_temp);      
                                     }
                              }
 //----------------  Port #2----------------     

digitalWrite(33, HIGH);  
 for (int i = 0; i < 8; i++) {
      bit_temp = digitalRead(i+22);
  if (bit_temp ^ bitRead (Bus2_mem, i)) {
       switch (i) {
           case 0: if (bit_temp) comsend("sim/ice/inlet_heat0_on"); 
			else comsend("sim/ice/inlet_heat0_off");  break;      // Fuel Vent De-Icing   
		case 1: digitalWrite ((!bit_temp+8), HIGH);  delay(300); //------------ Prop Heat
                digitalWrite ((!bit_temp+8), LOW);  
             if (bit_temp) comsend("sim/ice/prop_heat0_on"); 
			else comsend("sim/ice/prop_heat0_off");            // Prop Heat switch               
            break;               
		case 2: if (bit_temp) comsend("sim/ice/window_heat_on"); 
			else comsend("sim/ice/window_heat_off");  break;         // WSHLD 
		case 3: if (bit_temp) comsend("sim/ice/pitot_heat0_on");  
			else comsend("sim/ice/pitot_heat0_off");  break;       // Pitot Heat
		case 4: if (bit_temp) comsend("sim/ice/AOA_heat0_on"); 
			else comsend("sim/ice/AOA_heat0_off");  break;     // Stall Wrn Heating  
		case 5: digitalWrite ((!bit_temp+11), HIGH);    delay (300);  //-  AUTO Surface De-Icingh Gauge
               digitalWrite ((!bit_temp+11), LOW); 
                if (bit_temp) comsend("sim/ice/wing_heat0_on");  
				else comsend("sim/ice/wing_heat0_off");   break;    // AUTO Surface De-Icing sw  
		case 6: if (bit_temp) comsend("sim/fuel/fuel_pump_1_on"); 
				else comsend("sim/fuel/fuel_pump_1_off");  break;     // FUEL Pump L  
		case 7: if (bit_temp) comsend("sim/fuel/fuel_pump_2_on"); 
				else comsend("sim/fuel/fuel_pump_2_off");  break;      // FUEL PUMP R   
                    }
                bitWrite (Bus2_mem, i, bit_temp);      
                                     }     }
//------------------------ Port #3----------------  
digitalWrite(33, LOW);
digitalWrite(32, HIGH);   
 for (int i = 0; i < 8; i++) {
      bit_temp = digitalRead(i+22);
  if (bit_temp ^ bitRead (Bus3_mem, i)) {
       switch (i) {
		case 0:   comsend("sim/flight_controls/landing_gear_down"); 
		    break;           // GEAR DN    

               case 1:   comsend("sim/flight_controls/landing_gear_up"); 
			      break;           // GEAR UP  
               case 2:   drefsend("sim/cockpit2/switches/landing_lights_switch[1]", bit_temp, 454);
				   break;       //--------------- Landing Lights Bus (L)  ----------
               case 3:   drefsend("sim/cockpit2/switches/landing_lights_switch[2]", bit_temp, 454); 
					  break;       //--------------- Landing Lights Bus (R)  ----------
               case 4: if (bit_temp) comsend("sim/systems/prop_sync_on"); 
						else comsend("sim/systems/prop_sync_off");  break;        // Prop Sync 
               case 5:   drefsend("sim/cockpit2/switches/instrument_brightness_ratio[1]", bit_temp, 448);   
                         drefsend("sim/cockpit2/switches/instrument_brightness_ratio[2]", bit_temp, 448);
			    break;     //-----------  Panel - Instrument Backlight Switch (DREF)--  
               case 6:  Cabin_light = bit_temp; 
 					break;     	 //----------- Panel FLOOD (external)     
               case 7:    comsend("sim/annunciator/test_all_annunciators"); 
						 break;                         //--------- Test Button          
                    }
                bitWrite (Bus3_mem, i, bit_temp);    bitWrite (Bus3_mem, 7, 0);     
                                     }
                              } 

//----------------------- Port #4------------  
digitalWrite(33, HIGH);     
      //---------------- Ignition rotary, Starters (L, R)---
        for (int i = 0; i < 8; i++)  bitWrite (Byte_temp, i, digitalRead(i+22)); 
        if (Byte_temp != Bus4_mem) 
        {  
         switch  (Byte_temp & 15){
					// - OFF position only after L position    
               case 0:  if  ((Byte_temp & 15) == 1)   comsend("sim/magnetos/magnetos_off_1");  
					break;     
               case 1:     comsend("sim/magnetos/magnetos_left_1");  break; 
               case 2:     comsend("sim/magnetos/magnetos_right_1"); break; 
               case 4:     comsend("sim/magnetos/magnetos_both_1");  break; 
               case 8:     comsend("sim/starters/engage_starter_1"); break; 
                      }
          switch  (Byte_temp & 240){
               case 0:   if  ((Byte_temp & 240) == 16)   comsend("sim/magnetos/magnetos_off_2");
				   break;            
               case 16:     comsend("sim/magnetos/magnetos_left_2");  break; 
               case 32:     comsend("sim/magnetos/magnetos_right_2"); break; 
               case 64:     comsend("sim/magnetos/magnetos_both_2");  break;  
               case 128:    comsend("sim/starters/engage_starter_2"); break;               
                          } 
          Bus4_mem = (Byte_temp & 119);   //----- for starter pulse generation bits 3 and 7 set to 0     
          } 


//------------------ Port #5---- ADF, Timer FT/ET + Flaps + AP-on/off -----  
digitalWrite(33, LOW);  digitalWrite(32, LOW);  digitalWrite(31, HIGH);

int adf0 [] = {1, 10}; 
int enc_b = bitRead(Bus11_mem, 0);

 for (int i = 0; i < 8; i++) {
      bit_temp = !digitalRead(i+22);
  if (bit_temp ^ bitRead (Bus5_mem, i)) {
       switch (i) {
       case 0: if (bit_temp) comsend("sim/autopilot/servos_and_flight_dir_on"); 
        break;                      //------AP ON/OFF  autopilot/flight director mode = 2
       case 1: if (bit_temp) drefsend("sim/cockpit2/controls/flap_ratio[0]", 1, 465);
    break;                     // Flaps DN  
      case 2: if (bit_temp) drefsend("sim/cockpit2/controls/flap_ratio[0]", 0.5, 465);
   break;                    // Flaps APR  
       case 3: if (bit_temp) drefsend("sim/cockpit2/controls/flap_ratio[0]", 0, 465);
      break;                   // Flaps  Up;
       case 4: if (bit_temp == !digitalRead(i+23))   ADF1_st +=adf0[enc_b]; 
      			 else ADF1_st -=adf0[enc_b];
                            if ( ADF1_st >= 999)  ADF1_st = 999;         
                            if ( ADF1_st < 0) ADF1_st = 0; 
           //---ADF stby freq
        drefsend("sim/cockpit2/radios/actuators/adf1_standby_frequency_hz[0]", ADF1_st, 442); 
			  break;    
       case 5:   break; 
   //----- Change ADF stdby Freq
       case 6: if (bit_temp) comsend("sim/radios/adf1_standy_flip");
       case 7: if (bit_temp) charsend (84);    //----- Timer FT/ET    (test)
                    }
                bitWrite (Bus5_mem, i, bit_temp);      
                                     }
                              }
     
 //--------------- Port #6, active "1" ---------- Transponder  code          

digitalWrite(33, HIGH); 
 for (int i = 0; i < 8; i++) {
      bit_temp = digitalRead(i+22);
  if ((bit_temp ^ bitRead (Bus6_mem, i) == 1) && (bit_temp == 1)) {
       switch (i) {
    case 0:     comsend("sim/transponder/transponder_thousands_down"); break;              
    case 1:     comsend("sim/transponder/transponder_thousands_up"); break;     
    case 2:    comsend("sim/transponder/transponder_hundreds_down"); break;  
    case 3:    comsend("sim/transponder/transponder_hundreds_up"); break;     
    case 4:      comsend("sim/transponder/transponder_tens_down"); break;    
    case 5:     comsend("sim/transponder/transponder_tens_up"); break;       
    case 6:     comsend("sim/transponder/transponder_ones_up"); break;     
    case 7:     comsend("sim/transponder/transponder_ones_down");break;         
                 }
                 }                
               bitWrite (Bus6_mem, i, bit_temp);  }

//-------------- Port #7---------------   
  digitalWrite(33, LOW);
  digitalWrite(32, HIGH);  



        for (int i = 0; i < 8; i++)  bitWrite (Byte_temp, i, digitalRead(i+22)); 
        
     if (Byte_temp != Bus7_mem)                                         
     {
          if (!bitRead(Byte_temp, 6) && bitRead(Bus7_mem, 6))  {    //---- Altimeter Right
               if (bitRead(Byte_temp, 6) == bitRead(Byte_temp, 7))   Alt_baro1 -=0.01;       
               else Alt_baro1 +=0.01;
               if ( Alt_baro1 >= 40)  Alt_baro1 = 40;         
               if ( Alt_baro1 < 20) Alt_baro1 = 20; 
   //---- Alt Baro Hg CoPilot                  
     drefsend("sim/cockpit2/gauges/actuators/barometer_setting_in_hg_copilot[0]", Alt_baro1, 436);
    
             
          }      
         //---------------------------- Transpoder  mode- 

           if ((Bus7_mem & 62) != (Byte_temp & 62)) { 
                 switch (Byte_temp & 62) {         
             case 0:    comsend("sim/transponder/transponder_off");  break;                        
             case 2:    comsend("sim/transponder/transponder_standby"); break; 
             case 4:    comsend("sim/transponder/transponder_alt"); break; 
             case 8:    comsend("sim/transponder/transponder_test"); break; 
             case 16:   comsend("sim/transponder/transponder_on");  break; 
             case 32:   comsend("sim/transponder/transponder_ground"); break;             
                     }}    
                  //------IDENT  ----"i" 
               if ( bitRead(Byte_temp, 0))   comsend("sim/transponder/transponder_ident");   
      
          Bus7_mem = Byte_temp;
      } 

//------------------------ Port #8---------------- GPS
  digitalWrite(33, HIGH);
 for (int i = 0; i < 8; i++) {
      bit_temp = digitalRead(i+22);
  if ((bit_temp ^ bitRead (Bus8_mem, i) == 1) && (bit_temp == 1))  {
       switch (i) {
    case 0:    comsend("sim/GPS/g430n1_ent");  break;      //------ENT -----             
    case 1:    comsend("sim/GPS/g430n1_clr");   break;      //------CLR ---
    case 2:    comsend("sim/GPS/g430n1_direct"); break;       //------Direct to D-> --
    case 3:    comsend("sim/GPS/g430n1_menu");  break;         //------MENU -----
    case 4:    comsend("sim/GPS/g430n1_zoom_in"); break;        // GPS Range +   
    case 5:    comsend("sim/GPS/g430n1_zoom_out"); break;     // GPS Range -       
    case 6:    comsend("sim/GPS/g430n1_obs");  break;         //------OBS---- 
    case 7:    comsend("sim/GPS/g430n1_cdi"); break;           //------CDI---
                 }
                 }                
               bitWrite (Bus8_mem, i, bit_temp);  }

//----------------- Port #9---------------- GPS
  digitalWrite(33, LOW);
  digitalWrite(32, LOW);
  digitalWrite(31, LOW);  
  digitalWrite(30, HIGH);  
        for (int i = 0; i < 8; i++)  bitWrite (Byte_temp, i, digitalRead(i+22)); 

        if (Byte_temp != Bus9_mem)   
        {
           if (!bitRead(Byte_temp, 1) && bitRead(Bus9_mem, 1))   {  // GPS page/chapter
           switch (bitRead(Bus9_mem, 4)) {   // - Encoder pressed
  case 0:{        
        if (bitRead(Byte_temp, 0) == bitRead(Byte_temp, 1)) comsend("sim/GPS/g430n1_chapter_up");    
         else  comsend("sim/GPS/g430n1_chapter_dn"); 
             break;  }
   case 1:{        
       if (bitRead(Byte_temp, 0) == bitRead(Byte_temp, 1)) comsend("sim/GPS/g430n1_page_up");    
        else  comsend("sim/GPS/g430n1_page_dn"); 
             break;  }            
           }
           }
       //--------------------
      if (!bitRead(Byte_temp, 2) && bitRead(Bus9_mem, 2))   {      //---- Com1 stby
          switch (bitRead(Bus9_mem, 4)) {
  case 0:{                   
       if (bitRead(Byte_temp, 2) == bitRead(Byte_temp, 3)) comsend("sim/radios/stby_com1_fine_up");          
        else  comsend("sim/radios/stby_com1_fine_down");       
          break;   } 
  case 1:{  
         if (bitRead(Byte_temp, 2) == bitRead(Byte_temp, 3)) comsend("sim/radios/stby_com1_coarse_up");          
         else  comsend("sim/radios/stby_com1_coarse_down");       
          break;   }     
            }
               }
                                  
                  if (!bitRead(Byte_temp, 5))  comsend("sim/GPS/g430n1_proc");  //------PROC
                  if (!bitRead(Byte_temp, 6))  comsend("sim/GPS/g430n1_msg"); //------MSG 
                  if (!bitRead(Byte_temp, 7))  comsend("sim/GPS/g430n1_fpl");  //------FPL    
          Bus9_mem = Byte_temp;
         
        }

//------------------- Port #10----------------  Audio Panel
  digitalWrite(33, HIGH);                                 
        for (int i = 0; i < 8; i++)  bitWrite (Byte_temp, i, digitalRead(i+22));

       if (Byte_temp != Bus10_mem)                                         
        {
            Audio = Audio ^ Byte_temp;
  switch (Byte_temp) {
                                                        
    case 1:{   // A_Com1
            bitWrite(Lights[2], 0, bitRead(Audio, 0));
             comsend("sim/audio_panel/select_audio_com1");  break;   }
    case 2:{  // A_Com2             
            bitWrite(Lights[1], 7, bitRead(Audio, 1));
             comsend("sim/audio_panel/select_audio_com2");  break;   }
    case 16:{  //A_Nav1
            bitWrite(Lights[1], 6, bitRead(Audio, 4));
             comsend("sim/audio_panel/select_audio_nav1");  break;   }
    case 64:{  //  A_Nav2
            bitWrite(Lights[2], 1, bitRead(Audio, 6)); 
             comsend("sim/audio_panel/select_audio_nav2");  break;   }        
    case 32:{  //  A_Adf 
            bitWrite(Lights[2], 4, bitRead(Audio, 5));
             comsend("sim/audio_panel/select_audio_adf1");  break;   }
    case 8:{ //  A_DME
            bitWrite(Lights[2], 2, bitRead(Audio, 3));
            drefsend("sim/cockpit2/radios/actuators/audio_dme_enabled[0]",  bitRead(Audio, 3), 450);             
         break;  }  
    case 128:{  // A_Mkr
            bitWrite(Lights[2], 3, bitRead(Audio, 7));
            drefsend("sim/cockpit2/radios/actuators/audio_marker_enabled[0]",  bitRead(Audio, 7), 447);                
        break;   }    
   case 4:{      // A_Spk
           digitalWrite(Spkr,  bitRead(Audio, 2));
         //   bitWrite(Lights[2], xx, bitRead(Audio, 2));                     
         break;  }
         }
          Bus10_mem = Byte_temp; 
     
        }

//-------------- Port #11---------------- COM/NAV buttons + Set/Reset ET
  digitalWrite(33, LOW);
  digitalWrite(32, HIGH);  
        for (int i = 0; i < 8; i++)  bitWrite (Byte_temp, i, digitalRead(i+22)); 

        if (Byte_temp != Bus11_mem)   
        {
                //-----( bitRead(XXXXX, 0) )---Encoders button  
   //-------------- -- Com1/Com1-stby flip
             if ( bitRead(Byte_temp, 1) ) comsend("sim/radios/com1_standy_flip");    
   //-------------- -- NAV1/NAV1-stby flip
             if ( bitRead(Byte_temp, 2) ) comsend("sim/radios/nav1_standy_flip"); 
   //-------------- -- Com2/Com2-stby flip      
             if ( bitRead(Byte_temp, 3) ) comsend("sim/radios/com2_standy_flip");  
   //-------------- -- NAV2/NAV2-stby flip
             if ( bitRead(Byte_temp, 4) ) comsend("sim/radios/nav2_standy_flip"); 
             
             if ( bitRead(Byte_temp, 5) ) {}    // OBS 1 -reserve
             if ( bitRead(Byte_temp, 6) ) {}   // OBS 2 -reserve
             if ( bitRead(Byte_temp, 7) ) {  
				comsend("sim/instruments/timer_reset");  
				 comsend("sim/instruments/timer_start_stop");  } // Set/Reset ET on ADF
                          
          Bus11_mem = Byte_temp; 
        }

//----------------------- Port #12-------------- Brakes &  Fuel selectors
  digitalWrite(33, HIGH);        
        for (int i = 0; i < 8; i++)  bitWrite (Byte_temp, i, digitalRead(i+22));

        if (Byte_temp != Bus12_mem)   
        {
 
          if (bitRead (Byte_temp, 0)) {
			drefsend("sim/cockpit2/controls/parking_brake_ratio[0]", 0, 456);  }   // Brakes off           
          if (bitRead (Byte_temp, 1)) {
			drefsend("sim/cockpit2/controls/parking_brake_ratio[0]", 1, 456); }   // Brakes  
          if (bitRead (Byte_temp, 0) && bitRead (Byte_temp, 1)) {
			drefsend("sim/cockpit2/controls/parking_brake_ratio[0]", 0.5, 456); }  //  Brakes 0.5 
      

//--------------- Fuel selector (L, R) (Separate engines feeding) --------

			// reserved

         Bus12_mem = Byte_temp;           
        }

//------------------- Port #13-------------- Autopilot Panel
  digitalWrite(33, LOW);
  digitalWrite(32, LOW);
  digitalWrite(31, HIGH);         
        for (int i = 0; i < 8; i++)  bitWrite (Byte_temp, i, digitalRead(i+22));  

        if (Byte_temp != Bus13_mem)  //  
         {
   	if ( bitRead(Byte_temp, 0) ) comsend("sim/autopilot/fdir_servos_up_one");    // DN/UP Fdir
    if ( bitRead(Byte_temp, 1) ) comsend("sim/autopilot/fdir_servos_down_one"); // DN/UP Fdir
    if ( bitRead(Byte_temp, 2) ) comsend("sim/autopilot/flight_dir_on_only");   // AP_FD = 2 ;    
		drefsend("sim/cockpit2/autopilot/flight_director_mode[0]", AP_FD, 454);
    if ( bitRead(Byte_temp, 3) ) comsend("sim/autopilot/heading");  // HDG  mode                           
    if ( bitRead(Byte_temp, 4) )   comsend("sim/autopilot/altitude_hold");   // ALT
    if ( bitRead(Byte_temp, 5) )   comsend("sim/autopilot/back_course");  // BC   
    if ( bitRead(Byte_temp, 6) )   comsend("sim/autopilot/approach");   // APPR
    if ( bitRead(Byte_temp, 7) )   comsend("sim/autopilot/NAV");     // NAV   
       
         Bus13_mem = Byte_temp;           
        }

// sim/autopilot/servos_and_flight_dir_on    - Autopilot servo button

//----------------------- Port #14---------------- 
  digitalWrite(33, HIGH);    
        for (int i = 0; i < 8; i++)  bitWrite (Byte_temp, i, digitalRead(i+22)); 

        if (Byte_temp != Bus14_mem)   
        {		//--------DME NAV1/NAV2/STBY
             if ((Bus14_mem & 7) != (Byte_temp & 7)) { 
                   switch (Byte_temp & 7) {
                    case 1:  {  drefsend("sim/cockpit2/radios/actuators/DME_slave_source[0]", 1, 451);
						  break; }        
                case 4: {   drefsend("sim/cockpit2/radios/actuators/DME_slave_source[0]", 0, 451);
 					   break; }
                case 2:  {  drefsend("sim/cockpit2/radios/actuators/DME_slave_source[0]", 2, 451);
				    break; }
                   }
     if ((Byte_temp & 7) == 0)  drefsend("sim/cockpit2/radios/actuators/dme_power[0]", 0, 458); 
     else drefsend("sim/cockpit2/radios/actuators/dme_power[0]", 1, 458);
                                               }
             
   if (!bitRead(Byte_temp, 3) && bitRead(Bus14_mem, 3))    //---- Alt BAro Hg Pilot
       { 
         if (bitRead(Byte_temp, 3) == bitRead(Byte_temp, 4))   Alt_baro +=0.01;       
         else Alt_baro -=0.01;
               if ( Alt_baro >= 40)  Alt_baro = 40;         
               if ( Alt_baro < 20) Alt_baro = 20; 
     drefsend("sim/cockpit2/gauges/actuators/barometer_setting_in_hg_pilot[0]", Alt_baro, 438);                
           } 

           if (!bitRead(Byte_temp, 5) && bitRead(Bus14_mem, 5)) //--Autopilot ALT pres
           { 
               if (bitRead(Byte_temp, 5) == bitRead(Byte_temp, 6))   Alt_select -=100;       
               else Alt_select +=100;
               if ( Alt_select >= 40000)  Alt_select = 40000;         
               if ( Alt_select < 100) Alt_select = 100; 
             drefsend("sim/cockpit2/autopilot/altitude_dial_ft[0]", Alt_select, 458);                
           }
            if (!bitRead(Byte_temp, 7) && bitRead(Bus14_mem, 7)) {
			comsend("sim/autopilot/altitude_arm");  }     // ---Autopilot ALT ARM           
         Bus14_mem = Byte_temp;              
        }

//----------------------- Port #15----------------  
  digitalWrite(33, LOW);
  digitalWrite(32, HIGH);  
  

 for (int i = 0; i < 8; i++) {
      bit_temp = digitalRead(i+22);
  if (bit_temp ^ bitRead (Bus15_mem, i)) {
       switch (i) {
               case 0:  if (bit_temp == !digitalRead(i+23))   comsend("sim/radios/obs2_up");  
									 break;               //-- NAV2 OBS Pilot    
               case 1:  if (bit_temp == !digitalRead(i+21))   comsend("sim/radios/obs2_down"); 
	    break;         
               case 2:  if (bit_temp == digitalRead(i+23))   ADF1_Card -=1;       
                         else ADF1_Card +=1;
                         if ( ADF1_Card >= 361)  ADF1_Card -=360;         
                         if ( ADF1_Card < 0) ADF1_Card +=360; 
     //-ADF1 Card Pilot
     drefsend("sim/cockpit2/radios/actuators/adf1_card_heading_deg_mag_pilot[0]", ADF1_Card, 436);  
			 break;      
               case 3:   break;     
               case 4: if (bit_temp == digitalRead(i+23))   Alt_Radio -=10;       
                           else Alt_Radio +=10;
                             if ( Alt_Radio >= 2600)  Alt_Radio = 2600;         
                             if ( Alt_Radio < 0) Alt_Radio = 0; 
      //--- Radio Alt DH bug                   
     drefsend("sim/cockpit2/gauges/actuators/radio_altimeter_bug_ft_pilot[0]", Alt_Radio, 439);   
                        break;    
               case 5:   break;    
               case 6:   if (bit_temp == !digitalRead(i+23))   comsend("sim/radios/obs1_up"); 
					break;                            //HSI OBS (NAV1 OBS)                                       
               case 7:   if (bit_temp == !digitalRead(i+21))   comsend("sim/radios/obs1_down");    
					break;                                                                 
                    }
                                     }
                        bitWrite (Bus15_mem, i, bit_temp);                   
                              }  
  
//--------------------- Port #16 (Com/Nav Freq)------------------
  digitalWrite(33, HIGH);     
        for (int i = 0; i < 8; i++)  bitWrite (Byte_temp, i, digitalRead(i+22)); 
        
      if (Byte_temp != Bus16_mem)   
  { 
       int enc_b = bitRead(Bus11_mem, 0);          
       int com0 [] = {100, 1}; 

           if (!bitRead(Byte_temp, 1) && bitRead(Bus16_mem, 1))     //---- Com1 stby
           { 
            if (bitRead(Byte_temp, 0) == bitRead(Byte_temp, 1))   Com1_st +=com0[enc_b];       
            else Com1_st -=com0[enc_b];

                     if ( Com1_st >= 13700)  Com1_st -=1900;         
                     if ( Com1_st < 11800) Com1_st +=1900; 
   drefsend("sim/cockpit2/radios/actuators/com1_standby_frequency_hz[0]", Com1_st, 442);                        
         } 
 //----------------------------------------
         if (!bitRead(Byte_temp, 3) && bitRead(Bus16_mem, 3))   
         {          
            if (bitRead(Byte_temp, 2) == bitRead(Byte_temp, 3))   Com2_st +=com0[enc_b];       
            else Com2_st -=com0[enc_b];           //---- Com2 stby 
                    if ( Com2_st >= 13700)  Com2_st -=1900;             
                    if ( Com2_st < 11800) Com2_st +=1900; 
       drefsend("sim/cockpit2/radios/actuators/com2_standby_frequency_hz[0]", Com2_st, 442); 
          } 
  //----------------------------------------   
         if (!bitRead(Byte_temp, 5) && bitRead(Bus16_mem, 5))     //---- Nav1 stby
         { 
           if (bitRead(Byte_temp, 4) == bitRead(Byte_temp, 5))   Nav1_st +=com0[enc_b];       
           else Nav1_st -=com0[enc_b];
                     if ( Nav1_st >= 11800)  Nav1_st -=1000;             
                     if ( Nav1_st < 10800) Nav1_st +=1000; 
     drefsend("sim/cockpit2/radios/actuators/nav1_standby_frequency_hz[0]", Nav1_st, 442);                      
         } 
 //----------------------------------------   

        if (!bitRead(Byte_temp, 7) && bitRead(Bus16_mem, 7))   //---- Nav2 stby
        { 
          if (bitRead(Byte_temp, 6) == bitRead(Byte_temp, 7))   Nav2_st +=com0[enc_b];       
           else Nav2_st -=com0[enc_b];
                     if ( Nav2_st >= 11800)  Nav2_st -=1000;            
                     if ( Nav2_st < 10800) Nav2_st +=1000; 
         drefsend("sim/cockpit2/radios/actuators/nav2_standby_frequency_hz[0]", Nav2_st, 442);  
         } 
    
       Bus16_mem = Byte_temp; 
     }

//------------------------------------------ End of Bus DATA ---------------

//  if (analogRead(ThrottleL) != ThrottleL_pre);
//  if (analogRead(ThrottleR) != ThrottleR_pre);
//  if (analogRead(PropL) != PropL_pre);
//  if (analogRead(PropR) != PropR_pre);
//  if (analogRead(MixtureL) != MixtureL_pre);
//  if (analogRead(MixtureR) != MixtureR_pre);
//  if (analogRead(Flaps) != Flaps_pre);

//====== RECEIVe   DATA ==================================================
  
simstart:
//---- UDP-Packet Reading ( 4.7 millisec all )------------------
                                     
  packetSize = Udp.parsePacket();   //  Checks for the presence of a UDP
  if (packetSize)                  //  UDP packet was received
  {
              Udp.read ( DATA_buf, packetSize ); // Reads UDP
       
//-------Indicators data-------------------------           
       for (int i = 5; i < packetSize; i += 36)
       {  
             switch (DATA_buf[i])
             {
           
                case 1:    {}      //----- Test Button timeout--------         

                case 13:      //------- Flaps Indicators  -------------------           
                       {
                          for (int n=0; n<4; n++) bf.bytevalue[n] = DATA_buf[i+20+n];  
                              Fl_temp = bf.floatvalue;
                          if (Fl_temp > 0.05 && Fl_temp < 1 && Fl_temp != 0.5)
                                  { 
                                  bitWrite(Lights[0], 7, 1);
                                  bitWrite(Lights[0], 5, 0);
                                  bitWrite(Lights[0], 6, 0);
                                  }
                            else 
                                 {
                                  bitWrite(Lights[0], 7, 0);
                                  if (Fl_temp > 0.38 && Fl_temp < 0.52) bitWrite(Lights[0], 6, 1);
                                  else bitWrite(Lights[0], 6, 0);
                                  if (Fl_temp > 0.9) bitWrite(Lights[0], 5, 1);
                                  else bitWrite(Lights[0], 5, 0);
                                  }              
                         break;
                            } 

                 case 14:       //---Brakes --    
                       {
                                 if (DATA_buf[i+11] == 0) bitWrite(Lights[1], 3, 0);                
                                 else bitWrite(Lights[1], 3, 1);
                          break;
                              }          

                 case 34:       //------ Starter Warning ----
                         {
                                for (int n=0; n<4; n++) bf.bytevalue[n] = DATA_buf[i+4+n];
                                Var_temp = bf.floatvalue;
                                for (int n=0; n<4; n++) bf.bytevalue[n] = DATA_buf[i+8+n];
             
                                if (Var_temp < 0.1 && bf.floatvalue < 0.1) bitWrite(Lights[1], 2, 1);
                                else bitWrite(Lights[1], 2, 0);
                           break;
                                }

                 case 52:       //---- Alt Bus Load and Alt LR Warning ---------
                        {
                                for (int n=0; n<4; n++) bf.bytevalue[n] = DATA_buf[i+4+n];
                                        AltL = map(bf.floatvalue, 0, 100, 800, 2150);    
       // -----------  Alt load indicators servo 1
              if (AltL < 2200 && AltL > 700)       AltLGauge.writeMicroseconds(AltL);                 
            
                                for (int n=0; n<4; n++) bf.bytevalue[n] = DATA_buf[i+8+n];
                                       AltR = map(bf.floatvalue, 0, 100, 2100, 850);
       // -----------  Alt load indicators servo 2
                 if (AltR < 2200 && AltR > 700)       AltRGauge.writeMicroseconds(AltR);                  
                           
                  if (AltL == 0) bitWrite(Lights[1], 0, 1);
                  else bitWrite(Lights[1], 0, 0);

                  if (AltR == 0) bitWrite(Lights[1], 1, 1);
                  else bitWrite(Lights[1], 1, 0);
                             break;
                                }
  //------------------------ Fuel Gauges  ------    
                 case 62:
                       {
                   for (int n=0; n<4; n++) bf.bytevalue[n] = DATA_buf[i+4+n];
                       FuelL = bf.floatvalue;
            
                    for (int n=0; n<4; n++) bf.bytevalue[n] = DATA_buf[i+8+n];
                       FuelR = bf.floatvalue;
              
                       Var_temp = map(FuelL, 0, 470, 1680, 575);
                       if (Var_temp < 1690 && Var_temp > 574) FuelLGauge.writeMicroseconds(Var_temp); 
                       Var_temp = map(FuelR, 0, 470, 1700, 700);
                   if (Var_temp < 1710 && Var_temp > 690) FuelRGauge.writeMicroseconds(Var_temp);              
                               break;
                                      }
                 case 67:      //-- Gears Indicators  -----
                        {
                     for (int n=0; n<4; n++) bf.bytevalue[n] = DATA_buf[i+4+n];  // UDP buffer
                        Gear1 = bf.floatvalue;
                
                       for (int n=0; n<4; n++) bf.bytevalue[n] = DATA_buf[i+8+n];
                        Gear2 = bf.floatvalue;

                        for (int n=0; n<4; n++) bf.bytevalue[n] = DATA_buf[i+12+n];
                         Gear3 = bf.floatvalue;

              if (Gear1 == 1) bitWrite(Lights[0], 3, 1);   // Write to output
              else bitWrite(Lights[0], 3, 0);  
                     if (Gear2 == 1) bitWrite(Lights[0], 2, 1);
                     else bitWrite(Lights[0], 2, 0); 
              if (Gear3 == 1) bitWrite(Lights[0], 1, 1);
              else bitWrite(Lights[0], 1, 0); 
                      if (Gear1 == Gear2 && Gear2 == Gear3) bitWrite(Lights[0], 0, 0);
                       else bitWrite(Lights[0], 0, 1);              
                            break;  
                                  }
  //---------------O M I -------
                case 105:              
                          {  if (DATA_buf[i+7] == 0) bitWrite(Lights[2], 7, 0);
                              else bitWrite(Lights[2], 7, 1);
                              
                              if (DATA_buf[i+11] == 0) bitWrite(Lights[2], 5, 0);
                              else bitWrite(Lights[2], 5, 1);
              
                              if (DATA_buf[i+15] == 0) bitWrite(Lights[2], 6, 0);
                              else bitWrite(Lights[2], 6, 1);
                              break;
                                }
                                //------ Autopilot solenoid ----------             
                 case 108:       
                          {
                            for (int n=0; n<4; n++) bf.bytevalue[n] = DATA_buf[i+8+n];
                            if (bf.floatvalue == 2) bitWrite(Lights[0], 4, 1);
                            else bitWrite(Lights[0], 4, 0);
                            break;      }     
                   
                case 114:      //----------- Pitot heat light ----           
                          {
                  if (DATA_buf[i+23] == 0) bitWrite(Lights[1], 4, 1);    
                   else bitWrite(Lights[1], 4, 0);
                            break;
                               }
                         
                case 124:       //--------- Bus Volts ----------
                          {
                            for (int n=0; n<4; n++) bf.bytevalue[n] = DATA_buf[i+12+n];
                  
                          // -----------  Bus Volts indicator servo
                              Var_temp = map(bf.floatvalue, 0, 30, 750, 2050);
                if (Var_temp < 2200 && Var_temp > 700) BusGauge.writeMicroseconds(Var_temp);             
                              break;
                                  }
                case 127:      //----- Gears warning ----------
                          {
                            if (DATA_buf[i+27] == 0) bitWrite(Lights[1], 5, 0);
                            else bitWrite(Lights[1], 5, 1);
                            break;
                              }
            } 
        }
   }            // END   
       

//---------24 LED Output -----------        

 if (Lights[0] != Lights_mem[0] || Lights[1] != Lights_mem[1] || Lights[2] != Lights_mem[2])
  
        {
          for (int j = 2; j >= 0; j--)
          {
            for (int i = 7; i >= 0; i--)
            {
              digitalWrite(34, bitRead(Lights[j], i));
              digitalWrite(35, LOW);
              digitalWrite(35, HIGH);
            }
          }
          Lights_mem[0] = Lights[0];
          Lights_mem[1] = Lights[1];
          Lights_mem[2] = Lights[2];

        }
 time2 =  micros() - time1;  
   Serial.println(time2);       // 5500 max microseconds full cycle time
 
 }   //------------------

 void drefsend (char DataRef[], float Dvalue, int dr_end) 
{   

    
}   
 void charsend (int Data_ch)
                {
     
                }   
                
   void comsend (char Data_com[])
                {
  

                }  
               
                
    
//-----------------------------------------------

/*
        if (!bitRead(Bus1X, X) && bitRead(Bus1X_mem, X))                                     
        {      
         if (bitRead(Bus15, 6) == bitRead(Bus15, 7))   HSIbug += 1;       
          else HSIbug -= 1;

              if ( HSIbug >= 361)  HSIbug -=360;         
              if ( HSIbug < 0) HSIbug +=360; 
             drefsend("sim/cockpit2/autopilot/heading_dial_deg_mag_pilot[0]", HSIbug, 448);                
        }   
*/  
   
    








© Copyright 2012-2016 - SimVim