Hello !
I'm currently working on a school project with two friends of mine, we are asked to make a video game using both Processing and Arduino.
We decided to create an avoider type of game : the player is in control of a kind of speed racer, made in Processing. We used the
Arduino Light Theremin tutorial to make a sensor that will be used by the player to "drive" our speed racer.
The vehicle is supposed to be going up and down only, based on the values received from the photosensor.
Here comes our issue...
We are sending the
sensorValue we're getting in Arduino to a serial port, and then we're reading it in Processing, with the intent of using it as a variable to control the Yposition of our speed racer.
Everything works fine until we try to use the variable : it's a string, and we cant seem to be able to convert it to an integer !
We are but beginners in Processing and Arduino, so we tried several codes given as exemples on the internet, hoping for an easy way to convert a string to an integer. Sadly, we are stuck with conversion errors that we cant really understand.
Below are the Arduino and Processing codes, followed by three different ways to convert that we haven't be able to use so far.
Arduino code :
// variable to hold sensor value int sensorValue; // variable to calibrate low value int sensorLow = 1023; // variable to calibrate high value int sensorHigh = 0; // LED pin const int ledPin = 13;
void setup() { // Make the LED pin an output and turn it on pinMode(ledPin, OUTPUT); digitalWrite(ledPin, HIGH); // This sets up the speed connexion to the serial Serial.begin(9600);
// calibrate for the first five seconds after program runs while (millis() < 5000) { // record the maximum sensor value sensorValue = analogRead(A0); if (sensorValue > sensorHigh) { sensorHigh = sensorValue; } // record the minimum sensor value if (sensorValue < sensorLow) { sensorLow = sensorValue; } } // turn the LED off, signaling the end of the calibration period digitalWrite(ledPin, LOW); }
void loop() { //read the input from A0 and store it in a variable sensorValue = analogRead(A0);
//Send sensorValue on the Serial, as an int Serial.println(analogRead(A0));
// wait for a moment delay(100); }
Our Processing code, I'm just posting the part where we are reading the
sensorValue sent to the serial :
import processing.serial.*;
// Name of the serial port Serial myPort;
void setup() { size(800, 600);
// Listing all available serial ports println(Serial.list()); // Matching serial port myPort = new Serial(this, Serial.list()[1], 9600); }
void draw() { //reading serial port until \n String sensorValue = myPort.readStringUntil(13); if (sensorValue != null) { println(sensorValue); } }
These codes work out fine, we do get the correct values in our Processing console. It's all string, thought =/
-------
1) The first conversion attempt we made is with a cast, we tried to cast our string to an int :
void draw() { String sensorValue = myPort.readStringUntil(13); // Creating an integer to store the converted string
int value; if(message != null) { // Casting the string to an int
value = int(sensorValue);
println(value);
}
}
The value returned is 0, as if somehow the string is not converted. This is troubling for us since when we print
sensorValue, the values work just fine...
2) So we tried a different way, as I said : we found the
Integer.parseInt("String");
void draw() { String sensorValue = myPort.readStringUntil(13); // Creating an integer to store the converted string
int value; if(message != null) { // Using the parseInt method to convert the string to an int
int value = Integer.parseInt(sensorValue);
println(value);
}
}
This returns an error :
NumberFormatException: for input String "621"
621 is the value read, so we understand it as "the program can't convert the string even if it can read it"... This is confusing as well =/
NumberFormatException seems to be thrown if the
String does not contain a parsable
integer
void draw() { //reading serial port until \n String sensorValue = myPort.readStringUntil(13); if (sensorValue != null) { int value;
//Using the getInt(string) method to convert the string to an int value = getInt(sensorValue); println(value); } }
This throws a different kind of error :
The function
getInt(String)does not exist.
Well, this is quite not what the refence page says, at least the way we understand it.... ??
So here we are today.... It's a killer issue, our project is quite dead without the possibily to use the value.
It seems like the majority of conversion problems posted online have been solved with the
Integer.parseInt(); method, but anyway, any help will be most welcome !
PS : sorry for the possible grammar errors, we are french ^^