【问题标题】:ArdunioJoystickLibrary ButtonState PreferenceArdunioJoystickLibrary ButtonState Preference
【发布时间】:2017-03-30 16:49:16
【问题描述】:

您好,我目前正在使用 MHeironimus 的 ArduinoJoystickLibrary,我有一个 3 按钮游戏手柄,但我遇到了问题。我的 pin 2 上的按钮通常是打开的,我如何在代码中将其反转以使状态正常关闭。

我真的不知道,我只是修改了键盘+按钮摇杆代码让它正常工作,我基本上只需要能够控制每个按钮的初始状态,因为有时开关/按钮可能会反了。

一开始我以为这部分可以做到:

// Last state of the button
int lastButtonState[3] = {0,0,0};

如果我只是将这个{0,0,0} 更改为喜欢这个{0,1,0},那么我在引脚3 上的按钮通常会在HIGH 上或在HIGH 上。但是没有。基本上我只需要能够即时控制 3 个按钮状态中的每一个,因为我永远不知道开关/按钮将如何从我的大量开关/按钮中做出反应。

见下面的代码:

#include <Joystick.h>

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  3, 0,                  // Button Count, Hat Switch Count
  false, false, false,   // X and Y, but no Z Axis
  false, false, false,   // No Rx, Ry, or Rz
  false, false,          // No rudder or throttle
  false, false, false);  // No accelerator, brake, or steering

void setup() {
  // Initialize Button Pins
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);

  // Initialize Joystick Library
  Joystick.begin();
}

// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 2;

// Last state of the button
int lastButtonState[3] = {0,0,0};

void loop() {

  // Read pin values
  for (int index = 0; index < 3; index++)
  {
    int currentButtonState = !digitalRead(index + pinToButtonMap);
    if (currentButtonState != lastButtonState[index])
    {
      if (index < 4) {
        Joystick.setButton(index, currentButtonState);
        lastButtonState[index] = currentButtonState;
      } else {
        if (currentButtonState) {
          Joystick.setButton(index, currentButtonState);
          lastButtonState[index] = currentButtonState;
        }
      }
    }
  }

  delay(10);
}

【问题讨论】:

    标签: button arduino joystick


    【解决方案1】:

    定义你想要翻转的按钮:

    bool flipButtons[3] = {false, true, false};
    

    然后用它来翻转它,当你读取按钮状态时:

    int currentButtonState = !digitalRead(index + pinToButtonMap) ^ flipButtons[index];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-13
      相关资源
      最近更新 更多