# Piano on arduino and processing

**URL:** https://discourse.processing.org/t/piano-on-arduino-and-processing/29159
**Category:** Electronics (Arduino, etc.)
**Tags:** homework
**Created:** [April 6, 2021, 10:11am UTC](https://discourse.processing.org/t/piano-on-arduino-and-processing/29159 "2021-04-06T10:11:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sugievlos](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@sugievlos](https://discourse.processing.org/u/sugievlos)
#### Post date: [April 6, 2021, 10:11am UTC](https://discourse.processing.org/t/piano-on-arduino-and-processing/29159/1 "2021-04-06T10:11:36Z")

</div>

Hello everyone!

I have a problem with a project and I was wondering if one of you could help me, im new to processing 😄. I have to do a piano on arduino, and with processing, play the note on my computers’s speaker. Here are my codes if you can take a look at it i’d very much appreciate it!!

This is my arduino code :

```auto
#include <SPI.h>

//Variables for the buttons//
const int touche1 = 2;
const int touche2 = 3;
const int touche3 = 4;
const int touche4 = 5;

void setup() {

  Serial.begin(9600); 
  
  //Mode of the pins of my buttons//
  pinMode(touche1, INPUT);
  pinMode(touche2, INPUT);
  pinMode(touche3, INPUT);
  pinMode(touche4, INPUT);
  
}

void loop() {
  
  boolean etat1 = digitalRead(touche1); // This is where i analyse the button's states
  boolean etat2 = digitalRead(touche2);
  boolean etat3 = digitalRead(touche3);
  boolean etat4 = digitalRead(touche4);
  
  if(etat1 == HIGH)//if the button 1 is pushed,
  {
    Serial.println("1"); //then i write 1 in the monitor
    Serial.println();   
    
  }else if(etat2 == HIGH)
    {
      Serial.println("2");
      Serial.println();
      
    }else if(etat3 == HIGH)
      {
        Serial.println("3");
        Serial.println();
        
      }else if(etat4 == HIGH)
        {
          Serial.println("4");
          Serial.println();
        }
   

}

```

This is my processing code, I used the minim library to play the notes.

```auto
import processing.serial.*; //library to communicate with arduino
import ddf.minim.*;
import ddf.minim.ugens.*;

Minim minim;
AudioOutput out; 
Serial myPort; 

int note; 

void setup()
{
  size(400,400);
  printArray(Serial.list());
  
  myPort = new Serial(this, Serial.list()[0],9600);//declaration of the port
  
  minim = new Minim(this);
  out = minim.getLineOut();
}

void draw()
{  
   while(myPort.available()>0)//if the is a connection between arduino and processing 
  {
    note = myPort.read();//we read what number arduino sent
    
    if(note == 1)//if it is one, 
    {
      out.playNote(264.0); //we play the frequencie of the note DO
    }else if(note == 2)//else if it is 2
      {
        out.playNote(297.0); //we play the frequencie of the note RE
      }else if(note == 3)
        {
          out.playNote(330.0); //we play Mi...
        }else if(note == 4)
          {
            out.playNote(352.0); ///On joue la frequence de FA
          }
  background(0);
  stroke(255);

  //This is an animation that draws the frequencie//
  for(int i = 0; i < out.bufferSize() - 1; i++)
  {
    line( i, 50 + out.left.get(i)*50, i+1, 50 + out.left.get(i+1)*50 );
    line( i, 150 + out.right.get(i)*50, i+1, 150 + out.right.get(i+1)*50 );
  }
}

```

Thanks a lot and sorry for my english or my mistakes im new to processing!

---

<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: [April 6, 2021, 10:35am UTC](https://discourse.processing.org/t/piano-on-arduino-and-processing/29159/2 "2021-04-06T10:35:01Z")

</div>

Hello,

Welcome!

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

It helps us help you.

`:)`

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [April 6, 2021, 2:45pm UTC](https://discourse.processing.org/t/piano-on-arduino-and-processing/29159/3 "2021-04-06T14:45:35Z")

</div>

Hi @sugievlos ,

Welcome to the forum! 😉

As @glv said, you should format your code but the you don’t actually explain what issues you are facing.

We also can’t give you the full solution! 😋

---

<div class="post-metadata">

### Author: ![sugievlos](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@sugievlos](https://discourse.processing.org/u/sugievlos)
#### Post date: [April 6, 2021, 10:15pm UTC](https://discourse.processing.org/t/piano-on-arduino-and-processing/29159/4 "2021-04-06T22:15:24Z")

</div>

Thank you and thanks for responding!  
My issue is that when I press the buttons on my arduino, the notes on processing dont play, and i don’t see any errors by myself. Are my conditions wrong? Or maybe the way I read on the serial monitor?

---

<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: [April 6, 2021, 11:48pm UTC](https://discourse.processing.org/t/piano-on-arduino-and-processing/29159/5 "2021-04-06T23:48:38Z")

</div>

Hello,

I am assuming your hardware works and you can see output on the serial monitor.  
Do not use serial monitor when serial is communicating with Processing.

Moving on…

I simplified your code so you can see what you are receiving on Processing.

> **Arduino**
>
> ```auto
> void setup() 
> {
> Serial.begin(9600);
> }
> 
> void loop() 
> {
> Serial.println('1'); //write ASCII 1 to the monitor
> Serial.println();   
> delay(1000);
> Serial.println('2'); //write ASCII 2 to the monitor
> Serial.println();   
> delay(1000);
> }
> 
> ```

> **Processing**
>
> ```auto
> import processing.serial.*;
> Serial myPort; 
> 
> int note; 
> 
> void setup()
> {
> size(400,400);
> printArray(Serial.list());
>   
> myPort = new Serial(this, Serial.list()[2],9600);
> }  
> 
> void draw()
> {
> while(myPort.available()>0)
> {
> note = myPort.read();
> println(note);
>     
> if(note == 1) 
> {
> background(255, 0, 0);
> println("RED");
> }
> else if(note == 2)
> {
> background(0, 255, 0);
> println("GREEN");
> }
> }
> }     
> 
> ```

You can see that you are sending extra ASCII characters that are NOT EQUAL to a number (1, 2, 3, etc.)

Consider using:  
`if(note == '1'); // compares to ASCII 1`

println() is also sending ASCII 10 (LF Linfe Feed) and 13 (CR Carriage Return)  
And you are sending this twice!

Do some more exploration on this…

You may even what to consider just sending the _value_ with _Serial.write()_

That is enough to get you started.

References:

- [Serial \ Libraries \ Processing.org](https://processing.org/reference/libraries/serial/index.html)
- [ASCII](https://theasciicode.com.ar/ascii-control-characters/line-feed-ascii-code-10.html)
- [Electronics \ Processing.org](https://processing.org/tutorials/electronics/)
- And lots more out there on the subject…

`:)`

---

<div class="post-metadata">

### Author: ![sugievlos](https://avatars.discourse-cdn.com/v4/letter/s/c68b51/32.png) [@sugievlos](https://discourse.processing.org/u/sugievlos)
#### Post date: [April 7, 2021, 1:30pm UTC](https://discourse.processing.org/t/piano-on-arduino-and-processing/29159/6 "2021-04-07T13:30:17Z")

</div>

Thank you soooo much 🥺
