Cockpitbuilders.com

sign up .

January 25, 2020, 12:38:03 am

Login with username, password and session length
26 Guests, 0 Users
Members
Stats
  • Total Posts: 53801
  • Total Topics: 7200
  • Online Today: 43
  • Online Ever: 582
  • (January 22, 2020, 08:44:01 am)
Users Online
Users: 0
Guests: 26
Total: 26

COUNTDOWN TO WF2020


WORLDFLIGHT TEAM USA

Will Depart in...

Recent

Teensy LC Fuel Panel B (low part)

Started by kurt-olsson, July 24, 2019, 01:27:30 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

kurt-olsson

Made a function for the switches.
This is borderline, it will not be easy to understand when returning to the code. Sometimes it is better not to abstract but in this case it is so much more compact and easy to understand anyway.


const int PIN_KORRY_LIGHT_PUSH_TO_TEST = 17;
const int PIN_KORRY_LIGHT_PUSH_TO_TEST_2 = 10;
const int PIN_KORRY_LIGHT_RIGHT_AFT_LOW_PREASSURE = 9;
const int PIN_KORRY_LIGHT_LEFT_FWD_LOW_PREASSURE = 4;
const int PIN_KORRY_LIGHT_RIGHT_FWD_LOW_PREASSURE = 20;
const int PIN_KORRY_LIGHT_LEFT_FUEL_PUMP_LOW_PREASSURE = 16;
const int PIN_KORRY_LIGHT_LEFT_AFT_LOW_PREASSURE = 3;
const int PIN_KORRY_LIGHT_RIGHT_FUEL_PUMP_LOW_PREASSURE = 6;

const int PIN_SWITCH_FUEL_PUMP_RIGHT = 13; 
const int PIN_SWITCH_FUEL_PUMP_LEFT = 14; 
const int PIN_SWITCH_LEFT_AFT = 15; 
const int PIN_SWITCH_LEFT_FWD = 18; 
const int PIN_SWITCH_RIGHT_AFT = 19; 
const int PIN_SWITCH_RIGHT_FWD = 21; 


FlightSimFloat xPlane_low_fuel_press_l1;
FlightSimFloat xPlane_low_fuel_press_l2;
FlightSimFloat xPlane_low_fuel_press_r1;
FlightSimFloat xPlane_low_fuel_press_r2;
FlightSimFloat xPlane_low_fuel_press_c1;
FlightSimFloat xPlane_low_fuel_press_c2;

FlightSimFloat xPlane_fuel_tank_pos_lft1;
FlightSimFloat xPlane_fuel_tank_pos_lft2;
FlightSimFloat xPlane_fuel_tank_pos_rgt1;
FlightSimFloat xPlane_fuel_tank_pos_rgt2;
FlightSimFloat xPlane_fuel_tank_pos_ctr1;
FlightSimFloat xPlane_fuel_tank_pos_ctr2;

