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]