HOME Blog SimVimCockpit SimVimPanel Baron-58 sim ArdSimX About/Donate

Get dataref values for output

ArdSim library includes functions that let you easily configure your output devices for a specific dataref value output. You can link any dataref value with a specific LED, servo, analog gauge, simple stepper (later more devices will be supported). In these cases you don't need to read dataref values in your code to control such output devices.

Otherwise, if you need to get a dataref value for use in your specific program code or send it to some output device not included in the library, you can use the library functions "GetData()" together with "NewData()" with the dataref number from the out_#.cfg output list as argument.


GetData ( N );

With GetData function you can read the current (last) dataref value, no matter if it was changed since the last loop or not.


NewData ( N );

With NewData function you can check if the dataref value was changed since the last program loop before reading value with the "GetData" function. It is very preferable to use this function to prevent excessive reading of the same data and sending it to output devices.




Sample:
if ( NewData (30) ) { 
  
  Nav1st = GetData ( 30 );
  
      // your code here ( e.g. write the dataref #30 value to your output device)
      }

 // Check if the dataref #30 value has been changed since last reading and if it was, read this value and execute your program code


Pin mode - OutputPin()
In ArdSim since v2.0, all free digital Arduino pins (that are not used in key-matrix, encoder port or ArdSim library output functions) are automatcally assigned as Direct Digital Inputs for buttons/switches.

So, you can not use these pins for your output devices that are not included in ArdSim library, for example if you want to use an additional specific library for 7-segment indicators you can't use pins that have been asigned using this library functions.

When you need to use another output device besides those included in the library, you should use the function "OutputPin() in setup to reserve the pins that are used for this device. Otherwise, these pins will be set as input and your output device will be not work properly.

//----
void setup()  {
  
 	 ArdSimStart;

  OutputPin ( 6 );           // set output mode for the pin #6
  OutputPin ( 22, 29 );      // set output mode for the pin range #22...29
 }
     

NOTE: this function is NOT needed if the InputPin() function was previously used in setup.



Example - Using ArdSim library for LCD output

To control LCD (for example 16-char x2-line LCD screen 1602) you should use external "LiquidCrystal" or "LiquidCrystal_I2C" library.
See more details here: LCD displays with Arduino

Get data using ArdSim Plugin and config file with list of datarefs to be sent.
Use the library function GetData(D) with one argument "D"- number of the dataref in the config file and send received data to your LCD display.

