Using Pixy2 through arduino to roborio via usb

I watched this video:
arduino roborio integration

And after i figured out how to send pixy2 values over to the roborio, I cant figure out how to convert the string the pixy send, to an integer to then use it for robot centering on an object.

A sample of what i have is shown below:

If more context is needed just ask.

Could you post your Robot code and your Arduino code? Without that I can only guess on how your sending/receiving numbers.

My best guess as to your problem is your get_x() function reads a string every time you call it. This means every time you call it you wait for the Arduino to send another number.

Integer.toString() is a valid way to convert strings to ints in java. However it must only have numbers for it to work.

Arduino code:

significant top half of robot code:

So from the looks of your code, you seem to have a few problems. The first problem I see is the way you are sending data. Your loop method is first sending all of the detections (the for loop) and then you send the last detection again if you detect the 0x13 byte through serial. In the past, we have sent the first detection every loop because that is usually the one you want.

Another problem is that you are delaying the code for 50 ms multiple times which might cause the Arduino to be slow.

Your robot code looks mostly good except for the thing I mentioned earlier about the pixy_x() function. To solve this you could get the pixy_x() once per frame and store it in a local variable. In addition, you need to trim the string you read from the Arduino because it will have a newline character (\n) which will cause Integer.valueOf() to fail.