Go Down

Topic: Faster Serial Communication? (Read 1 time) previous topic - next topic


I think I'm going to build an electronic drum kit with my Arduino. I just wrote a bit of code for a bass pedal:

Code: [Select]

int butpin = 2;
int last;
int but;

void setup() {
 pinMode(butpin, INPUT);

void loop() {
 but = digitalRead(butpin);
if (but == HIGH && last == 0){
  noteOff(1, 20, 0);
  last = 1;
} else{
if (but == LOW && last == 1){
  noteOn(1, 20, 64);
  last = 0;
} else {
// delay(1);

void noteOn(byte channel, byte note, byte velocity) {
 midiMsg( (0x90 | (channel & 0xf)), note, velocity);

// Send a MIDI note-off message.  Like releasing a piano key
void noteOff(byte channel, byte note, byte velocity) {
 midiMsg( (0x80 | (channel & 0xf)), note, velocity);

// Send a general MIDI message
void midiMsg(byte cmd, byte data1, byte data2) {
 //digitalWrite(ledPin,HIGH);  // indicate we're sending MIDI data
 Serial.print(cmd, BYTE);
 Serial.print(data1, BYTE);
 Serial.print(data2, BYTE);

When I press the pedal it sends a midi note(yippy). The only issue is I can hear the lag. Right now I have my Arduino hooked up to a USB1.1 port, will I gain any speed by using a USB2.0 port? Is there any way for me to improve this performance? My expansion plans involve piezos and the analog pins. There isn't much point in continuing if I can't significantly reduce this lag.

Right now I'm running the Roland Serial Midi driver.

Thanks in advance.


You should be able to send somewhere between close to 3K   characters a second with the serial (unless there is software overhead I am missing).   However digitalRead has some error checking and has a significant delay.  As I recall,  it is around 1/60 of a second.

If digitalRead/Write are slowing you down you can read/write directly to the port close to 4million times a second.  Look up "port manipulation".  There are several forum posts on the subject.  The serialWrite code is pretty tight and I doubt you could do it any faster than the library.


As I recall,  it is around 1/60 of a second.

No it's not it's closer to 0.4mS. If you can hear any lag then it is the rest of the system not what you posted here.

USB1.1 port, will I gain any speed by using a USB2.0 port?

No you are just using serial bridge running at the MIDI rate.

Are you feeding the Arduino MIDI output directly into your system or is there some other software in between?


i have heard of lag in MIDI if you send more then one note and keep sending the overhead, you will heard the lag, but if you send 3 note in the same message that lag should not be there!
Not sure if this is what you need


Thanks for the info, it's quite helpful. After your posts I focused on my software rather than my code/the arduino. Turns out that Ableton Live is just really slow. Maybe it is my computer though. I changed programs and it responds MUCH faster.

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!

via Egeo 16
Torino, 10131