void setup() {
 
  xPlane_low_fuel_press_l1 = XPlaneRef("laminar/B738/annunciator/low_fuel_press_l1");
  xPlane_low_fuel_press_l2 = XPlaneRef("laminar/B738/annunciator/low_fuel_press_l2");
  xPlane_low_fuel_press_r1 = XPlaneRef("laminar/B738/annunciator/low_fuel_press_r1");
  xPlane_low_fuel_press_r2 = XPlaneRef("laminar/B738/annunciator/low_fuel_press_r2");
  xPlane_low_fuel_press_c1 = XPlaneRef("laminar/B738/annunciator/low_fuel_press_c1");
  xPlane_low_fuel_press_c2 = XPlaneRef("laminar/B738/annunciator/low_fuel_press_c2");

  xPlane_fuel_tank_pos_lft1 = XPlaneRef("laminar/B738/fuel/fuel_tank_pos_lft1");
  xPlane_fuel_tank_pos_lft2 = XPlaneRef("laminar/B738/fuel/fuel_tank_pos_lft2");
  xPlane_fuel_tank_pos_rgt1 = XPlaneRef("laminar/B738/fuel/fuel_tank_pos_rgt1");
  xPlane_fuel_tank_pos_rgt2 = XPlaneRef("laminar/B738/fuel/fuel_tank_pos_rgt2");
  xPlane_fuel_tank_pos_ctr1 = XPlaneRef("laminar/B738/fuel/fuel_tank_pos_ctr1");
  xPlane_fuel_tank_pos_ctr2 = XPlaneRef("laminar/B738/fuel/fuel_tank_pos_ctr2");
 
  pinMode(PIN_KORRY_LIGHT_PUSH_TO_TEST,OUTPUT);
  pinMode(PIN_KORRY_LIGHT_PUSH_TO_TEST_2,OUTPUT);
  pinMode(PIN_KORRY_LIGHT_RIGHT_AFT_LOW_PREASSURE,OUTPUT);   
  pinMode(PIN_KORRY_LIGHT_LEFT_FWD_LOW_PREASSURE,OUTPUT);
  pinMode(PIN_KORRY_LIGHT_RIGHT_FWD_LOW_PREASSURE,OUTPUT);
  pinMode(PIN_KORRY_LIGHT_LEFT_FUEL_PUMP_LOW_PREASSURE,OUTPUT);
  pinMode(PIN_KORRY_LIGHT_RIGHT_FUEL_PUMP_LOW_PREASSURE,OUTPUT);
  pinMode(PIN_KORRY_LIGHT_LEFT_AFT_LOW_PREASSURE,OUTPUT);

  pinMode(PIN_SWITCH_FUEL_PUMP_RIGHT, INPUT_PULLUP);
  pinMode(PIN_SWITCH_FUEL_PUMP_LEFT, INPUT_PULLUP);
  pinMode(PIN_SWITCH_LEFT_AFT, INPUT_PULLUP);
  pinMode(PIN_SWITCH_LEFT_FWD, INPUT_PULLUP);
  pinMode(PIN_SWITCH_RIGHT_AFT, INPUT_PULLUP);
  pinMode(PIN_SWITCH_RIGHT_FWD, INPUT_PULLUP);

}

void loop() {
  FlightSim.update(); 
 
  analogWrite(PIN_KORRY_LIGHT_PUSH_TO_TEST,255);
  analogWrite(PIN_KORRY_LIGHT_PUSH_TO_TEST_2,255);
  analogWrite(PIN_KORRY_LIGHT_LEFT_FUEL_PUMP_LOW_PREASSURE,mapfloat(xPlane_low_fuel_press_c1, 0,1,0,255));
  analogWrite(PIN_KORRY_LIGHT_RIGHT_FUEL_PUMP_LOW_PREASSURE, mapfloat(xPlane_low_fuel_press_c2, 0,1,0,255));
  analogWrite(PIN_KORRY_LIGHT_RIGHT_AFT_LOW_PREASSURE, mapfloat(xPlane_low_fuel_press_r1, 0,1,0,255));
  analogWrite(PIN_KORRY_LIGHT_RIGHT_FWD_LOW_PREASSURE,mapfloat(xPlane_low_fuel_press_r2, 0,1,0,255));
  analogWrite(PIN_KORRY_LIGHT_LEFT_FWD_LOW_PREASSURE,mapfloat(xPlane_low_fuel_press_l2, 0,1,0,255));
  analogWrite(PIN_KORRY_LIGHT_LEFT_AFT_LOW_PREASSURE, mapfloat(xPlane_low_fuel_press_l1, 0,1,0,255));

  readSwitchHigh(PIN_SWITCH_FUEL_PUMP_RIGHT, 0,1, xPlane_fuel_tank_pos_ctr2);
  readSwitchHigh(PIN_SWITCH_FUEL_PUMP_LEFT, 0,1, xPlane_fuel_tank_pos_ctr1);
  readSwitchHigh(PIN_SWITCH_LEFT_AFT, 1,0, xPlane_fuel_tank_pos_lft1);
  readSwitchHigh(PIN_SWITCH_LEFT_FWD, 1,0, xPlane_fuel_tank_pos_lft2);
  readSwitchHigh(PIN_SWITCH_RIGHT_AFT, 1,0, xPlane_fuel_tank_pos_rgt1);
  readSwitchHigh(PIN_SWITCH_RIGHT_FWD, 1,0, xPlane_fuel_tank_pos_rgt2);

  //put a small delay here otherwise the processing is so fast. Lots of read/write that dont have to be so fast.
  delay(20);
}

void readSwitchHigh(int pin, int highValue, int lowValue, FlightSimFloat &dataref) {
  if (digitalRead(pin) == HIGH && dataref != highValue) {
    dataref = highValue;
  }
  if (digitalRead(pin) == LOW && dataref != lowValue) {
    dataref = lowValue;
  }
}

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Like the Website ?
Support Cockpitbuilders.com and Click Below to Donate