Go Down

Topic: slow serial communication (Read 405 times) previous topic - next topic

hilukasz

Feb 04, 2013, 05:25 am Last Edit: Feb 04, 2013, 05:29 am by hilukasz Reason: 1
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):

Code: [Select]
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:

Code: [Select]
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;
}
for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Nick Gammon

Code: [Select]

  Serial.begin(9600);


Why not:

Code: [Select]

  Serial.begin(115200);
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

hilukasz


Code: [Select]

  Serial.begin(9600);


Why not:

Code: [Select]

  Serial.begin(115200);



well then.... I feel silly. thanks :)
for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy