Radio code (sound and Arduino)

Hello, I need to code a program that retrieves a value from the Arduino card and plays a sound based on that value.
The value varies according to the position of a potentiometer: an Arduino program transforms the voltage across the potentiometer into 3 digital values: 1, 2 and 3.
The Processing program must play sound 1 when the recovered value is 1, play sound 2 when the recovered value is 2 and play sound 3 when the value is 3.
I must point out that when you switch from sound 1 to sound 2 (or from sound 2 to 3, etc.), sound 1 is not stopped but only muted. Once the sound has been played in its entirety, it must be automatically restarted.
For the moment I have written this program, but it doesn’t work.
Could someone please help me?
I am French so sorry for my English.

My code :

import processing.serial.*;
import ddf.minim.*; //Import de la bibliothèque "minim" pour lecture des sons
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;


Serial port;            //L'objet "port" qui gère la lcture des sons
Minim minim;            //L'objet "minim" qui gère la lcture des sons
AudioPlayer radio1;     //Objet contenant les données audio de la premiere radio
AudioPlayer radio2;     //Objet contenant les données audio de la deuxieme radio
AudioPlayer radio3;     //Objet contenant les données audio de la troisieme radio

int value=0; //valeur lue sur le port série, envoyé par la carte Arduino

void setup() {
    size(800, 600); //Crée une fenêtre vierge de 800*600 pixels
    port = new Serial(this, "COM7", 9600); // !!!!!!!!! A CHANGER   !!!!!!!!!!!!!
    port.bufferUntil('\n'); //Attendre arrivée d'un saut de ligne pour générer évènement série
    minim = new Minim(this);
    radio1 = minim.loadFile("radio1.mp3");
    radio2 = minim.loadFile("radio2.mp3");
    radio3 = minim.loadFile("radio3.mp3");
}

void draw() {
    background(0,60,60); //Couleur de l'arrière plan en RVB
    radio();
    delay(100);
}

void radio(){
  println(value);
  if (!radio1.isPlaying() && value==1){
       radio1.rewind();
       radio1.play();
    }
  if (!radio2.isPlaying() && value==2){
       radio1.rewind();
       radio1.play();
    }
  if (!radio3.isPlaying() && value==3){
       radio1.rewind();
       radio1.play();
    }
}

void serialEvent(Serial port){
    String serialStr = port.readStringUntil('\n');
    serialStr=trim(serialStr);
    int values[]=int(split(serialStr,','));
    if(values.length==2){
      value=values[1];
    }
    
}
1 Like

Can you share the program that you have written?

Paste your code, then format it with the button: </>

possibly you want concentrate on a processing code and make a good show,
so you not too fit / or even interested into

  • arduino code and
  • tricky serial communication

i can suggest to use
GitHub - firmata/arduino: Firmata firmware for Arduino ,
means you just upload a ready sketch to arduino,
and on processing side start with a example from lib
Arduino Playground - HomePage
install lib: Arduino Firmata v9 ( and close open IDE )

Files / Examples / Contributed Libraries /

Arduino ( Firmata ) / arduino input /

as a start i cut it down

/*arduino_input*/
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;

void setup() {
  size(470, 280);
  println(Arduino.list());  
  // Modify this line, by changing the "0" to the index of the serial
  // port corresponding to your Arduino board (as it appears in the list
  // printed by the line above).
  arduino = new Arduino(this, Arduino.list()[0], 57600);
}

void draw() {
   println(arduino.analogRead(0));  //Ain 0
}

so you get integer numbers 0 … 1023 ( range of arduino Ain )
and in processing can do with it what you want.

example:
arduino example select:


arduino upload:

new processing code with select 3

/*arduino_input*/
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
String arduinoport = Arduino.list()[1];
int sel, vAin0;

void setup() {
  size(1024, 280);
  println(Arduino.list());  
  // Modify this line, by changing the "0" to the index of the serial
  // port corresponding to your Arduino board (as it appears in the list printed by the line above).
  println("try connect to "+arduinoport);
  arduino = new Arduino(this, arduinoport, 57600);
}

void draw() {
  background(200, 200, 0);
  arduino_read();
  select_of_three();
}

void select_of_three() {
  int lim12 = 340, lim23 = 680;
  if ( vAin0 >= 0     && vAin0 < lim12 ) sel = 0; 
  if ( vAin0 >= lim12 && vAin0 < lim23 ) sel = 1; 
  if ( vAin0 >= lim23 && vAin0 < 1024 )  sel = 2;
  fill (0,200,0);
  stroke(0,0,200);
  rect (sel * lim12, 10, lim12, height-40);
}

void arduino_read() {
  vAin0 = arduino.analogRead(0);  //Ain 0
  stroke(200, 0, 0);
  line( vAin0, height-10, vAin0, height-20);
  if ( keyPressed ) println("read vAin0: "+vAin0);
}

1 Like

Oh yes sure i forgot… sorry

I have a Arduino program and it works :

const int potar = A0;
int value,valueMap;

void setup() {
  pinMode(potar,INPUT);
  Serial.begin(9600);
}

void loop() {
  value = analogRead(potar);
  valueMap = map(value,0, 1023, 1, 3);
  Serial.println(valueMap);
  delay(100);
}

It returns values 1, 2 and 3, because i have three sounds.

ok, very good, was just a idea to help.

now i see your processing code,
can you pls repair this post using the

</> code formatter

ok, and what is the question?
why you not use a

String serialStr = port.readStringUntil(’\n’);
println(serialStr);

