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

Thread: Trying to get X-Plane heading bug to move

  1. #1
    Senior Member
    Join Date
    Oct 2015
    Posts
    239

    Trying to get X-Plane heading bug to move

    I'm trying to get my rotary encoder to operate X-Plane C-172 heading bug.

    I'm using code that works fine without the Flight Sim code so I know that part of it works. I used ISR method for the encoder.

    I looked at the examples and added as much of the Flight Sim code as I could.

    I'm not sure how to code the part where I'm actually turning the encoder knob. I have Serialprintln() commented out where I was sending info to the serial window.

    I think it's in there that I need to add some of the flight sim code but I'm not sure what?

    Thank you for any help...

    Code:
    // Create Variables (global scope)
    FlightSimCommand HeadingUp;
    FlightSimCommand HeadingDown;
    
    // Single rotary encoder ==================================
                 const int encoder_HDG_SingleKnob_PinA = 3; 
                 const int encoder_HDG_SingleKnob_PinB = 4;
                 const int encoder_HDG_SingleKnob_PushSwitch = 5;
              volatile long encoder_HDG_SingleKnob_Count = 0;
                       long encoder_HDG_SingleKnob_LastCount = 0;
    volatile unsigned long encoder_HDG_SingleKnob_LastInterruptTime = 0;
    // Single rotary encoder ==================================
    
    int buttonPressDelay = 0;
    
    // ------------------------------------------------------------------
    // SETUP    SETUP    SETUP    SETUP    SETUP    SETUP    SETUP    
    // ------------------------------------------------------------------
    void setup()
    {
      Serial.begin(115200); // 115200
      while (!Serial); // wait for Arduino Serial Monitor
    
      pinMode(encoder_HDG_SingleKnob_PinA, INPUT);
      pinMode(encoder_HDG_SingleKnob_PinB, INPUT);
      pinMode(encoder_HDG_SingleKnob_PushSwitch, INPUT_PULLUP);
      //Serial.println("setup start");
      attachInterrupt(digitalPinToInterrupt(encoder_HDG_SingleKnob_PinA), encoder_ISR_HDG_SingleKnob_PinA, LOW);
    
      HeadingUp = XPlaneRef("sim/autopilot/heading_up");
      HeadingDown = XPlaneRef("sim/autopilot/heading_down");
      
      //Serial.println("setup end");
    }
    
    // ------------------------------------------------------------------
    // LOOP     LOOP     LOOP     LOOP     LOOP     LOOP     LOOP
    // ------------------------------------------------------------------
    void loop()
    {
      FlightSim.update();   // <-- causes actual reception of data
      
      if ((!digitalRead(encoder_HDG_SingleKnob_PushSwitch)))
      {
        encoder_HDG_SingleKnob_Count = 0;
    	encoder_HDG_SingleKnob_LastCount = 0;
        while (!digitalRead(encoder_HDG_SingleKnob_PushSwitch))
        {
          delay(10);      
        }
        //Serial.println("Reset");
      }
    
      // If the current rotary switch position has changed then update everything
      if (encoder_HDG_SingleKnob_Count == encoder_HDG_SingleKnob_LastCount)
      {
    
      }
      else
      {
    	  if (encoder_HDG_SingleKnob_Count > encoder_HDG_SingleKnob_LastCount)
    	  {
    		  // Heading knob manually turned cw here
    		  //Serial.print("cw ");
    		  //Serial.println(encoder_HDG_SingleKnob_Count);
    	  }
    	  else
    	  {
    		  // Heading knob manually turned ccw here
    		  //Serial.print("ccw ");
    		  //Serial.println(encoder_HDG_SingleKnob_Count);
    	  }
    	  encoder_HDG_SingleKnob_LastCount = encoder_HDG_SingleKnob_Count;
      }
    }
    
    // ------------------------------------------------------------------
    // INTERRUPT     INTERRUPT     INTERRUPT     INTERRUPT     INTERRUPT 
    // ------------------------------------------------------------------
    void encoder_ISR_HDG_SingleKnob_PinA()
    {
    	unsigned long interruptTime = millis();
    	if (interruptTime - encoder_HDG_SingleKnob_LastInterruptTime > 5)
    	{
    		if (digitalRead(encoder_HDG_SingleKnob_PinB) == LOW)
    		{
    			encoder_HDG_SingleKnob_Count--;
    		}
    		else
    		{
    			encoder_HDG_SingleKnob_Count++;
    		}
    	}
    	encoder_HDG_SingleKnob_LastInterruptTime = interruptTime;
    }

  2. #2
    Junior Member
    Join Date
    Sep 2020
    Posts
    1
    your problem is Trying to get X-Plane heading bug to move

    have a look at the example Teensy/Flightsim/NavFrequency ,
    where the frequency is changed up or down by a set of flightsimcommands UP and DN with an encoder.
    this is pretty much the same task - you only have to replace NAV FREQUENCY with HEADING BUG.

    I STRONGLY recommend >>> USE THE ENCODER Library ! DONT write any encoder code with interrupts yourself.
    the teensy will automaticly use interrupts on the two designed pins for SCK and DA.
    when ever the countervalue changes ( this is the only thing you can observe, because all the other signals are very short in time )
    create an IMPULS for UP or DOWN just for one cycle. this impuls will write the value 1 to your command.
    some cycles ( or millis ) later create another Impuls , which will write the value 0 to your command.

    The TEENSY MONITOR PLUGIN will show you exactly the appearance of your signals like HDG UP begin and HDG UP end
    you have to make sure , that there is always the pair "begin" followed by "end" for every COMMAND.

Posting Permissions

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