Can't Get Music to Play from Serial

edited February 2017 in Arduino

Hi,

I have written the below program to get a serial input to play a sound. However, the serial variable changes but does not play the sound. If I set the serial variable to any of the desired if values the sound will play, so playback is not the problem. If anyone can assist it would be greatly appreciated.

/* ARDUINO TO PROCESSING

Read Serial messages from Arduino for use in Processing. Even though Serial Library comes with install of Processing, upon first usage, you may be prompted to execute two sudo Terminal commands after entering your user password

Created by Daniel Christopher 10/27/12 Public Domain

*/

import processing.serial.*; //import the Serial library import processing.sound.*;

SoundFile file1; SoundFile file2; SoundFile file3; SoundFile file4;

int end = 10; // the number 10 is ASCII for linefeed (end of serial.println), later we will look for this to break up individual messages String serial = null; // declare a new string called 'serial' . A string is a sequence of characters (data type know as "char") Serial port; // The serial port, this is a new instance of the Serial class (an Object) String val; String A = "A"; String B = "B"; String C = "C"; String D = "D";

void setup() { size(640, 360); background(128); file1 = new SoundFile(this, "scream1.mp3"); file2 = new SoundFile(this, "scream2.mp3"); file3 = new SoundFile(this, "scream3.mp3"); file4 = new SoundFile(this, "scream4.mp3");

String portName = Serial.list()[2]; port = new Serial(this, portName, 9600); // initializing the object by assigning a port and baud rate (must match that of Arduino) port.clear(); // function from serial library that throws out the first reading, in case we started reading in the middle of a string from Arduino serial = port.readStringUntil(end); // function that reads the string from serial port until a println and then assigns string to our string variable (called 'serial') serial = null; // initially, the string will be null (empty)

}

void draw() { while (port.available() > 0) { //as long as there is data coming from serial port, read it and store it

serial = port.readStringUntil(end); println(serial); delay(1000); //file4.play(); if (serial.equals(A) == true) { //if the string is not empty, print the following file1.play(); println("serialA");

}
else if (serial.equals(B) == true){
  file2.play();
  println("serial");

}

else if (serial.equals(C) == true){
  file3.play();
  println("serial");

}

else if (serial.equals(D) == true){
  file4.play();
  println("serial");

}
else if (serial.equals(null)  == true){
  delay(100);
}


}

}

Answers

  • edited February 2017

    == true is not needed try this:

    if(serial.equals(A +'\n'))

    else if(serial.equals(B +'\n'))

    etc..

    Because you're using Serial.println() on the Arduino side the string returned to the Processing side has the last character as a linfeed ('\n').

  • @mj10021

    Please edit your post and format your code. Select your code and press ctrl+o and have an empty line above and below your code.

    Kf

Sign In or Register to comment.