# Arduino - processing: serial data to toggle mp3 file in minim

**URL:** https://discourse.processing.org/t/arduino-processing-serial-data-to-toggle-mp3-file-in-minim/17068
**Category:** Electronics (Arduino, etc.)
**Created:** [January 13, 2020, 4:44pm UTC](https://discourse.processing.org/t/arduino-processing-serial-data-to-toggle-mp3-file-in-minim/17068 "2020-01-13T16:44:18Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![clara\_h](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@clara\_h](https://discourse.processing.org/u/clara_h)
#### Post date: [January 13, 2020, 4:44pm UTC](https://discourse.processing.org/t/arduino-processing-serial-data-to-toggle-mp3-file-in-minim/17068/1 "2020-01-13T16:44:18Z")

</div>

Hi,

I’m sending data through the serial port to toggle a sound file on and off in minim.

The string data is all being read correctly in Processing, but the code isn’t accessing the if loops for some reason??

here’s the code:

```auto
import processing.serial.*;
import ddf.minim.*;
//import processing.video.*; 

Minim minim;

AudioPlayer player;
Serial myPort;
String val = null;    

void setup() {
  minim = new Minim(this);
  size(640,480);
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  player = minim.loadFile("groove.mp3");
}
 
void serialEvent (Serial myPort) {
  while(myPort.available() > 0) {
    val=myPort.readStringUntil('\n');

    if(val != null){
      val = val.trim(); // let's remove whitespace characters
      if (val.equals("L")) {
        if(player.isPlaying() == true){
          player.pause();
        }
      }
    
      else {
        if(player.isPlaying() == false){
          player.play();
        }
      }
    } 
  }
}

void draw() {
  background(0);
  println(val);
} 

```

Anyone that could possibly help with this?

---

<div class="post-metadata">

### Author: ![Nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nathan](https://discourse.processing.org/u/Nathan)
#### Post date: [January 13, 2020, 10:52pm UTC](https://discourse.processing.org/t/arduino-processing-serial-data-to-toggle-mp3-file-in-minim/17068/2 "2020-01-13T22:52:43Z")

</div>

For starters, you probably should fix those import statements to be:

```auto
import processing.serial.*;
import ddf.minim.*;

```

It may have nothing to do with your problem, but neither processing.serial or ddf.minim are things you can import, they’re just package names.

So it’s printing out the values correctly that your Arduino board is sending? Have you tried throwing in a few println statements to see whether it’s getting into the if blocks? Do you have a playable file called ‘groove.mp3’?

P.S.  
You can also the ‘\</\>’ button in the post editor to have code shown properly, or place the code in between two lines of triple backticks ( ``` ).

---

<div class="post-metadata">

### Author: ![clara\_h](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@clara\_h](https://discourse.processing.org/u/clara_h)
#### Post date: [January 15, 2020, 11:17am UTC](https://discourse.processing.org/t/arduino-processing-serial-data-to-toggle-mp3-file-in-minim/17068/3 "2020-01-15T11:17:10Z")

</div>

Thanks for the reply.

Yes, there is a file playing called ‘groove.mp3’ It plays within the else loop. The console prints ‘status: Q’ or ‘status: L’ depending on the input from the Arduino.  
I have tried println to the console from within the if block: if (val.equals(“L”)) {… and nothing prints out.

Any ideas?

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [January 15, 2020, 11:34am UTC](https://discourse.processing.org/t/arduino-processing-serial-data-to-toggle-mp3-file-in-minim/17068/4 "2020-01-15T11:34:26Z")

</div>

Hello,

Processing has an **Auto format** in the _Edit_ tab.

Also (in addition to above), please format your code for posting and I will look at it and try it.  
See section " Is your post formatted?":

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-tips-on-asking-questions/2147):
>
> Summary (TL;DR) Ask complete questions to get better answers! Be specific. For example: I want to load an image. I tried using createImg() , and I expected the image to be on canvas, but what happened instead was the image showing up below the canvas. Isolate your problem and work in small steps. Share only the code directly related to your problem if it is part of a bigger project, and if something isn’t working, try the smallest code that could do the thing you want. Can we run your code to …

🙂

---

<div class="post-metadata">

### Author: ![clara\_h](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@clara\_h](https://discourse.processing.org/u/clara_h)
#### Post date: [January 15, 2020, 11:56am UTC](https://discourse.processing.org/t/arduino-processing-serial-data-to-toggle-mp3-file-in-minim/17068/5 "2020-01-15T11:56:17Z")

</div>

Thank you.

Here is the code:

```auto
import processing.serial.*;
import ddf.minim.*;
//import processing.video.*; 

Minim minim;

AudioPlayer player;
Serial myPort;
String val = null;    

void setup() {
  minim = new Minim(this);
  size(640, 480);
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  player = minim.loadFile("groove.mp3");
}

void serialEvent (Serial myPort) {
  while (myPort.available() > 0) {
    val=myPort.readStringUntil('\n');

    if (val != null) {
      val = val.trim(); // let's remove whitespace characters
      if (val.equals("L")) {
        if (player.isPlaying() == true) {
          player.pause();
          println("in the Loud if statement");
        }
      }
      //} else {
        if (player.isPlaying() == false) {
          player.play();
          println("in the Quiet if statement");
          //println(val);
        }
      }
    }
  }
}

void draw() {
  background(0);
  println(val);
} 

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 15, 2020, 12:02pm UTC](https://discourse.processing.org/t/arduino-processing-serial-data-to-toggle-mp3-file-in-minim/17068/6 "2020-01-15T12:02:20Z")

</div>

> [@clara\_h](#):
>
> The console prints ‘status: Q’ or ‘status: L’ depending on the input from the Arduino.

the posted code would never do that, unless you send  
`status: X` from arduino, and then the if equals “X” will never work.

you not posted the arduino code,  
not posted the arduino IDE Montor print  
not posted the console print

so lots of guessing here

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [January 15, 2020, 6:58pm UTC](https://discourse.processing.org/t/arduino-processing-serial-data-to-toggle-mp3-file-in-minim/17068/7 "2020-01-15T18:58:00Z")

</div>

Hello,

This may help:

> [@Processing to Arduino Serial Library Example](https://discourse.processing.org/t/processing-to-arduino-serial-library-example/11143/5):
>
> I modified the existing “SimpleWrite” example to: save and check for the state of mouseOverRect() print state to console only writes serial data once a delay() to wait for Arduino to reset once connected to it; in my case, it is an Arduino Mega 2560 R3. I kept the spirit of the original example intact and left it as a “Simple Write” program. My edit of “SimpleWrite” example so it only sends data once: /\*\* \* Simple Write. \* \* Check if the mouse is over a rectangle and writes the status …

🙂

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 16, 2020, 8:13am UTC](https://discourse.processing.org/t/arduino-processing-serial-data-to-toggle-mp3-file-in-minim/17068/8 "2020-01-16T08:13:15Z")

</div>

tested code

> **example**
>
> ```auto
> 
> import processing.serial.*;
> Serial myPort;
> String data;    
> boolean diag = true; // __________________________________ print diagnostic info
> 
> import ddf.minim.*;
> Minim minim;
> AudioPlayer player;
> String song = "data/groove.mp3"; // ______________________ select song
> 
> void setup_serial() { // _________________________________ USB arduino..
> printArray(Serial.list());
> String portName = Serial.list()[0]; // _________________ adjust 0.. x port
> myPort = new Serial(this, portName, 115200);
> myPort.clear();
> myPort.bufferUntil('\n');
> println("try connect to "+portName);
> }
> 
> void serialEvent(Serial p) { // __________________________ handle serial data
> data = trim(p.readStringUntil('\n'));
> if (data != null) {
> if (diag) println(data); // __________________________ print every GOOD line
> if (data.equals("L")) song_play(true); // ____________ start
> if (data.equals("Q")) song_play(false); // ___________ stop
> }
> }
> 
> void song_play(boolean on) { // __________________________ PLAY / PAUSE
> if ( !on && player.isPlaying() ) player.pause(); //___ stop
> if ( on && !player.isPlaying() ) player.loop(); // ____ start [l] 'L'
> }
> 
> void setup() {
> size(640, 480);
> minim = new Minim(this);
> player = minim.loadFile(song);
> setup_serial();
> println("we expect keyboard [l] or [q] or USB line 'L' or 'Q' ");
> }
> 
> void draw() {} 
> 
> void keyPressed() {
> if ( key == 'l' ) song_play(true); // __________________ start
> if ( key == 'q' ) song_play(false); // _________________ stop
> }
> 
> // arduino code tested with arduino IDE 1.8.10 hourly and Board Leonardo
> 
> /*
> 
> int dt = 1000; // msec between sending
> 
> void setup() {
> Serial.begin(115200);
> }
> 
> void loop() {
> if ( analogRead(0) > 512 ) Serial.println("L");
> else Serial.println("Q");
> delay(dt); // sample rate
> }
> 
> */
> 
> ```
> 
> prints
> 
> ```auto
> [0] "COM1"
> [1] "COM7"
> try connect to COM7
> we expect keyboard [l] or [q] or USB line 'L' or 'Q' 
> L
> L
> 
> ```

---

<div class="post-metadata">

### Author: ![clara\_h](https://avatars.discourse-cdn.com/v4/letter/c/b487fb/32.png) [@clara\_h](https://discourse.processing.org/u/clara_h)
#### Post date: [January 16, 2020, 11:20am UTC](https://discourse.processing.org/t/arduino-processing-serial-data-to-toggle-mp3-file-in-minim/17068/9 "2020-01-16T11:20:38Z")

</div>

Hi again.  
Thank you very much for this.  
I have tested your processing code example and getting this error message in the console:

```auto
==== JavaSound Minim Error ====
==== java.io.FileNotFoundException: data/groove.mp3

=== Minim Error ===
=== Couldn't load the file data/groove.mp3

try connect to /dev/cu.usbmodem1421
we expect keyboard [l] or [q] or USB line 'L' or 'Q' 
Q
Error, disabling serialEvent() for /dev/cu.usbmodem1421
null

```

This the Processing code I’m using:

```auto
import processing.serial.*;
import ddf.minim.*;
//import processing.video.*; 

Minim minim;

AudioPlayer player;
Serial myPort;
String val = null;    

void setup() {
  minim = new Minim(this);
  size(640, 480);
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  player = minim.loadFile("data/groove.mp3");
}

void serialEvent (Serial myPort) {
  while (myPort.available() > 0) {
    val=myPort.readStringUntil('\n');

    if (val != null) {
      val = val.trim(); // let's remove whitespace characters
      if (val.equals("L")) {
        if (player.isPlaying() == true) {
          player.loop();
          println("in the Loud if statement");
        }
      }
      //} else {
        if (player.isPlaying() == false) {
          player.pause();
          println("in the Quiet if statement");
          //println(val);
        }
      }
    }
  }

void draw() {
  background(0);
  println(val);
} 

```

With this Arduino code (board Leonardo):

```auto
// Define hardware connections
#define PIN_GATE_IN 2
#define IRQ_GATE_IN 0
#define PIN_LED_OUT 13
#define PIN_ANALOG_IN A0

// soundISR()
// This function is installed as an interrupt service routine for the pin
// change interrupt. When digital input 2 changes state, this routine
// is called.
// It queries the state of that pin, and sets the onboard LED to reflect that 
// pin's state.
void soundISR()
{
  int pin_val;

  pin_val = digitalRead(PIN_GATE_IN);
  digitalWrite(PIN_LED_OUT, pin_val);   
}

void setup()
{
  Serial.begin(9600);

  // Configure LED pin as output
  pinMode(PIN_LED_OUT, OUTPUT);

  // configure input to interrupt
  pinMode(PIN_GATE_IN, INPUT);
  attachInterrupt(IRQ_GATE_IN, soundISR, CHANGE);

  // Display status
  Serial.println("Initialized");
}

void loop()
{
  int value;

  // Check the envelope input
  value = analogRead(PIN_ANALOG_IN);

  // Convert envelope value into a message
  //Serial.print("Status:");
  if(value <= 10)
  {
    Serial.println("Q");
  }
  else
  {
    Serial.println("L");
  }

  // pause for 0.5 second
  delay(500);
}

```

is printing ‘Q’ and ‘L’ depending on the Arduino input, however the printLn (“in the Quiet if statement”) only gets through rarely.

```auto
L
L
L
L
in the Quiet if statement
L
L
L
L

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 16, 2020, 11:48am UTC](https://discourse.processing.org/t/arduino-processing-serial-data-to-toggle-mp3-file-in-minim/17068/10 "2020-01-16T11:48:50Z")

</div>

i modified your serial event code until it worked here, so i will not  
again work with yours.

first, the minim error ?  
i expect that files are under data/…  
and processing can read from sketchpath and datapath,  
but i recommend to use absolute path,

you could however change to

```auto
String song = "groove.mp3"; // ______________________ select song

```

* * *

other thing is that i recommended with my code example to  
separate the song logic from the serial logic,  
in my example then also can start from keyboard,  
using the same loop/pause logic

* * *

why you not repaired the TOP POST CODE posting?
