0

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");
  • Read [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – PakkuDon Mar 16 '14 at 20:55
  • My bad, in the code the == is correct I just wrote it wrong in the title of the post –  Mar 16 '14 at 20:58
  • oh whoops hold on a sec messing with equals() –  Mar 16 '14 at 21:00
  • If I use equals() it plays the sound once, and then crashes. Does anyone see why the loop might be going out of bounds? –  Mar 16 '14 at 21:09
  • Show us the exception. A wild guess at this point is that you're trying to store multiple keypresses in your array, but without an error message, your guess is as good as mine. – MarsAtomic Mar 16 '14 at 21:13
  • Can you ensure the "myString" is not null as well as empty? if (myString != null && !myString.isEmpty()) { – JBuenoJr Mar 16 '14 at 21:15
  • I tried the !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. updating post –  Mar 16 '14 at 21:24

1 Answers1

0

Figured it out -

For some reason the sound needs to be rewinded!! (which to me is strange since it isn't a vhs hahaha but ok!)

Basically we added

soundclip.rewind();

for each sound clip and now it's working.

Thanks for your help!