【问题标题】:Flexiforce to Uno to Keyboard. Working but not as intededFlexiforce 到 Uno 到键盘。工作但未按预期工作
【发布时间】:2016-02-24 19:55:32
【问题描述】:

我有一个 Flexiforce 25 磅压力传感器连接到我的 Arduino Uno,它连接到我的笔记本电脑。我的要求是能够使用压力传感器上的水龙头玩视频游戏。我成功地向我的笔记本电脑发送信号,并且我在我的 Mac 上使用 Java 发送击键(Key Down 和 Key Up)信号以在游戏中移动。但是,我面临着长按键和按键的问题。如何检测并确保我的程序运行顺畅?

Uno 代码:

void setup() {}

void loop() {
int sensorValue = analogRead(A0);
int sensorValue2 = analogRead(A1);
int sensorValue3 = analogRead(A2);
int sensorValue4 = analogRead(A3);
int sensorValue5 = analogRead(A4);
int sensorValue6 = digitalRead(2);

// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
float voltage2 = sensorValue2 * (5.0 / 1023.0);
float voltage3 = sensorValue3 * (5.0 / 1023.0);
float voltage4 = sensorValue4 * (5.0 / 1023.0);
float voltage5 = sensorValue5 * (5.0 / 1023.0);
float voltage6 = sensorValue6 * (5.0 / 1023.0);

if(voltage > 1){ 
   Serial.begin(9600); Serial.println("A0"); Serial.end();
}
if(voltage2 > 1){ 
   Serial.begin(9600); Serial.println("A1"); Serial.end();
}
if(voltage3 > 1){ 
   Serial.begin(9600); Serial.println("A2"); Serial.end();
}
if(voltage4 > 1){ 
   Serial.begin(9600); Serial.println("A3"); Serial.end();
}
if(voltage5 > 1){ 
   Serial.begin(9600); Serial.println("A4"); Serial.end();
}
if(voltage6 > 0){ 
   Serial.begin(9600); Serial.println("D2"); Serial.end();
}
// Wait 100 milliseconds
delay(100);
}

在 java 中,我使用 java.wt.Robotgnu.io.SerialPort 按键并从串行端口读取。在 serialEvent 上,我将覆盖侦听器函数以执行我的操作,即在我的机器上按下键。

Java 代码:

public void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            String inputLine = null;
            if (input.ready()) {
                inputLine = input.readLine();
                if (inputLine.contains("A0")) {

                    long start = System.currentTimeMillis();
                    while (System.currentTimeMillis() - start < duration) {
                        robot.keyPress(KeyEvent.VK_UP);
                    }
                    robot.keyRelease(KeyEvent.VK_UP);
                    System.out.println("UP");

                } else if (inputLine.contains("A1")) {

                    long start = System.currentTimeMillis();
                    while (System.currentTimeMillis() - start < duration) {
                        robot.keyPress(KeyEvent.VK_DOWN);
                    }
                    robot.keyRelease(KeyEvent.VK_DOWN);
                    System.out.println("DOWN");

                } else if (inputLine.contains("A2")) {

                    long start = System.currentTimeMillis();
                    while (System.currentTimeMillis() - start < duration) {
                        robot.keyPress(KeyEvent.VK_LEFT);
                    }
                    robot.keyRelease(KeyEvent.VK_LEFT);
                    System.out.println("LEFT");

                } else if (inputLine.contains("A3")) {

                    long start = System.currentTimeMillis();
                    while (System.currentTimeMillis() - start < duration) {
                        robot.keyPress(KeyEvent.VK_RIGHT);
                    }
                    robot.keyRelease(KeyEvent.VK_RIGHT);
                    System.out.println("RIGHT");

                } else if (inputLine.contains("A4")) {

                    long start = System.currentTimeMillis();
                    while (System.currentTimeMillis() - start < duration) {
                        robot.keyPress(KeyEvent.VK_S);
                    }
                    robot.keyRelease(KeyEvent.VK_S);
                    System.out.println("S");

                } else if (inputLine.contains("D2")) {
                    robot.keyPress(KeyEvent.VK_D);
                    robot.keyRelease(KeyEvent.VK_D);
                    System.out.println("D");

                }
            }

        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}

这将执行 SuperMario 3 GBA 游戏的上、下、左、右、“S”和“D”操作。这有效,但不如预期的那么顺利。有什么东西可以直接绑定到我的键盘上,以获得更流畅的按键操作。

【问题讨论】:

  • “顺利”是什么意思?无论如何,您可以直接连接到 USB 端口并使 arduino 显示为 HID 外围设备(在本例中为键盘)。然后,您可以直接从板上发送击键,而不是使用 PC 上的程序/服务。您可以使用支持 USB 的 arduino 板(例如 leonardo)或编写自己的程序(例如使用 V-USB 库)
  • 平滑我的意思是现在连续动作有延迟。假设我拿着传感器一会儿。 Arduino 发送多次击键并执行。即使我没有按下它也需要一段时间才能停止发送的击键。
  • 我已经下载了 V-USB 库,但我无法直接从 Arduino 发送按键。有没有办法通过这个进行调试?谢谢。我开始研究 Arduino Uno,然后意识到我应该订购 Leonardo。这是一个最终项目,我认为与莱昂纳多交换并开始工作对我来说有点晚了。
  • 你关注this article了吗? V-USB 是 ATmel 处理器的库,所以我唯一一次使用它是没有 arduino 框架,但我使用 avr-gcc 编译 C++ 程序

标签: java macos keyboard arduino arduino-uno


【解决方案1】:

我的建议是使用 Arduino Leonardo (https://www.arduino.cc/en/Main/ArduinoBoardLeonardo) 或 Arduino Micro (https://www.arduino.cc/en/Main/ArduinoBoardMicro) 而不是 UNO。这将允许您使用内置键盘库 (https://www.arduino.cc/en/Reference/MouseKeyboard) 进行按键操作,无需将串行数据转换为按键操作。

使用 Leonardo 或 Micro 的另一个优势是让您可以选择使用 Arduino 操纵杆库将此设备显示为游戏控制器/操纵杆(您可以在 http://www.instructables.com/id/Arduino-LeonardoMicro-as-Game-ControllerJoystick/http://mheironimus.blogspot.com/2015/11/arduino-joystick-library.html 阅读有关此库的信息)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 2018-08-21
    • 2016-10-30
    • 2021-06-04
    • 2022-01-24
    • 2015-05-11
    • 2020-05-15
    相关资源
    最近更新 更多