Piano on arduino and processing

Hello everyone!

I have a problem with a project and I was wondering if one of you could help me, im new to processing :smile:. 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 :

#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.

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!

Hello,

Welcome!

Please format your code:
https://discourse.processing.org/faq#format-your-code

It helps us help you.

:)

1 Like

Hi @sugievlos ,

Welcome to the forum! :wink:

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! :yum:

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?

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

:)

2 Likes

Thank you soooo much :pleading_face:

1 Like