to understand what is wrong?

if i look your arduino code ( what actually not send a good CSV line )
value=values[0];
might work

Thank’s for your responses !
Sorry for the code, It is formated now.

I didn’t use this command because I don’t know this language… eheh
The program that I have was not made by me, but I try to understand it maximally, I do my best :slight_smile:

I’ll try this command this night.

I added the 2 lines of code, and it seems that the value that comes out of the Arduino card is not retrieved by Processing (“null”).
However, the port used is COM7… I don’t understand.

i expected you to add this print inside your

void serialEvent(Serial port){
    String serialStr = port.readStringUntil('\n');
    serialStr=trim(serialStr);
    println(serialStr);     // this shows the RAW data ( and the usual timeout "null" )
    int values[]=int(split(serialStr,','));
    println(" how long "+values.length);  // this shows if the following == 2 makes any sense
    if(values.length==2){
     printArray(values);   // this shows where possible data end up
      value=values[1];    // here i suggested a [0]
    }
    
}

if anything is your screenshot is correct, your arduino send “0”
what is not according your arduino code: only “1” “2” “3” possible???


did you test my proposed example? arduino firmata and my processing firmata sketch?

    • no arduino code problems
    • no serial code problems
    • easy processing code, just get a integer number you can map in processing.
2 Likes

Thank you so much guy !
You made me understand all the lines of this program (especially for the SerialEvent function). It was Chinese for me but now I understand everything and I succeeded what I wanted to do, here is the final program (I just added more audio files) :

import processing.serial.*;
import ddf.minim.*; //Import de la bibliothèque "minim" pour lecture des sons
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;


Serial port;            //L'objet "port" qui gère la lcture des sons
Minim minim;            //L'objet "minim" qui gère la lcture des sons
AudioPlayer radio1;     //Objet contenant les données audio de la premiere radio
AudioPlayer radio2;     //Objet contenant les données audio de la deuxieme radio
AudioPlayer radio3;     //Objet contenant les données audio de la troisieme radio
AudioPlayer bruitage;

int value=0; //valeur lue sur le port série, envoyé par la carte Arduino

void setup() {
  background(0, 60, 60); //Couleur de l'arrière plan en RVB
  size(200,200);
  port = new Serial(this, Serial.list()[0], 9600); // !!!!!!!!! A CHANGER   !!!!!!!!!!!!!
  port.bufferUntil('\n'); //Attendre arrivée d'un saut de ligne pour générer évènement série
  minim = new Minim(this);
  radio1 = minim.loadFile("radio1.mp3");
  radio2 = minim.loadFile("radio2.mp3");
  radio3 = minim.loadFile("radio3.mp3");
  bruitage = minim.loadFile("bruitage.mp3");
}

void draw() {
  radio();
  delay(100);
}

void radio() {
  println("valeur" + value);
  if (!radio1.isPlaying() && value==1) {
    radio3.pause();
    radio2.pause();
    bruitage.pause();
    radio1.rewind();
    radio1.play();
  }
  if (!bruitage.isPlaying() && value==2) {
    radio1.pause();
    radio2.pause();
    radio3.pause();
    bruitage.rewind();
    bruitage.play();
  }
  if (!radio2.isPlaying() && value==3) {
    radio1.pause();
    radio3.pause();
    bruitage.pause();
    radio2.rewind();
    radio2.play();
  }
  if (!bruitage.isPlaying() && value==4) {
    radio1.pause();
    radio2.pause();
    radio3.pause();
    bruitage.rewind();
    bruitage.play();
  }
  if (!radio3.isPlaying() && value==5) {
    radio1.pause();
    radio2.pause();
    bruitage.pause();
    radio3.rewind();
    radio3.play();
  }
}

void serialEvent(Serial port){
    String serialStr = port.readStringUntil('\n');
    serialStr=trim(serialStr);
    int values[]=int(split(serialStr,','));
    if(values.length==1){
      value=values[0];    // here i suggested a [0]
      println(value);
    }
}

2 Likes

good, and your chinese ( processing fu ) is much better now.

but may i suggest
+add+ diagnostic print disable:

boolean diagp = false;

void serialEvent(Serial port){
    String serialStr = port.readStringUntil('\n');
    serialStr=trim(serialStr);
    if ( diagp ) println(serialStr);     // this shows the RAW data ( and the usual timeout "null" )
    int values[]=int(split(serialStr,','));
    if ( diagp ) println(" how long "+values.length);  // this shows if the following == 2 makes any sense
    if(values.length > 0 ){  // keep it open for later improvements
     if ( diagp) printArray(values);   // this shows where possible data end up
      value=values[0];
      println(value);
    }
}

and thanks for sharing the running code.

1 Like

I have a question, where can I get library cc.arduino for processing ? I see that you install library Firmata for arduino, but library cc.arduino I can`t find nowhere

1 Like

you start Processing IDE ( now 3.5.3 )
Tools / Add Tool… /
(TAB) Libraries / Arduino ( Firmata )
now rev 9

https://playground.arduino.cc/Interfacing/Processing/

minimal starting code

import processing.serial.*;
import cc.arduino.*;
Arduino arduino;

void setup() {
  println(Arduino.list());
  arduino = new Arduino(this, Arduino.list()[0], 57600);
}

make sure on the port ( list[0] ) a arduino is connected
were you have loaded the Firmata code!


if you want work with your own arduino code ( not firmata (protocol ))
pls see
https://processing.org/reference/libraries/serial/index.html

1 Like