Assign variable "float Nav1_st;" and use it to receive Nav1 frequency and output it on LCD screen. (the dataref #7 in the output config file is used):

30 0 sim/cockpit2/radios/actuators/nav1_standby_frequency_hz

part of the code for this example:

//--------------------------
loop() { 

ArdSimScan;

	if (NewData(7)) { 
  
 	 Nav1_stby = GetData (7);            //  number of dataref (7) in the out_x.cfg file
	 lcd.print ( Nav1_stby/100 );		//    write to lcd ( the float "Nav1_stby" is devided to 100 to display dot )    
  
}

Practical example - I2C LCD display for Autopilot annunciator display

External "LiquidCrystal_I2C" library is used in this example for I2C LCD display as described here: "LCD displays with ArdSim"

This is a sample sketch for I2C wired 16x2 LCD for displaying Autopilot modes


#include <LiquidCrystal_I2C.h>         (download library it here):
//================================ 
#include <SPI.h> 
#include <Ethernet.h>         // -- comment or delete these two lines if USB is used   
//================================ 

#include <ArdSim_Interface.h>         //  -- ArdSim library   ver. > 4.8
//================================ 

LiquidCrystal_I2C lcd(0x27,16,2);  // i2c address 0x27

//------------------------------------------------------------------
void setup() { 

	BoardNumber 1;   

	lcd.init();                      
	lcd.backlight();
  
} 
//-------------------------------------------------------------------
void loop()  { 

	 ArdSimScan; 

for (int i=1; i<9; i++) { 
 	   if (NewData(i)) {  int val=GetData(i);
        
switch (i) {
  
  case 1: lcd.setCursor(0, 0);  if (val > 0)  lcd.print("FD");   else lcd.print("  ");
          lcd.setCursor(14, 0); if (val == 2) lcd.print("AP");   else lcd.print("  ");   break;
  case 2: lcd.setCursor(0, 1);  if (val == 2) lcd.print("HDG");  else lcd.print("   ");  break;         
  case 3: lcd.setCursor(0, 1);  if (val > 0)  lcd.print("BC");   else lcd.print("  ");   break;    
  case 4: lcd.setCursor(4, 1);  if (val > 0)  lcd.print("APPR"); else lcd.print("    "); break;  
  case 5: lcd.setCursor(4, 0);  if (val > 0)  lcd.print("NAV");  else lcd.print("   ");  break;
  case 6: lcd.setCursor(9, 0);  if (val == 2) lcd.print("ALT");  else lcd.print("   ");  break;
  case 7: lcd.setCursor(9, 0);  if (val == 2) lcd.print("V/S");  else lcd.print("   ");  break; 
  case 8: lcd.setCursor(10, 1); if (val == 1) lcd.print("YAW");  else lcd.print("   ");  break;
         }
  } }

}
//=====================================================================

    /*  Lines in the "out_1.cfg"  file:

1 0 sim/cockpit2/autopilot/flight_director_mode
2 0 sim/cockpit2/autopilot/heading_status
3 0 sim/cockpit2/autopilot/backcourse_status
4 0 sim/cockpit2/autopilot/approach_status
5 0 sim/cockpit2/autopilot/nav_status
6 0 sim/cockpit2/autopilot/altitude_hold_status
7 0 sim/cockpit2/autopilot/vvi_status
8 0 sim/cockpit2/switches/yaw_damper_on    
     */

*/ //-------------------------------------


Nubmer of Output parameters

In ArdSim library the number of output parameters is limited by 50. (If you try to use >50 datarefs you will get unpredictable Arduino behaviour). This is done to reduce memory usage, which can be critical for such boards as UNO or Mini with custom program code and additional libraries. This value was chosen empirically and it is more or less enough and universal.

But, you can use as much datarefs for output as you need, this number 50 is limited only for one single board, and you can easily add more Arduino boards and distribute the data between them (up to 50 for each). Use output config file for each additional board with its own list of dataref.

Also, you can use separate Arduino boards for input and output.

Custom data input



The functions described on this page are related to ArdSim library ONLY.

The latest ArdSimX Interface doesn't require any custom I/O functions.


ArdSim plugin transmits any command or dataref value assigned in the config file to X-Plane in response to activity of the input device (button , switch, encoder, analog input change).

But sometimes you would need to send command or dataref value based on the result of your custom code, not in response to input device action.

Using the function "SimInput" you can send the ID number for a specific command/dataref from your code to X-Plane.


SimInput ( ID );

In this function the argument "ID" is just the number of your custom ID that should be represented in config file as prefix "C" with this number (i.e. C3) .



Custom ID in the input config file (in_#.cfg):

C3 sim/operation/fix_all_systems


Arduino code example:


 ArdSimScan; 

// Send commans/datarefs to X-Plane from your custom code 
   
      {  // your custom code here

         // you can check some code conditions and send  command C3:
  
          SimInput(3);   
	                                    
      }
} 
In response to you custom code result the plugin will send the command "Fix all failed systems" assigned under ID "C3".

Using datarefs for custom input configuration

If you need to change the value of a specific dataref directly, you should specify the dataref string and its value in the config file the same way as for simple button, but using the "C" prefix without "+/-" signs. On the program action the dataref will be set (changed) to this value or incremented:

If your chosen dataref is an array (such as engine data for a multi-engine aircraft), you can also add the array index number after the value (starting with 1). Otherwise, the default array number will be 1.
C1   sim/...../...../DATAREF_1    15  		// set the value=15 for DATAREF_1 
C2   sim/...../...../DATAREF_2    1+ 		// increment value for DATAREF_2  by 1
C3   sim/...../...../DATAREF_3    1    2 	// set the value=1 for DATAREF_3 (unit #2) 


Counter Mode Button in ArdSim v > 4.5

This is a button that cyclically switches between 2...8 modes for some custom actions. The button can be connected to any free Arduino pin (not a matrix input). Place "CountButton" function anywhere in your main loop code for the pin used as counter mode button.

Using the function "CountButton" you can cycle modes when the button is clicked. With this function you can read the current mode for this button ( check if it has changed since the last cycle to mode number you assign).


CountButton ( IN, M, C );


IN - Arduino Pin number used mfor this button
M - number of modes ( 2...8 ) - on every click you switch the modes: 1-2-1-2...
C - mode number to check

Since ver 4.9 you can use as many counter buttons as you need ( earlier there was only one).

Also any button input configured for sending commands to the simulator can also be used as counter button independantly (that means you can send simulator commands and read the count at the same time).



Arduino code example:


   // check if the button current mode is "2" (from 4), button connected to  pin #5 :

	if (CountButton (5, 4, 2))   {   } ;              //if mode = 2 - some action 


Using Counter Button with Custom Data Input
This button function can be easily combined with " Custom data input " function. On every click of this button plugin will send next command associated with button mode number.

In this example you need to assign four custom commands (with IDs C5, C6, C7, C8 ) and connect the button to the pin #12.


	if (CountButton (5, 4, 1))     SimInput (5);               // if mode = 1 action C5 - forward view with nothing
	else if (CountButton (5, 4, 2))     SimInput (6);               // if mode = 2 action C6 - forward view with HUD
	else if (CountButton (5, 4, 3))     SimInput (7);                // if mode = 3 action C7 - view from tower
	else if (CountButton (5, 4, 4))     SimInput (8);                // if mode = 4 action C8 - night vision view
		

Custom IDs in the input config file (in_#.cfg):

C5 sim/view/forward_with_nothing
C6 sim/view/forward_with_hud
C7 sim/view/tower
C8 sim/view/night_vision




Counter Mode Button in ArdSim v3.5...4.4

This is a button that cyclically switches between 2-4 modes for some custom actions. The button can be connected either directly to any free Arduino pin or to a matrix input. Place "CountButton" function anywhere in your main loop code for the pin used as counter mode button.

Using the function "CountButton" you can cycle modes when the button is clicked. The function returns the current mode number if the mode has changed since the last cycle, or 0 if the mode hasn't changed. The returned mode number can be used in your custom code as a condition for different actions.


CountButton ( IN, M, MX );


IN - Arduino Pin number or matrix node number
M - number of modes ( minimum 2 ) - on every click you switch the modes: 1-2-1-2...
"MX" - optional keyword for matrix button



Arduino code example:


   //assign variable "cmode=1" 

 int c = CountButton (5, 3); 
 
	if (c !=0 ) cmode = c;

	if (cmode == 1)   {   } ;              //if mode = 1 action 
	if (cmode == 2)   {   } ;              //if mode = 2 action 
	if (cmode == 3)   {   } ;              //if mode = 3 action 


Using Counter Button with Custom Data Input
This button function can be easily combined with " Custom data input " function. On every click of this button plugin will send next command associated with button mode number.

In this example you need to assign four custom commands (with IDs C5, C6, C7, C8 ) and connect the button to the pin #12.


   //assign variable "cmode=1" 

 int c = CountButton (12, 4); 
 
	if (c !=0 )  
	
	 {  cmode = c;

	if (cmode == 1)     SimInput (5);                // if mode = 1 action C5 - forward view with nothing
	if (cmode == 2)     SimInput (6);               // if mode = 2 action C6 - forward view with HUD
	if (cmode == 3)     SimInput (7);                // if mode = 3 action C7 - view from tower
	if (cmode == 4)     SimInput (8);                // if mode = 3 action C8 - night vision view
		}

Custom IDs in the input config file (in_#.cfg):

C5 sim/view/forward_with_nothing
C6 sim/view/forward_with_hud
C7 sim/view/tower
C8 sim/view/night_vision











© Copyright 2012-2016 - SimVim Design