Cockpitbuilders.com

Arduino Projects => Sketches - => Topic started by: kurt-olsson on October 05, 2018, 12:09:01 PM

Title: Teensy LC Anti Ice Panel Sketch Code
Post by: kurt-olsson on October 05, 2018, 12:09:01 PM
Hi.

This is the final code for my OEM Anti ice Panel used with Zibo 737 and X-Plane with a Teensy LC.

const int PIN_5V_BACKLIGHT = 22;
const int PIN_KORRY_PUSH_TO_TEST = 10;

const int PIN_KORRY_LIGHT_COWL_ANTI_ICE_1 = 3;
const int PIN_KORRY_LIGHT_COWL_ANTI_ICE_2 = 17;
const int PIN_KORRY_LIGHT_COWL_VALVE_OPEN_1 = 4;
const int PIN_KORRY_LIGHT_COWL_VALVE_OPEN_2 = 6;
const int PIN_KORRY_LIGHT_L_VALVE_OPEN = 16;
const int PIN_KORRY_LIGHT_R_VALVE_OPEN = 20;

const int PIN_SWITCH_ENGINE_ANTI_ICE_1 = 19;
const int PIN_SWITCH_WING_ANTI_ICE = 9;
const int PIN_SWITCH_ENGINE_ANTI_ICE_2 = 7;

//CONNECTED SWITCH PINS NOT USED: 18,5,21,8

FlightSimFloat xPlane_wing_ice_on_left;
FlightSimFloat xPlane_wing_ice_on_right;
FlightSimFloat xPlane_cowl_ice_on_0;
FlightSimFloat xPlane_cowl_ice_on_1;
FlightSimFloat xPlane_cowl_ice_0;
FlightSimFloat xPlane_cowl_ice_1;

FlightSimFloat xPlane_eng1_heat_pos;
FlightSimFloat xPlane_eng2_heat_pos;
FlightSimFloat xPlane_wing_heat_pos;

//array, overhead panel = 2
FlightSimFloat xPlane_panel_brightness_ratio_manual;
void setup() {
  // put your setup code here, to run once:
  xPlane_wing_ice_on_left = XPlaneRef("laminar/B738/annunciator/wing_ice_on_L");
  xPlane_wing_ice_on_right = XPlaneRef("laminar/B738/annunciator/wing_ice_on_R");
  xPlane_cowl_ice_on_0 = XPlaneRef("laminar/B738/annunciator/cowl_ice_on_0");
  xPlane_cowl_ice_on_1 = XPlaneRef("laminar/B738/annunciator/cowl_ice_on_1");
  xPlane_cowl_ice_0 = XPlaneRef("laminar/B738/annunciator/cowl_ice_0");
  xPlane_cowl_ice_1 = XPlaneRef("laminar/B738/annunciator/cowl_ice_1");

  xPlane_eng1_heat_pos = XPlaneRef("laminar/B738/ice/eng1_heat_pos");
  xPlane_eng2_heat_pos = XPlaneRef("laminar/B738/ice/eng2_heat_pos");
  xPlane_wing_heat_pos = XPlaneRef("laminar/B738/ice/wing_heat_pos");

  xPlane_panel_brightness_ratio_manual = XPlaneRef("sim/cockpit2/electrical/panel_brightness_ratio_manual[2]");
   
  pinMode(PIN_5V_BACKLIGHT,OUTPUT);
  pinMode(PIN_KORRY_PUSH_TO_TEST,OUTPUT);
  pinMode(PIN_KORRY_LIGHT_L_VALVE_OPEN,OUTPUT);
  pinMode(PIN_KORRY_LIGHT_R_VALVE_OPEN,OUTPUT);
  pinMode(PIN_KORRY_LIGHT_COWL_VALVE_OPEN_1,OUTPUT);
  pinMode(PIN_KORRY_LIGHT_COWL_VALVE_OPEN_2,OUTPUT);
  pinMode(PIN_KORRY_LIGHT_COWL_ANTI_ICE_1,OUTPUT);
  pinMode(PIN_KORRY_LIGHT_COWL_ANTI_ICE_2,OUTPUT);

  pinMode(PIN_SWITCH_ENGINE_ANTI_ICE_1,INPUT_PULLUP);
  pinMode(PIN_SWITCH_WING_ANTI_ICE,INPUT_PULLUP);
  pinMode(PIN_SWITCH_ENGINE_ANTI_ICE_2,INPUT_PULLUP);
}

