I am routing midi data via IAC bus internally then reading it with processing, after that I am sending the messages to different arduino’s so I have to have an efficient way to split the messages between the arduinos. The way I am using right now isn’t very readable, and seems a bit slow and since this is music based it’s fairly noticeable that there is a lag, which is no bueno.
assuming it is because of all the serial writes I am doing. Is there a way to write all these at once? or any suggestions on speeding this up?
arduino code (just one of the arduinos):
int redLED = 6;
int greenLED = 5;
int blueLED = 3;
void setup()
{
Serial.begin(9600);
pinMode(redLED, OUTPUT); // red
pinMode(greenLED, OUTPUT); // green
pinMode(blueLED, OUTPUT); // blue
}
void loop()
{
switch(GetFromSerial())
{
case 'Q':
analogWrite(redLED, GetFromSerial());
break;
case 'W':
analogWrite(greenLED, GetFromSerial());
break;
case 'E':
analogWrite(blueLED, GetFromSerial());
break;
}
}
int GetFromSerial()
{
while (Serial.available()<=0) {
}
return Serial.read();
}
processing bit:
import themidibus.*;
import processing.serial.*;
Serial port;
MidiBus myBus;
int lastNote;
boolean noteOff;
void setup() {
port = new Serial(this, Serial.list()[4], 9600);
size(400,400);
MidiBus.list();
myBus = new MidiBus(this,0,0);
}
void draw() {
background(0);
int[] myRGB = { 100, 0, 255 };
// goes to first arduino
if(lastNote == 37){
port.write('Q');
port.write(0);
port.write('W');
port.write(255);
port.write('E');
port.write(255);
}
// goes to second arduino
else if(lastNote == 40 || lastNote == 41){
port.write('R');
port.write(255);
port.write('G');
port.write(0);
port.write('B');
port.write(125);
}
if(noteOff){
port.write('Q');
port.write(0);
port.write('W');
port.write(0);
port.write('E');
port.write(0);
port.write('R');
port.write(0);
port.write('G');
port.write(0);
port.write('B');
port.write(0);
}
}
void noteOn(int channel, int pitch, int velocity) {
//println("Pitch: " + pitch);
lastNote = pitch;
noteOff = false;
}
void noteOff(int channel, int pitch, int velocity) {
//println("off: " + pitch);
lastNote = 99999;
noteOff = true;
}