Pages: [1]   Go Down
Author Topic: slow serial communication  (Read 320 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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;
}
« Last Edit: February 03, 2013, 11:29:27 pm by hilukasz » Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  Serial.begin(9600);

Why not:

Code:
  Serial.begin(115200);
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  Serial.begin(9600);

Why not:

Code:
  Serial.begin(115200);

well then.... I feel silly. thanks smiley
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Pages: [1]   Go Up
Jump to: