Go Down

Topic: Help implementing setHandleTimeCodeQuarterFrame from Arduino MIDI library (Read 145 times) previous topic - next topic

lorencz

I'm new to programming in C, (like, I started modifying sketches yesterday,) so bear with me. I'm trying to build a MIDI clock divider for a modular synth. I'm using an Arduino UNO. I'm using a basic MIDI input sketch that I've modified to hopefully read MIDI time code as well. I can verify that my MIDI connection is good, and that I'm receiving MIDI data because the onboard LED blinks with NoteOn messages. I can also verify that the outboard LED I'm using for quarter note data is working, because if I switch the pin numbers the outboard LED will blink with NoteOn information.

Edit: I should also mention that I have my DAW configured to send MIDI clock data on the correct port, and FWIW I can see the RX LED on my board blinking at a speed proportional to the tempo I have set on my DAW, so it looks like it's getting some kind of time related data. Maybe just song position though, not sure.

Code: [Select]
#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

// -----------------------------------------------------------------------------

// This function will be automatically called when a NoteOn is received.
// It must be a void-returning function with the correct parameters,
// see documentation here:
// http://arduinomidilib.fortyseveneffects.com/a00022.html

int count = 0; //initialize time code count at 0
void handleNoteOn(byte channel, byte pitch, byte velocity)
{
    digitalWrite(13, HIGH);
     // Do whatever you want when a note is pressed.

    // Try to keep your callbacks short (no delays ect)
    // otherwise it would slow down the loop() and have a bad impact
    // on real-time performance.
}

void handleNoteOff(byte channel, byte pitch, byte velocity)
{
    digitalWrite(13, LOW);
    // Do something when the note is released.
    // Note that NoteOn messages with 0 velocity are interpreted as NoteOffs.
}

void handleTimeCodeQuarterFrame(byte data)
{
    if(count < 24)            //if the number of recorded time code signals is less than 24, increase the count
      {
        count++; 
        digitalWrite(12, LOW);
      }
    else if(count == 24)      //if a quarter note's worth of time code messages have arrived, reset the count and blink an LED
      {
        count = 1;
        digitalWrite(12, HIGH);
      }
}

// -----------------------------------------------------------------------------

void setup()
{
    pinMode(12, OUTPUT);
    pinMode(13, OUTPUT);
    // Connect the handleNoteOn function to the library,
    // so it is called upon reception of a NoteOn.
    MIDI.setHandleNoteOn(handleNoteOn);  // Put only the name of the function

    // Do the same for NoteOffs
    MIDI.setHandleNoteOff(handleNoteOff);

    // And for TimeCode
    MIDI.setHandleTimeCodeQuarterFrame(handleTimeCodeQuarterFrame);
   
    // Initiate MIDI communications, listen to all channels
    MIDI.begin(MIDI_CHANNEL_OMNI);

         
}

void loop()
{
    // Call MIDI.read the fastest you can for real-time performance.
    MIDI.read();

    // There is no need to check if there are messages incoming
    // if they are bound to a Callback function.
    // The attached method will be called automatically
    // when the corresponding message has been received.
}

Grumpy_Mike


lorencz

Oh, yeah. Oops. The quarter note indicator LED is off the whole time. I think my code that is supposed to extract the clock data and count it, and then turn the LED pin high isn't working.

Grumpy_Mike

The code only sets the LED high, I assume it is wired up so that means on, for the duration of one MIDI message and then it turns off. Maybe this is too fast.
Try resetting the counter as you do now but turn on the LED if the count is greater than twelve.

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