I am trying to make a hands free mouse using Arduino IDE and Processing IDE. I don't have a problem with serializing the code and converting to string, but when I try and convert the serial string into int the int goes to 0. I have tried to trim it and used
integer.parseInt
but it still gives me the error:NumberFormatException : For input String:"".
Here is my code:
import processing.serial.*;
import java.awt.event.KeyListener;
import java.awt.Robot;
Serial myPort; // Create object from Serial class
Robot robot;
String val; // Data received from the serial port
boolean valtrue;
int xy = 0;
int x=0;
void setup()
{
String portName = Serial.list()[3];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('.');
try {
robot = new Robot();
}
catch (Exception e) {
e.printStackTrace();
}
}
void draw() {
//reading serial port until \n
String sensorValue = myPort.readStringUntil('\n');
if (sensorValue != null) {
int value = Integer.parseInt(sensorValue.trim());
println(value);
}
}
If anyone is able to help, please answer.
--
Processing has an
int()
function that you can use instead of Integer.parseInt()
. Behind the scenes the int()
function is calling the Integer.parseInt()
function, but it's shorter. Up to you.
But your error says it all: you're passing an empty
String
value ""
into the parseInt()
function. An empty String
value can't be turned into a number, so you get the exception.
You're going to have to track down why your
String
value is empty, but that's what's causing this particular error. I will point out that the draw()
function is called 60 times per second, and you're trying to read from your port every single time, so maybe you're reading faster than you're writing?
--
댓글 없음:
댓글 쓰기