Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Encoder with X Plane

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    11

    Encoder with X Plane

    Hello guys hoping someone can get me on the right track here. I'm running Teensy 2.0++ with X Plane 11. Trying to get an encoder to activate a certain dataref. here is the program:

    #include <Encoder.h>

    #define TempSel_Left 25

    #define TempSel_Right 26

    Encoder TempSel = Encoder(25,26);

    FlightSimInteger TempSelRotary;

    long encoder_prev=0; // detects rotary position change

    void setup() {

    pinMode(25, INPUT_PULLUP);

    pinMode(26, INPUT_PULLUP);

    TempSelRotary = XPlaneRef("1-sim/gauge/Tempsel");

    }

    void loop() {

    FlightSim.update();

    long enc = TempSel.read();

    if (enc != encoder_prev){

    TempSelRotary = TempSelRotary + (enc - encoder_prev);

    encoder_prev = enc;}

    This is for a third party aircraft. I have the correct dataref that's not an issue.

    Basically

    what the knob in the 3rd party aircraft is its a D rate knob that changes the engine temp from

    -30 to +70. and the dataref for those are 0.0 to 1.00. So there is only 21 detents changing

    5 degrees each detent.

    The code I wrote changes the knob in x plane but, it only changes +70 and -30 only those 2

    detents nothing in between. I can't figure out what exactly I am missing to turn the rest. I am using a cts288 encoder.

    Any help would be awesome.

  2. #2
    Senior Member
    Join Date
    Aug 2016
    Posts
    128
    so if I'm reading things right, you need it to output from 0 to 1.0 as you twist the knob, but whats happening is its only outputting 0 or 1. so maybe your not using a floating point variable. or its not incrementing it by a tiny amount. the amount it should increment or decrement is 1.0 / 21 = .0476 or basicly .05, keep in mind I dont have xplane, plus you didnt post all the code.

  3. #3
    Junior Member
    Join Date
    Nov 2018
    Posts
    11
    Yes you are correct about the output from 0.0 to 1.0. and not incrementing a tiny bit. That's basically all the code for that knob.

  4. #4
    Senior Member
    Join Date
    Aug 2016
    Posts
    128
    this line seems to set the value, its the one that must be changed to either add or subtract .05 for each detent of the encoder. somehow.

    TempSelRotary = TempSelRotary + (enc - encoder_prev);

    that should get you to do the whole range, plus you need to check to make sure it doesnt go below 0.0 or above 1.0

  5. #5
    The encoder returns integers so the 1st issue your not going to get floating point. To debug I would print the TempSel.read(); every time it changes and print the read values without subtracting to see what the values you get when you turn the encoder. It should become obvious after a while what math is needed to translate to 0.0 to 1.0.

    I have a rotary encoder for example which goes up +4 each clockwise click and -4 each counter clockwise tick.

  6. #6
    Junior Member
    Join Date
    Nov 2018
    Posts
    11
    Yes firehopper, I ended up changing this line "TempSelRotary = TempSelRotary + (enc - encoder_prev);" and adding
    ((enc _ encoder_prev) * .05;
    encoder_prev = enc *.05;}
    I can now get all of the detents showing up perfectly. The only thing I need to do now is somehow to get it to only use the detents
    between 0.0 to 1.0. Other than that its working great.

  7. #7
    Senior Member
    Join Date
    Aug 2016
    Posts
    128
    basicly you need to go
    if value < 0 then value = 0
    if value > 1.0 then value = 1.0
    that kinda thing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •