USB-MIDI - delay when processing commands

Status
Not open for further replies.

bennigraf

New member
Hi everyone!

I have a project where I basically receive MIDI over USB to set digital potis over SPI. I've attached the (shortened) code below.

My problem is that there is a delay of about a whole second between sending the MIDI CC command (I'm using SuperCollider for this) and the reaction of the digital potentiometer. I can't really imagine that it's because of the SPI interface because there's very few code involved (there are no delay() calls or anything involved in the Mcp4261-class I use).

Now I also don't really know what my options are to debug this. Any tips?

Thanks,
Benni.


Code:
// This example demonstrates control over SPI to the Microchip MCP4261 Digital potentometer
// SPI Pinouts are for Arduino Uno and Arduino Duemilanove board (will differ for Arduino MEGA)

// Download these into your Sketches/libraries/ folder...

#include <Arduino.h>
#include <SPI.h>

// Mcp4261 library available from https://github.com/dreamcat4/Mcp4261
#include <Mcp4261.h>

#define POTI_1_PIN 0

float poti1_ohms = 101550.00;

// and measure those
float poti1_wiper = 115;

MCP4261 potis[] = {
  MCP4261(POTI_1_PIN, poti1_ohms, poti1_wiper)
};

void setup()
{
  SPI.begin(); 
  
  potis[0].scale = 127; // scale to midi values
  
  potis[0].wiper0(0);
  potis[0].wiper1(0);
    
  usbMIDI.setHandleControlChange(OnControlChange);
}

float lastMillis = 0;

void loop() { 
  usbMIDI.read();
}

void OnControlChange(byte channel, byte control, byte value) {
  int wiperNum = control%2;
  if(wiperNum == 0) {
    potis[0].wiper0((int)value);
  } else {
    potis[0].wiper1((int)value);
  }
}
 
Do you have an Oscilloscope or Logic Analyzer to check if the SPI is being activated straight after the Midi command is sent and see if the MCP4261 is doing the change more or less immediately after recieving the SPI command?

Also try and slow down the loop (sounds counter intuitive) by adding a delay(10); after the read.
 
Hi!

Thanks for pointing this out! I don't have an oscilloscope or anything, but a led on the spi line was helpful already :). It turned out that occasionally I sent too many MIDI-messages, and that combined with a very short delay in the library added up and led to this latency.

Thanks,
Benni.
 
Status
Not open for further replies.
Back
Top