Cockpitbuilders.com

Arduino Projects => Sketches - => Topic started by: RayS on April 05, 2015, 09:13:43 PM

Title: Ray's Fuel and Flap Code using ARDREF plugin...
Post by: RayS on April 05, 2015, 09:13:43 PM
Code!

You'll need a few plug-ins, like ARDREF for X-Plane (If you are using X-Plane... if you're using FSX or some other derivative, this might not be of much use.

So without delay, here's my Arduino code to get L/R fuel tank levels and flap position for setting my 3-section d'Arsenval Beechcraft flap gauge...

#include <SoftwareSerial.h>
#include <SPI.h>         
#include <Ethernet.h>
#include <Wire.h>
#include <XP_ARDref.h>         // -- include XPlane ARDref library
#include <Servo.h>

//#define txPin 2
#define fuel_L 4
#define fuel_R 5

#define txPWM 3
#define txPWM_1 5
#define txPWM_2 6
#define txPWM_3 7

//====================================================
byte Ard_MAC[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // MAC-Address of this  ethernet shield
int ARP = 5080;               // ARDref port
int IP[] = {10,0,0,49};   // This board IP address (0,0,0,0 -  if auto) .
                                        // If you have router with DHCP - you may set auto (0, 0, 0, 0)

//====================================================
ARDref XP (ARP, IP, Ard_MAC);   //  Create  XPlaneData object (XP)
//====================================================

// Vars to hold X-Plane data
  int Flaps;      // Flaps, current
  float Fuel_1;      // Flaps, current
  float Fuel_2;      // Flaps, current

// Vars for Flap Gauge
  int _Loop = 0;
  int Loop_1 = 0;
  int Loop_2 = 0;
  int Loop_3 = 0;


void setup()  {
 
  XP.begin(1);  // init, use Number of this Arduino ( always 1 )
         // if you use more than one Arduino with Ethernet shield, use 2, 3...)
//-------------------------------------   
  //XP.Pins_In (2, 12); 
  //XP.Pins_In (14, 29);     // Assign pins for inputs (first#, last# including)
  //XP.Pins_In (33, 49); 
  //XP.Pins_Out (13, 13);
  //XP.Pins_Out (30, 32);     // Assign pins for Outputs (first#, last# including)

}

//=================================================
void loop() { 
 
  XP.GetData();   //this function must be in each loop , for X-Plane data reading

  // Read X-Plane data from ARDREF
  Fuel_1 = XP.DataOut[1]; // Get value of parameter (#1 from your "out_1.cfg" list)
  Fuel_2 = XP.DataOut[2]; // Get value of parameter (#2 from your "out_1.cfg" list)
  Flaps =  XP.DataOut[3] * 100.0; // Get value of parameter (#3 from your "out_1.cfg" list)

  //_seconds = XP.DataOut[4] *100.0;

  Serial.println(Fuel_1); //Just so we can see what's going on

  analogWrite(3, (Fuel_1 / 3) + 5); // Simple stuff once you scale the output
  analogWrite(4, (Fuel_2 / 3) + 9); // for the desired d'arsenvol movements

   _Loop = (Flaps * 2.20) + 14 ;

  //--------------------------------
  // Flap Range: 0 - 35
  //--------------------------------
  if (_Loop >=0 && _Loop <= 85)
  {
    Loop_1 = (_Loop * 3)  ;
    analogWrite( txPWM_1, 0);     // Hold PWM_1 LOW
    digitalWrite(txPWM_2, HIGH);  // Hold PWM_2 HI
    analogWrite(txPWM_3, Loop_1); // PWM
   }
   
   
  //--------------------------------
  // Flap Range: 35-70
  //--------------------------------
  if (_Loop >=86 && _Loop <= 170)
  {
    Loop_2 = ((_Loop * 3) * -1);
    analogWrite( txPWM_1, 0);      // LOW
    analogWrite(txPWM_2, Loop_2);  // PWM
    digitalWrite(txPWM_3, HIGH);   // HIGH
   }


  //--------------------------------
  //  Flap Range: 170 - FULL DOWN
  //--------------------------------
  if (_Loop >=171 && _Loop <= 239)
  {
    Loop_3= (_Loop * 3)+2; 
    analogWrite( txPWM_1, Loop_3);  // PWM
    analogWrite(txPWM_2,  0);       // LOW
    digitalWrite(txPWM_3, HIGH);    // HIGH
   }

// Reset Each Individual Loop as they track through full range
if(Loop_1 >=254) {Loop_1 = 14; }
if(Loop_2 >=254) {Loop_2 = 0; }
if(Loop_3 >=240) {Loop_3 = 243; }

}   //-- End-----------------






Title: Re: Ray's Fuel and Flap Code using ARDREF plugin...
Post by: Trevor Hale on April 06, 2015, 02:49:36 AM
Great Code Ray,  thanks for sharing.

Trev
Title: Re: Ray's Fuel and Flap Code using ARDREF plugin...
Post by: VladSim on July 17, 2015, 07:27:29 AM
Hi Ray,
With new ArdSim library and plugin the code for analog Flaps and fuel gauges looks very simple.
This example is included in ArdSim package.
It is for linear scaled gauges, but can be easy transformed for non-linear scales:

/*
Analog gauge outputs
Download ArdSim Library on the site.
http://arduino.svglobe.com (http://arduino.svglobe.com)
*/
#include <SPI.h>         
#include <Ethernet.h>
#include <ARDsim.h>         // -- include ARDsim library
#include <Servo.h>
//====================================================
byte MAC[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};// MAC-Address of Arduino
unsigned int XP = 5080;           // port to receive Data from Xplane
byte IP[] = {192, 168, 1, 9};     //IP of this Arduino
//====================================================

#define Flaps     5, 0, 1            // Flaps gauge on pin #5
#define Fuel1     6, 10, 300     // -- fuel 1 (10-300 kgs)  - pin #6
#define Fuel2     7, 10, 300    // -- fuel 2 (10-300 kgs)   - pin #7

void setup()  {

  SimStart (XP, MAC, IP);    //-- ArdSim initialization

}
//----------------------------------------------------------
void loop() {

   SimScan(); //  this function must be in each loop
   
Gauge(6, Flaps);     // flaps controlled with dataref #6
Gauge(9, Fuel1);     // dataref #9 (in the in_1.cfg file)
Gauge(10, Fuel2);    // dataref #10

}   //-- End------------------------------------------------


Best regards
Title: Re: Ray's Fuel and Flap Code using ARDREF plugin...
Post by: RayS on July 17, 2015, 01:03:24 PM
Thanks for keeping us up to date with your great library!

I've been working this past week to get your new ARDSim code installed and it works beautifully. My Flap gauge is a 3-core mechanism so I'll see what I can do with that this weekend.

Thanks for all your support!