Convertir String a Int

Hola, tengo un sketch de processing que recibe data de un sensor (on/off) conectado a un arduino. El tema es que los datos enviados de arduino son strings y necesito que sean int. Estoy intentando convertirlos pero me tira error, alguien sabe porque?

Adjunto una imagen del error

codigo arduino

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
  Serial.begin(9600);  
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
    Serial.println("1.");
    delay(250);
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
    Serial.println("0.");
    delay(250);
  }
//  Serial.println();
//  //Serial.print("el boton esta en: ");
//  Serial.print(buttonState);

  //delay(400);
}

codigo processing

[code]// Example by Tom Igoe 

import processing.serial.*; 

Serial myPort;    // The serial port
PFont myFont;     // The display font
String inString;  // Input string from serial port
int lf = 10;      // ASCII linefeed 
int convertido = 123;

void setup() { 
  size(400, 200); 
  // You'll need to make this font with the Create Font Tool 
  myFont = loadFont("Serif-48.vlw"); 
  textFont(myFont, 18); 
  // List all the available serial ports: 
  println(Serial.list()); 
  // I know that the first port in the serial list on my mac 
  // is always my  Keyspan adaptor, so I open Serial.list()[0]. 
  // Open whatever port is the one you're using. 
  myPort = new Serial(this, Serial.list()[4], 9600); 
  myPort.bufferUntil(lf);
} 

void draw() { 
  background(0); 

  String texto = inString;
  convertido = int(texto);

  text("received: " + inString, 10, 50); 
  text("converted: " + convertido, 10, 80); 



  if (inString == "1.") {
    fill(255);
    ellipse(width/2, height/2, width/3, height/3);
  }
} 

void serialEvent(Serial p) { 
  inString = p.readString();
}

[/code]

El error en este que pones acá creo que es porque estás queriendo convertir "1." a un entero, y eso genera un error. No estoy seguro, pero juraría que es por ese punto.

Hola,
...¿y no podrías poner en el metodo draw() un IF? ....es decir, si puedes predecir la respuesta del inString, pues creas otra variable con el valor que deseas implmentar... no se si me explico:
void draw() {
bacjground(0);
text("recived: " + inString, 10,50);
if (inString == "1"){
int convertido = 1;
}
.....
}

La verdad es que desconozco Processing y desde hace poco estoy intentando aprender Java, ...así que disculpa si he sugerido una cosa sin sentido....

Salu2!

El problema era que estaba mandando mal el dato desde arduino, me venia el dato con u salto de linea, por eso no lo convertia bien a int!

Hola,

Para ahorrarte complicaciones puedes utilizar directamente:

Serial.Write(0);//Para escribir el número 0
Serial.Write(1);//Para escribir el número 1

Saludos.