Edit: If I use equals() to compare strings, it plays the sound once, and then crashes. Does anyone see why the loop might be going out of bounds?
code updated
Edit 2: I tried inserting !myString.isEmpty and now it is no longer crashing, but it still only plays each sound once and will not play again after the first iteration.
We are trying to get a sound to play for a toy when the length of a button press is equal to a certain amount. Printing wise, the responses are as expected, (at least for short button presses) and it will print out that the array[1] spot is equal to "S", however for some reason this doesn't trigger our if function that should cause a sound to play when array[1] is equal to "S". Any ideas?
It is also, for some reason, not reading "L" or long button presses. It's driving us crazy we've been messing with it for like three hours! What do you smart guys think?
Java code from processing:
import processing.serial.*; //accepts serial input
import ddf.minim.*; //imports minim sound library
Minim minim;
String [] array;
Serial arduinoPort; //reads serial from arduino
AudioPlayer au_player1, au_player2, au_player3; //initializes sound files, au_player(n) (n=number of wav files)
void setup() {
minim = new Minim(this);
au_player1 = minim.loadFile("comply_not.wav");
au_player2 = minim.loadFile("go_away.wav");
String portName = Serial.list()[2];
arduinoPort = new Serial(this, portName, 9600);
}
void draw() {
while (arduinoPort.available () > 0) {
String myString = arduinoPort.readStringUntil('\n');
if ((myString != null)&&(!myString.isEmpty)) {
// println(myString);
printArray(array);
array = split(myString, ",");
if (array[1].equals("S")) {
au_player1.play();
}
else if (array[1].equals("L")) {
au_player2.play();
}
delay(100);
}
}
}
void stop() {
minim.stop();
super.stop();
}
Arduino Code:
Essentially, this just reads in the button presses and then is sent over to processing which is all in Java so if you don't know the arduino stuff that's not a big deal. I think it's something to do with the Java.
#define BUTTON_PIN 2 // Button
#define LONGPRESS_LEN 25 // Min nr of loops for a long press
#define DELAY 100 // Delay per loop in ms
Serial.print("No");
Serial.print(",");
Serial.print("No");
Serial.print(",");
Serial.println("No");
break;
case EV_SHORTPRESS:
Serial.print(",");
Serial.print("S");
Serial.print(",");
Serial.println("No");
break;
case EV_LONGPRESS:
Serial.print(",");
Serial.print("L");
Serial.print(",");
Serial.println("No");