String to a 3 digit Integer?
in
Programming Questions
•
3 years ago
Hi, Im quite new with processing and Ive been trying it for a while.
Im trying to make a graph to display the information of an analog sensor.
Im receiving by Serial a 3 digit STRING number of the analogue value: 0-255 successfuly and printing it in a textbox. Now, trying to make a graph, I used the curveVertex(x, y); method. My problem comes when I try to put that STRING number into the curveVertex(x, y) where I get an error that I cant put a STRING in those varibles.
Is where I tried to convert the STRING into an INTEGER with Integer.parseInt but it just saves in the string the 3 digit number several times until it overflows and gives an error: NumberFormatException:for input string: 2682702692
while what I wanted was the 268, then the 270, and then the 269.
my questions are: is there any way to put in the CurveVerte(x,y) a String? or how can I convert that string into an integer or anything I can use for the CurveVertex?
Here is my code:
import processing.serial.*;
Serial myPort; // The serial port
void setup()
{
size(400,400);
background(255);
println(Serial.list());
myPort = new Serial(this, Serial.list()[3], 9600);
}
void draw()
{
// Expand array size to the number of bytes you expect
byte[] inBuffer = new byte[3];
while (myPort.available() > 0)
{
inBuffer = myPort.readBytes();
myPort.readBytes(inBuffer);
if (inBuffer != null)
{
String myString = new String(inBuffer);
int data = Integer.parseInt(myString);
println(data);
fill(200, 100, 200);
text("accX: ", 15, 30);
text(data, 50, 30);
line(10, 270, 150, 270);
noFill();
stroke(0);
smooth();
beginShape();
for(int j=20;j<=140;j++)
{
curveVertex(j, data); // the first control point
}
endShape();
}
}
}
thanks and im looking forwards to any help!
1