void loop() {
  FlightSim.update();   

  analogWrite(PIN_5V_BACKLIGHT,mapfloat(xPlane_panel_brightness_ratio_manual, 0,1,0,255));
  analogWrite(PIN_KORRY_PUSH_TO_TEST,255); //this should be controlled by the aircrafts battery/amp power, if no power then the korry should not light on this test.

  analogWrite(PIN_KORRY_LIGHT_L_VALVE_OPEN, mapfloat(xPlane_wing_ice_on_left, 0,1,0,255));     
  analogWrite(PIN_KORRY_LIGHT_R_VALVE_OPEN, mapfloat(xPlane_wing_ice_on_right,0,1,0,255));
  analogWrite(PIN_KORRY_LIGHT_COWL_VALVE_OPEN_1,mapfloat(xPlane_cowl_ice_on_0,0,1,0,255));
  analogWrite(PIN_KORRY_LIGHT_COWL_VALVE_OPEN_2,mapfloat(xPlane_cowl_ice_on_1,0,1,0,255));
  analogWrite(PIN_KORRY_LIGHT_COWL_ANTI_ICE_1,mapfloat(xPlane_cowl_ice_0,0,1,0,255));
  analogWrite(PIN_KORRY_LIGHT_COWL_ANTI_ICE_2,mapfloat(xPlane_cowl_ice_1,0,1,0,255));

  if (digitalRead(PIN_SWITCH_WING_ANTI_ICE) == HIGH && xPlane_wing_heat_pos == 0) {
    xPlane_wing_heat_pos = 1;
  }
  if (digitalRead(PIN_SWITCH_WING_ANTI_ICE) == LOW && xPlane_wing_heat_pos == 1) {
    xPlane_wing_heat_pos = 0;
  }

  if (digitalRead(PIN_SWITCH_ENGINE_ANTI_ICE_2) == HIGH && xPlane_eng2_heat_pos == 1) {
    xPlane_eng2_heat_pos = 0;
  }
  if (digitalRead(PIN_SWITCH_ENGINE_ANTI_ICE_2) == LOW && xPlane_eng2_heat_pos == 0) {
    xPlane_eng2_heat_pos = 1;
  }

  if (digitalRead(PIN_SWITCH_ENGINE_ANTI_ICE_1) == HIGH && xPlane_eng1_heat_pos == 1) {
    xPlane_eng1_heat_pos = 0;
  }
  if (digitalRead(PIN_SWITCH_ENGINE_ANTI_ICE_1) == LOW && xPlane_eng1_heat_pos == 0) {
    xPlane_eng1_heat_pos = 1;
  }

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

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;
}
Title: Teensy LC Anti Ice Panel Sketch Code
Post by: archen on October 05, 2018, 09:49:13 PM
Hello Peter,

This looks awesome. I'm currently using Arduino with SimVim but I'm looking at options for my desktop 737 sim I'm building.

Is it hard to learn teensy? Do you have any good links on how to get started? I understand teensy is Arduino based. Is it possible to use any Arduino board or do you need a teensy specific board?

I like the simplicity with SimVim but as you know with simplicity comes limitations so I'm looking for alternatives.

Is teensy able to communicate via Ethernet?


Best regards
Anders

Skickat från min SM-G955F via Tapatalk

Title: Re: Teensy LC Anti Ice Panel Sketch Code
Post by: kurt-olsson on October 06, 2018, 04:06:06 AM
Hi Anders.

I started with Arduino and was going to write my C++ plugin myself for X-plane because i wanted total control and not be limited by another library written by someone else. But FlightsimControl is all i wanted it had exactly the same architechture that i was building for. So no need to invent the wheel again.

Read:https://www.pjrc.com/teensy/td_flightsim.html

Teensy with X-Plane is a real win, stable and fast. I dont know if your using X-plane but for me there is no option to use old FSX or Prepared3D that looks ugly and is a slideshow. See lots of people building cool setup ending up with cartoon 13FPS. But this is my own opinion.

I have only tried the Teensy with USB cable, i am not going wifi route because it needs to be stable and cable provides just that.

Also the coding is really easy, there are 4-5 different techniques in a boeing. Switch, lights, motors etc and it is alot of repettwting coding for all components.

I am using sketch language same as on Arduino, really great.