MIDI control of effect pedals sample code MIDI control of effect pedals sample code

Japanese

MIDI control of effect pedals with Arduino

Sample code

A program that receives a program change (0xC*) command and generates pulses

When a program change (0xc0) command is sent to Ch1 (0x00), the number of pulses set in the first parameter is generated on the A7 pin.


#include <MIDI.h>

#define LED A7

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI); // use serial-1(TX1/RX1)

void BlinkLed(byte num)         // Basic blink function
{
    for (byte i=0;i<num;i++)
    {
        digitalWrite(LED,HIGH); // Increase the width of the pulse
        digitalWrite(LED,HIGH); // by 4 times
        digitalWrite(LED,HIGH);
        digitalWrite(LED,HIGH);

        digitalWrite(LED,LOW);
    }
}

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

void setup()
{
    pinMode(LED, OUTPUT);
    MIDI.begin(1);           // Launch MIDI, by default listening to channel 1.
    BlinkLed(1);
}

void loop()
{
    if (MIDI.read())                // Is there a MIDI message incoming ?
    {
        switch(MIDI.getType())      // Get the type of the message we caught
        {
            case midi::ProgramChange:       // If it is a Program Change (0xCn),
                BlinkLed(MIDI.getData1());  // blink the LED a number of times
                                            // correponding to the program number
                                            // (0 to 127, it can last a while..)
                break;
            // See the online reference for other message types
            default:
                break;
        }
    }
}