trouble converting string to int
in
Programming Questions
•
1 month ago
I'm trying to convert serial data, coming from my Arduino, to an integer that Processing can use to change the color (gray value) of a rectangle.
myString returns a perfectly functioning value, but when converted using val=int(myString), it returns null.
I know you can't test it without my Arduino Uno setup, but it seems like I'm simply missing something obvious.
What am I doing wrong?
myString returns a perfectly functioning value, but when converted using val=int(myString), it returns null.
I know you can't test it without my Arduino Uno setup, but it seems like I'm simply missing something obvious.
What am I doing wrong?
- /**
* Simple Read
*
* Read data from the serial port and change the color of a rectangle
* when a switch connected to a Wiring or Arduino board is pressed and released.
* This example works with the Wiring / Arduino program that follows below.
*/
import processing.serial.*;
int lf = 10; // Linefeed in ASCII
String myString = null;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup()
{
size(200, 200);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
myPort.clear();
// Throw out the first reading, in case we started reading
// in the middle of a string from the sender.
myString = myPort.readStringUntil(lf);
myString = null;
}
void draw()
{
while (myPort.available() > 0) {
myString = myPort.readStringUntil(lf);
if (myString != null) {
val=int(myString);
println(myString);
println(val);
}
}
background(255); // Set background to white // If the serial value is not 0,
fill(val); // set fill to gray value determined by val
rect(50, 50, 100, 100);
// }
}
1