# Radio code (sound and Arduino)

**URL:** <https://discourse.processing.org/t/radio-code-sound-and-arduino/9175>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [March 10, 2019, 4:36pm UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175 "2019-03-10T16:36:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mimal](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@Mimal](https://discourse.processing.org/u/Mimal)\
**Post date:** [March 10, 2019, 4:36pm UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175/1 "2019-03-10T16:36:49Z")

</div>

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 :

```auto
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];
    }
    
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 10, 2019, 4:38pm UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175/2 "2019-03-10T16:38:31Z")

</div>

> [@Mimal](#):
>
> For the moment I have written **this program** , but it doesn’t work.

Can you share the program that you have written?

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

---

<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:** [March 11, 2019, 2:53am UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175/3 "2019-03-11T02:53:17Z")

</div>

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](https://github.com/firmata/arduino) ,  
means you just upload a ready sketch to arduino,  
and on processing side start with a example from lib  
[Arduino Playground - HomePage](http://playground.arduino.cc/Interfacing/Processing)  
install lib: Arduino Firmata v9 ( and close open IDE )

> Files / Examples / Contributed Libraries /

> Arduino ( Firmata ) / arduino input /

as a start i cut it down

```auto
/*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:

 ![2019-03-11_15-58-16_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/661b54f5aeda11a4886894aa349f4fb33fa40f3e.png)  
arduino upload:  
 ![2019-03-11_15-57-10_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/996ccd71e7235ced152740249a5d87822c71957e.png)  
new processing code with select 3  
 ![2019-03-11_16-25-13_snap](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a9148401aa2041469221e4570476d728cf4b77a0.png)

```auto
/*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);
}

```

---

<div class="post-metadata">

**Author:** ![Mimal](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@Mimal](https://discourse.processing.org/u/Mimal)\
**Post date:** [March 11, 2019, 8:59am UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175/5 "2019-03-11T08:59:07Z")

</div>

Oh yes sure i forgot… sorry

---

<div class="post-metadata">

**Author:** ![Mimal](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@Mimal](https://discourse.processing.org/u/Mimal)\
**Post date:** [March 11, 2019, 9:01am UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175/6 "2019-03-11T09:01:37Z")

</div>

I have a Arduino program and it works :

```auto
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.

---

<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:** [March 11, 2019, 9:31am UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175/7 "2019-03-11T09:31:21Z")

</div>

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

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

```auto
</> code formatter

```

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

```auto
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

---

<div class="post-metadata">

**Author:** ![Mimal](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@Mimal](https://discourse.processing.org/u/Mimal)\
**Post date:** [March 11, 2019, 11:41am UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175/8 "2019-03-11T11:41:51Z")

</div>

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 🙂

I’ll try this command this night.

---

<div class="post-metadata">

**Author:** ![Mimal](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@Mimal](https://discourse.processing.org/u/Mimal)\
**Post date:** [March 11, 2019, 5:47pm UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175/9 "2019-03-11T17:47:42Z")

</div>

> [@kll](#):
>
> readStringUntil(’\n’)

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.

 ![proc](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f4e36f585f04161d36e76fcd6e454d658ede8078.png)

---

<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:** [March 12, 2019, 1:13am UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175/10 "2019-03-12T01:13:25Z")

</div>

i expected you to add this print inside your

```auto
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.

---

<div class="post-metadata">

**Author:** ![Mimal](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@Mimal](https://discourse.processing.org/u/Mimal)\
**Post date:** [March 13, 2019, 6:21pm UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175/11 "2019-03-13T18:21:35Z")

</div>

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) :

```auto
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);
    }
}

```

---

<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:** [March 14, 2019, 2:03am UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175/12 "2019-03-14T02:03:42Z")

</div>

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

but may i suggest  
+add+ diagnostic print disable:

```auto
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.

---

<div class="post-metadata">

**Author:** ![qwest70](https://avatars.discourse-cdn.com/v4/letter/q/b2d939/32.png) [@qwest70](https://discourse.processing.org/u/qwest70)\
**Post date:** [July 23, 2019, 10:39am UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175/13 "2019-07-23T10:39:26Z")

</div>

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

---

<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:** [July 23, 2019, 11:33am UTC](https://discourse.processing.org/t/radio-code-sound-and-arduino/9175/14 "2019-07-23T11:33:30Z")

</div>

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

[https://playground.arduino.cc/Interfacing/Processing/](https://playground.arduino.cc/Interfacing/Processing/)

minimal starting code

```auto
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](https://processing.org/reference/libraries/serial/index.html)
