Hello,
I'm a newbie and don't have much experience programming. I'd like to expand on my last project where I used the serial data from an accelerometer attached to the body, to create sound through a piezo buzzer. Now, I'd like to use this data to play mp3 files in Processing. I know how to play an audio file in Processing but I don't know how to bring in the arduino data and link it with Processing.
Any help would be much appreciated.
Here is my Arduino code:
*/
// these constants describe the pins. They won't change:
const int sensorPinx = 2; // x-axis of the accelerometer
const int sensorPiny = 3; // y-axis
const int sensorPinz = 4; // z-axis (only on 3-axis models)
int sensorValuex;
int sensorValuey;
int sensorValuez;
int speakerPin = 9; // speaker connected to digital pin 9
void setup()
{
pinMode(sensorPinx, INPUT); // sets the sensorPin to be an input
pinMode(sensorPiny, INPUT); // sets the sensorPin to be an input
pinMode(sensorPinz, INPUT); // sets the sensorPin to be an input
pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output
Serial.begin(9600); // initialize the serial communications:
}
void loop()
{
sensorValuex = analogRead(sensorPinx); // read the value from the sensor
sensorValuey = analogRead(sensorPiny); // read the value from the sensor
sensorValuez = analogRead(sensorPinz); // read the value from the sensor
// print the sensor values:
Serial.print(analogRead(sensorPinx));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(sensorPiny));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(sensorPinz));
Serial.println();
// delay before next reading:
delay(100);
if (sensorValuex >=50 && sensorValuex <=100) { // body not moving
beep(speakerPin,2093,100); //C
if (sensorValuex >=300 && sensorValuex <=400);
beep(speakerPin,2637,500); //E
if (sensorValuex >=425 && sensorValuex <=500);
beep(speakerPin,3136,100); //G
}
else
{
digitalWrite(speakerPin, LOW); //turn speaker on
}
} // scale();
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds) {
int x;
long delayAmount = (long)(1000000/frequencyInHertz);
long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin,LOW);
delayMicroseconds(delayAmount);
}
}
void scale () {
beep(speakerPin,2093,100); //C:
beep(speakerPin,2349,100); //D
beep(speakerPin,2637,500); //E
beep(speakerPin,2793,500); //F
beep(speakerPin,3136,100); //G
beep(speakerPin,3520,500); //A
beep(speakerPin,3951,500); //B
beep(speakerPin,4186,500); //C */
}
Here is my Processing code:
*/
import processing.serial.*;
//import cc.arduino.*;
//Arduino arduino;
//Serial myPort;
String serialString;
import ddf.minim.*;
AudioPlayer player;
Minim minim;
void setup()
{
size(512, 200, P2D);
minim = new Minim(this);
// load a file, give the AudioPlayer buffers that are 1024 samples long
// player = minim.loadFile("song.mp3");
// load a file, give the AudioPlayer buffers that are 2048 samples long
player = minim.loadFile("song.mp3", 2048);
// play the file
player.play();
player.loop();
}
void draw()
{
background(0);
stroke(255);
// draw the waveforms
// the values returned by left.get() and right.get() will be between -1 and 1,
// so we need to scale them up to see the waveform
// note that if the file is MONO, left.get() and right.get() will return the same value
for(int i = 0; i < player.left.size()-1; i++)
{
line(i, 50 + player.left.get(i)*50, i+1, 50 + player.left.get(i+1)*50);
line(i, 150 + player.right.get(i)*50, i+1, 150 + player.right.get(i+1)*50);
}
}
void stop()
{
// always close Minim audio classes when you are done with them
player.close();
minim.stop();
super.stop();
}