X-Plane + Teensy Code help

Status
Not open for further replies.

raflyer

Well-known member
I added the EGT section to my code today and now the rest of the code quit working. If I take out all the EGT gauge stuff then the original code works fine again. Any idea what I missed? Thanks
Rob
Code:
// Code for Course Dev Bar and GS on HSI
#include <Servo.h> 

Servo glareDim;  // create servo object to control a servo 

FlightSimFloat crsBar;  //-2.0 to 2.0 2 dot deflection
FlightSimFloat gsBar;  //-2.0 to 2.0 2 dot deflection
FlightSimFloat egt1;   // in degrees celcius
#define CourseDeviation 10
#define GsDeviation 9
#define Egt1Gauge 3

int potpin = 15;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 


// setup runs once
void setup() {
  setupCourseDeviation();
  setupGsDeviation();
  setupEgt1Gauge();
  glareDim.attach(4);  
}

// loop runs repetitively, as long as power is on
void loop() {
  FlightSim.update(); // causes X-Plane's changes to be received
  updateCourseDeviation();
  updateGsDeviation();
  updateEgt1Gauge();

}


void setupCourseDeviation() {
  crsBar = XPlaneRef("sim/cockpit/radios/nav1_hdef_dot");
  pinMode(CourseDeviation, OUTPUT);  // PWM out
}

void updateCourseDeviation() {
  // crsBar -2.0 to 2.0 2 dot deflection
  int dev = (crsBar * 127); // dev = Deviation of course bar
  dev = map(dev, -254, 254, 0, 255);
  analogWrite(CourseDeviation, dev);
}

void setupGsDeviation() {
  gsBar = XPlaneRef("sim/cockpit/radios/nav1_vdef_dot");
  pinMode(GsDeviation, OUTPUT);  // PWM out
}

void updateGsDeviation() {
  // crsBar -2.0 to 2.0 2 dot deflection
  int gsdev = (gsBar * 127); // dev = Deviation of course bar
  gsdev = map(gsdev, -254, 254, 0, 255);
  analogWrite(GsDeviation, gsdev);

  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 800, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  glareDim.write(val);                  // sets the servo position according to the scaled value 
  delay(15); 

}

void setupEgt1Gauge(){
  egt1 = XPlaneRef("sim/cockpit2/engine/indicators/EGT_deg_C[0]");
  pinMode(Egt1Gauge, OUTPUT);
}

void updateEgt1Gauge(){
  egt1 = map(egt1, 0, 700, 0, 3095);
  analogWriteResolution(12);
  analogWrite(Egt1Gauge, egt1);
  
}
 
Rob,

analogWriteResolution() is global for all pins. So, if you set it in updateEgt1Gauge(), it will interfere with the values you write in updateGsDeviation() and updateCourseDeviation().

You should either scale all your values to the same range (8 bits: 0-255 or 12 bits: 0-4095) and use a single analogWriteResulution during the setup phase or use analogWriteResolution before each call to analogWrite.
 
Rob, one more thing. Try to avoid the delay() function in updateGsDeviation(). It might interfere with your other controls and reduces their update rate.

You could use something like this. Check the elapsedMillis() documentation: https://www.pjrc.com/teensy/td_timing_elaspedMillis.html

Code:
elapsedMillis glareDimTimer;

void updateGsDeviation() {
  // crsBar -2.0 to 2.0 2 dot deflection
  int gsdev = (gsBar * 127); // dev = Deviation of course bar
  gsdev = map(gsdev, -254, 254, 0, 255);
  analogWrite(GsDeviation, gsdev);

  if (glareDimTimer>15) {  // only executes if more than 15 milliseconds have passed since
                           // glareDimTimer was started
    glareDimTimer -= 15;   // reset glareDimTimer
    val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
    val = map(val, 0, 800, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
    glareDim.write(val);                  // sets the servo position according to the scaled value 
  }

}
 
Status
Not open for further replies.
Back
Top