Hi, Ive been trying to send a string value from Arduino to Processing but I keep getting a NullPointerException when it gets to this line
if (val.equals("hola"))
{
//file.play();
println("Funciona");
}
I know the value gets store in the val variable but for some reason when it tries to compare the val value with whats inside the .equals it throws the NullPointerException. Ive tried looking to a bunch of tutorial with no success. Im sure its a pretty simple mistake but i dont know enough about processing to really get it. Any help would be appreciated. I was following the first part of this tutorial https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all and tried to mix it with the equals() function.
This is my Arduino Code:
int fotoCelda1;
boolean fotoceldaDown = 0;
void setup()
{
Serial.begin(9600);
Serial.println("inicial");
}
void loop()
{
fotoCelda1 = analogRead(0);
if (fotoCelda1 < 800)
{
fotoceldaDown = true;
}
else
{
fotoceldaDown = false;
}
if (fotoceldaDown)
{
Serial.println("hola");
delay(300);
fotoceldaDown = false;
}else
{
Serial.println("chao");
delay(300);
}
}
This is my Processing code:
import processing.serial.*;
Serial myPort; // Create object from Serial class
String val=" "; // Data received from the serial port
import processing.sound.*;
SoundFile file;
void setup() {
size(640, 360);
background(255);
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
myPort.bufferUntil('\n');
file = new SoundFile(this, "Cucarachero.mp3");
}
void draw() {
if ( myPort.available() > 0)
{
val = myPort.readStringUntil('\n');
}
println(val);
if (val.equals("hola"))
{
//file.play();
println("Funciona");
}
else
{
println("no entre en panico");
}
}
Thank you again!