Flexiforce到Uno到键盘。工作但不是有意义的

时间:2015-11-22 18:45:59

标签: java macos keyboard arduino arduino-uno

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

Uno code:

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

执行Up,Down,Left,Right,&#34; S&#34;和&#34; D&#34; SuperMario 3 GBA游戏的操作。这可行,但不如预期的那么顺利。有什么我可以用来直接绑定到我的键盘以获得更顺畅的键操作。

1 个答案:

答案 0 :(得分: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 Joystick库将此设备显示为游戏控制器/操纵杆(您可以在http://www.instructables.com/id/Arduino-LeonardoMicro-as-Game-ControllerJoystick/或{{3}阅读此库。 })。