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