Two buttons cueing two audio files with Minim

edited November 2017 in Arduino

I'm a noob trying to get two switch buttons on an Arduino to be able to repeatedly cue two audio samples in Processing. I can get the audio files to play once each, and I imagine I need to reload the player after it finishes playing, but when I try to add player = minim.loadFile("pool splashing.mp3"); to the end of the if statement that plays the file, it makes the audio terribly distorted and slow. Something is definitely wrong in my code. Strangely, when I add player2 = minim.loadFile("nature sounds.mp3"); to the second if statement that plays it, the button no longer works. What am I missing? Thanks in advance.

Arduino code:

int switchPin1 = 4;                      
int switchPin2 = 2;
int buttonState = 0;  

void setup() {

  pinMode(switchPin1, INPUT_PULLUP);             
  pinMode(switchPin2, INPUT_PULLUP);             
  Serial.begin(9600);                   
}

void loop() {

  if (digitalRead(switchPin1) == LOW) {  
    Serial.println("1");              
  }

  if (digitalRead(switchPin2) == LOW) { 
    Serial.println("2");            

  delay(100);                           
}

and my Processing code:

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

Serial myPort; 
Minim minim;
AudioPlayer player;
AudioPlayer player2;
String val;     // data received from the serial port


void setup(){
  size(500,500);
  myPort = new Serial(this, "/dev/cu.usbmodem1411", 9600);
  myPort.buffer(1);
  minim = new Minim(this);
  player = minim.loadFile("pool splashing.mp3");
  player2 = minim.loadFile("nature sounds.mp3");

}

void draw(){

}

void serialEvent (Serial myPort){
println(val);
if ( myPort.available() > 0)
 {  // if data is available,
  val = myPort.readString();         // read it and store it in val
  }
  println(val); //print it out in the console

    if(val.equals("1")){
      player.play();
      println("song1");

    }

    if(val.equals("2")){
      player2.play();
      println("song2");
    }
}
Tagged:

Answers

  • Check this code for Arduino

    const int inputPin_1 = 2;  //pin for pullup resistor
    const int inputPin_2 = 4;  //pin for pullup resistor
    
    int value_1 = 0;
    int value_2 = 0;
    
    int lastvalue_1;
    int lastvalue_2;
    
    void setup() {
    
    Serial.begin(9600);      //Start serial communication
    
    }
    
    void loop(){
    
      value_1 = digitalRead(inputPin_1);  //read the digital input
      value_2 = digitalRead(inputPin_2);  //read the digital input
      Song_1();
      Song_2();
    
    }
    
    void Song_1(){  
    
      if (value_1 != lastvalue_1) {
        if (value_1 == LOW) {
            Serial.println("1");
            Serial.println("Sent!");
            delay(2000);
        }
      }  
    
      lastvalue_1 = value_1;
    
    }
    
      void Song_2(){  
    
        if (value_2 != lastvalue_2) {
          if (value_1 == LOW) {
            Serial.println("2");
            Serial.println("Sent!");
            delay(2000);
          }
        }  
    
      lastvalue_2 = value_2;
    
    }
    

    And this for Processing:

    import processing.serial.*;
    import ddf.minim.*;
    
    Serial myPort; 
    Minim minim;
    AudioPlayer player;
    AudioPlayer player2;
    String val;     // data received from the serial port
    
    void setup(){
    size(500,500);
    //myPort = new Serial(this, "/dev/cu.usbmodem1411", 9600);
    String portName = Serial.list()[2]; 
    //change the 0 to a 1 or 2 etc. to match your port
    myPort = new Serial(this, portName, 115200);
    myPort.buffer(1);
    minim = new Minim(this);
    player = minim.loadFile("Know.mp3");
    player2 = minim.loadFile("Sugar.mp3");
    }
    
    void draw(){
    cancion();
    }
    void cancion(){  
    println(val);
    if ( myPort.available() > 0){  // If data is available,
    val = myPort.readString();         // read it and store it in val
    } 
    
    if(val == "1"){
      player.play();
      println("song1"); 
    }
    
    if(val == "2"){
      player2.play();
      println("song2");
    }
    else{    
          println("null");
    }
    }
    
Sign In or Register to comment.