We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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"); } }
Answers
Check this code for Arduino
And this for Processing: