Hello,
I am doing a project with aruidno and processing. And the effect that I want to achieve is like when you move your hands close to the device, it will start to play a music. And when you change the distance between your hands and the device, some drum like sound will change accordingly(with the music play at the same time).
Now I am using the arduino midishield to send realtime midi signal to Reason Propellerhead on my computer and using processing as an on-off switcher to control the music player.
But the problem is that is seems they can not work at the same time. When the Reason generate a sound the player is mute and when the player is playing the music the Reason seems have some problem..
Could you give me some advices on how to fix it? I really need your help because I am so confused and have no idea about what's going wrong with it.
Here are the code .
Code for arduino
#include "Ultrasonic.h"
#include <Wire.h>
#define THRESHOLD 30
#define note_MAX 127
#define note_MIN 0
long RangeInCentimeters_new;
long RangeInCentimeters_old;
Ultrasonic ultrasonic(7);
void setup()
{
Serial.begin(31250);
}
void loop()
{
String cmd;
int note = 0;
RangeInCentimeters_old = RangeInCentimeters_new;
ultrasonic.MeasureInCentimeters();
RangeInCentimeters_new = ultrasonic.RangeInCentimeters;
delay(250);
note = map( RangeInCentimeters_old,0,40,40,127 );
if(RangeInCentimeters_new<THRESHOLD)
{
if(RangeInCentimeters_old<THRESHOLD)
return;
else
cmd = "Music";
}
else{
if(RangeInCentimeters_old<THRESHOLD)
cmd = "Stop";
else
return;
}
sendmsg(cmd);
}
void sendmsg(String msg)
{
if(Serial.available() <= 0)
{
Serial.println(msg);
}
}
void noteOn(int cmd, int pitch, int velocity)
{
Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}
Code for processing:
import processing.serial.*;
import processing.sound.*;
SoundFile soundfile;
Serial myPort;
String val;
boolean firstContact = false;
String status="stop";
void setup()
{
size(200, 200);
myPort = new Serial(this, Serial.list()[2], 31250);
myPort.bufferUntil('\n');
soundfile = new SoundFile(this, "01.mp3");
}
void draw()
{
}
void serialEvent( Serial myPort) {
val = myPort.readStringUntil('\n');
val = trim(val);
println(val);
if(val.equals( "Music" )){
//SoundFile soundfile = new SoundFile(this, "01.mp3");
if(status.equals("stop")){
soundfile.play();
soundfile.amp(0.5);
status="playing";
}
}
else{
if(status.equals("playing")){
soundfile.stop();
status="stop";
}
}
}
Thank you soooo much!!
Linxi