1

I have a Flexiforce 25 pound pressure sensors connected to my Arduino Uno which connects to my laptop. My requirement is to be able to play video games using taps on pressure sensors. I am successfully sending signals to my Laptop and I am using Java on my Mac to send keystrokes (Key Down and Key Up) signals to move in the game. However I am facing issues with long key down and key up. How do I detect that and make sure that my program runs smooth?

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

In java I'm using java.wt.Robot and gnu.io.SerialPort to press key and read from serial port. On a serialEvent I will override the listener function to perform my operation which is press Keys on my machine.

Java Code:

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

This performs Up, Down, Left, Right, "S" and "D" operation for SuperMario 3 GBA game. This works but not as smooth as intended. Is there anything that I can use to directly bind to my keyboard to get a smoother key operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.