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;
}