最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

controller - Arduino Pro micro Flight Peripheral (I have very limited programing knowledge) - Stack Overflow

programmeradmin4浏览0评论

Hey I need help with "my" code. I generated it with chat GPT based on the data for the arduino pro micro I have and data on the sensors/switches i use. I want my simulator peripheral to interact with the pc independent of other peripherals used for flight simulators(like joystick and pedals), I plan to use it mainly with X-plane 11. Or if its compatible microsoft flight sim, but x plane is the main target. I have 3 sliding pot-meters (10kohm) and I want them to have a percentage value that i can bind to the specific controls in the simulator (Throttle (gas) prop (airplane propeller rpm) mixture (fuel to air ratio). The toggle switches (and rotary) i can program, once i made a simpler peripheral and i just bound them to key presses on a keyboard and it worked fine. Probably not the most elegant solution but its the one i came up with and understood the workings of.

I'm studying aviation so programing is not amongst my skill set, please be patient with me if I don't understand something, or made a mistake blatantly obvious for someone in this field.

#include <Arduino.h>
#include <Joystick.h>   // From the NicoHood HID library
#include <Bounce2.h>    // For debouncing toggle and rotary switches

// Create a Joystick instance
Joystick_ Joystick;

// ----- Pin Definitions -----
// Sliding potentiometers (acting as voltage dividers)
// Throttle (100mm) on A0, Angle-of-Attack (75mm) on A1, Mixture on A2
const int throttlePin = A0;
const int aoaPin      = A1;
const int mixturePin  = A2;

// Calibration values for the throttle potentiometer
// (Assuming 0 ohm corresponds to an analog read of 0 and 10kΩ corresponds to 1023)
const int throttleAnalogMin = 0;
const int throttleAnalogMax = 1023;

// Three toggle switches (wired as on/off switches, ON when connected to GND)
// 1. Master Switch (Airplane Power)
// 2. Comm/Nav Equipment
// 3. PFD/ATX (example like the Xbox "X" button)
// Connected to digital pins D2, D3, and D4 respectively.
const int togglePins[3] = {2, 3, 4};
Bounce toggleDebouncers[3];

// Ignition rotary switch with 5 positions (only one output active at a time)
// Positions are connected to digital pins D5, D6, D7, D8, and D9.
const int ignitionPins[5] = {5, 6, 7, 8, 9};
Bounce ignitionDebouncers[5];

void setup() {
  // Initialize the USB HID joystick interface
  Joystick.begin();

  // Set up toggle switches with internal pull-ups and debouncing
  for (int i = 0; i < 3; i++) {
    pinMode(togglePins[i], INPUT_PULLUP);
    toggleDebouncers[i].attach(togglePins[i]);
    toggleDebouncers[i].interval(25); // 25 ms debounce interval
  }
  
  // Set up ignition rotary switch pins with internal pull-ups and debouncing
  for (int i = 0; i < 5; i++) {
    pinMode(ignitionPins[i], INPUT_PULLUP);
    ignitionDebouncers[i].attach(ignitionPins[i]);
    ignitionDebouncers[i].interval(25);
  }
}

void loop() {
  // --- Process Throttle Potentiometer ---
  int throttleVal = analogRead(throttlePin);
  // Map the raw analog value (0 to 1023) to a percentage (0% to 100%)
  int throttlePercent = map(throttleVal, throttleAnalogMin, throttleAnalogMax, 0, 100);
  throttlePercent = constrain(throttlePercent, 0, 100);
  // Send the throttle percentage to the joystick X-axis
  Joystick.setXAxis(throttlePercent);

  // --- Process Angle-of-Attack and Mixture (raw values; adjust as needed) ---
  int aoaVal     = analogRead(aoaPin);
  int mixtureVal = analogRead(mixturePin);
  Joystick.setYAxis(aoaVal);   // e.g., map AoA to the Y-axis
  Joystick.setZAxis(mixtureVal); // e.g., map Mixture to the Z-axis
  
  // --- Process Toggle Switches ---  
  // With internal pull-ups enabled, a LOW reading indicates the switch is ON.
  // These are mapped to joystick buttons 0, 1, and 2.
  for (int i = 0; i < 3; i++) {
    toggleDebouncers[i].update();
    bool toggleState = (toggleDebouncers[i].read() == LOW);
    Joystick.setButton(i, toggleState);
  }
  
  // --- Process Ignition Rotary Switch ---
  // Map the 5 positions as follows:
  // Position 1 (Off)    → Hat = -1 (centered)
  // Position 2 (Left)   → Hat = 270°
  // Position 3 (Right)  → Hat = 90°
  // Position 4 (Both)   → Hat = 180°
  // Position 5 (Starter)→ Hat = 0°
  int hatValue = -1;  // Default to Off (centered)
  for (int i = 0; i < 5; i++) {
    ignitionDebouncers[i].update();
    if (ignitionDebouncers[i].read() == LOW) { // Active state detected
      if (i == 0) {
        hatValue = -1;    // Off
      } else if (i == 1) {
        hatValue = 270;   // Left
      } else if (i == 2) {
        hatValue = 90;    // Right
      } else if (i == 3) {
        hatValue = 180;   // Both
      } else if (i == 4) {
        hatValue = 0;     // Starter
      }
      break; // Only one position should be active at a time
    }
  }
  Joystick.setHat(hatValue);
  
  delay(20);  // Short delay to limit USB report rate
}

The picture is just for reference to the pin layout.

Hey I need help with "my" code. I generated it with chat GPT based on the data for the arduino pro micro I have and data on the sensors/switches i use. I want my simulator peripheral to interact with the pc independent of other peripherals used for flight simulators(like joystick and pedals), I plan to use it mainly with X-plane 11. Or if its compatible microsoft flight sim, but x plane is the main target. I have 3 sliding pot-meters (10kohm) and I want them to have a percentage value that i can bind to the specific controls in the simulator (Throttle (gas) prop (airplane propeller rpm) mixture (fuel to air ratio). The toggle switches (and rotary) i can program, once i made a simpler peripheral and i just bound them to key presses on a keyboard and it worked fine. Probably not the most elegant solution but its the one i came up with and understood the workings of.

I'm studying aviation so programing is not amongst my skill set, please be patient with me if I don't understand something, or made a mistake blatantly obvious for someone in this field.

#include <Arduino.h>
#include <Joystick.h>   // From the NicoHood HID library
#include <Bounce2.h>    // For debouncing toggle and rotary switches

// Create a Joystick instance
Joystick_ Joystick;

// ----- Pin Definitions -----
// Sliding potentiometers (acting as voltage dividers)
// Throttle (100mm) on A0, Angle-of-Attack (75mm) on A1, Mixture on A2
const int throttlePin = A0;
const int aoaPin      = A1;
const int mixturePin  = A2;

// Calibration values for the throttle potentiometer
// (Assuming 0 ohm corresponds to an analog read of 0 and 10kΩ corresponds to 1023)
const int throttleAnalogMin = 0;
const int throttleAnalogMax = 1023;

// Three toggle switches (wired as on/off switches, ON when connected to GND)
// 1. Master Switch (Airplane Power)
// 2. Comm/Nav Equipment
// 3. PFD/ATX (example like the Xbox "X" button)
// Connected to digital pins D2, D3, and D4 respectively.
const int togglePins[3] = {2, 3, 4};
Bounce toggleDebouncers[3];

// Ignition rotary switch with 5 positions (only one output active at a time)
// Positions are connected to digital pins D5, D6, D7, D8, and D9.
const int ignitionPins[5] = {5, 6, 7, 8, 9};
Bounce ignitionDebouncers[5];

void setup() {
  // Initialize the USB HID joystick interface
  Joystick.begin();

  // Set up toggle switches with internal pull-ups and debouncing
  for (int i = 0; i < 3; i++) {
    pinMode(togglePins[i], INPUT_PULLUP);
    toggleDebouncers[i].attach(togglePins[i]);
    toggleDebouncers[i].interval(25); // 25 ms debounce interval
  }
  
  // Set up ignition rotary switch pins with internal pull-ups and debouncing
  for (int i = 0; i < 5; i++) {
    pinMode(ignitionPins[i], INPUT_PULLUP);
    ignitionDebouncers[i].attach(ignitionPins[i]);
    ignitionDebouncers[i].interval(25);
  }
}

void loop() {
  // --- Process Throttle Potentiometer ---
  int throttleVal = analogRead(throttlePin);
  // Map the raw analog value (0 to 1023) to a percentage (0% to 100%)
  int throttlePercent = map(throttleVal, throttleAnalogMin, throttleAnalogMax, 0, 100);
  throttlePercent = constrain(throttlePercent, 0, 100);
  // Send the throttle percentage to the joystick X-axis
  Joystick.setXAxis(throttlePercent);

  // --- Process Angle-of-Attack and Mixture (raw values; adjust as needed) ---
  int aoaVal     = analogRead(aoaPin);
  int mixtureVal = analogRead(mixturePin);
  Joystick.setYAxis(aoaVal);   // e.g., map AoA to the Y-axis
  Joystick.setZAxis(mixtureVal); // e.g., map Mixture to the Z-axis
  
  // --- Process Toggle Switches ---  
  // With internal pull-ups enabled, a LOW reading indicates the switch is ON.
  // These are mapped to joystick buttons 0, 1, and 2.
  for (int i = 0; i < 3; i++) {
    toggleDebouncers[i].update();
    bool toggleState = (toggleDebouncers[i].read() == LOW);
    Joystick.setButton(i, toggleState);
  }
  
  // --- Process Ignition Rotary Switch ---
  // Map the 5 positions as follows:
  // Position 1 (Off)    → Hat = -1 (centered)
  // Position 2 (Left)   → Hat = 270°
  // Position 3 (Right)  → Hat = 90°
  // Position 4 (Both)   → Hat = 180°
  // Position 5 (Starter)→ Hat = 0°
  int hatValue = -1;  // Default to Off (centered)
  for (int i = 0; i < 5; i++) {
    ignitionDebouncers[i].update();
    if (ignitionDebouncers[i].read() == LOW) { // Active state detected
      if (i == 0) {
        hatValue = -1;    // Off
      } else if (i == 1) {
        hatValue = 270;   // Left
      } else if (i == 2) {
        hatValue = 90;    // Right
      } else if (i == 3) {
        hatValue = 180;   // Both
      } else if (i == 4) {
        hatValue = 0;     // Starter
      }
      break; // Only one position should be active at a time
    }
  }
  Joystick.setHat(hatValue);
  
  delay(20);  // Short delay to limit USB report rate
}

The picture is just for reference to the pin layout.

Share Improve this question asked Mar 14 at 10:46 hetot hehetot he 112 bronze badges 1
  • Can you please elaborate a bit more on what exactly your question/problem is? – André Commented Mar 17 at 9:49
Add a comment  | 

1 Answer 1

Reset to default 0

I can see that at least the map-function is missing, so you would need something like:

int map(int throttleVal, int throttleAnalogMin, int throttleAnalogMax, int outMin, int outMax)
{
    if (throttleVal <= throttleAnalogMin) return outMin;
    if (throttleVal >= throttleAnalogMax) return outMax;

    float fraction = static_cast<float>(throttleVal - throttleAnalogMin) / static_cast<float>(throttleAnalogMax - throttleAnalogMin);

    // Scale to output range, round to nearest int
    int outVal = static_cast<int>(outMin + (outMax - outMin) * fraction + 0.5);

    return outVal;

You can omit the constrain() call that the generative AI has put in, as the function above already takes care of the input range.

发布评论

评论列表(0)

  1. 暂无评论