I'm currently working on a project that includes a short demo game, we've decided to use LibGDX as the java library and we're required to use an arduino and/or raspberry pi as a controller input. My assumption was to make an inputprocessor that uses the serial monitor input sent by the arduino. We're currently stumped on how to accomplish this and aren't even sure if you can use the inputprocessor in this way.
#define left 2
#define right 3
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(left))
{
goLeft();
}
if(digitalRead(right))
{
goRight();
}
delay(100);
}
void goLeft()
{
Serial.println(1000);
}
void goRight()
{
Serial.println(500);
}
This is the code running on the Arduino it's a simple setup that just serial prints 2 different numbers depending on what button is pressed to be later used as a left or right direction in the game.
port = SerialPort.getCommPort("COM4");
if(port.openPort()) {
System.out.println("Successfully opened the port.");
} else {
System.out.println("Unable to open the port.");
return;
}
port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
data = new Scanner(port.getInputStream());
if(data.hasNextLine())
{
int number = 0;
try{number = Integer.parseInt(data.nextLine());}catch(Exception e){}
if(number == 500)
{
bucket.x += 200 * Gdx.graphics.getDeltaTime();
}
if(number == 1000)
{
bucket.x -= 200 * Gdx.graphics.getDeltaTime();
}
}
This is the code currently used during the render() function in our game. However what happens is that the game will only continue to render when there is a nextLine in the serial monitor and will not render at any other times. We're currently using the java library jserial comm in order to facilitate the connection between the ardiono and java application. For this reason we believed we would require an inputprocessor however as we're currently student developers we're unable to figure out if this is the case and how we could accomplish it.