Go Down

Topic: Basic MIDI Code Examples w/ Genuino Micro (Read 1 time) previous topic - next topic

_pmj

Sep 06, 2016, 08:20 am Last Edit: Sep 06, 2016, 08:29 am by _pmj
Been looking around for some examples / guidelines for MIDI code for use with a Genuino Micro based controller.

There's tons of examples around but there's often caveats that specify that they're for specific Arduino's only other than the Micro.

Just looking for code examples that show how to send potentiometer and button press CC information to a 5-pin MIDI output socket.

Thanks for any leads or useful examples for the Micro.

PieterP

If you want to use a regular MIDI socket, you can use whatever Arduino board you like, the code does not change.

If you want to control your computer directly over USB, take a look at my tutorial on how to build an Arduino MIDI controller. You can follow the instructions for the Leonardo (it has the same chip as the micro).

Grumpy_Mike

#2
Sep 07, 2016, 01:14 pm Last Edit: Sep 07, 2016, 01:16 pm by Grumpy_Mike
With that board you can connect directly to the PC using USB and have it look like a MIDI device.

You can use the libraries here
https://github.com/arduino-libraries/MIDIUSB
With Docs at:-
https://www.arduino.cc/en/Reference/MIDIUSB

This is some test code that fires off random notes.
Code: [Select]
/*
 * MIDIUSB_test.ino
 *
 * Created: 4/6/2015 10:47:08 AM
 * Author: gurbrinder grewal
 * Modified by Arduino LLC (2015)
 */

#include "MIDIUSB.h"

// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}

void setup() {
  Serial.begin(115200);
}

// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).

void controlChange(byte channel, byte control, byte value) {
  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  MidiUSB.sendMIDI(event);
}

void loop() {
    int val;
  val = random(20,100);
    noteOn(0, val, 64);
    MidiUSB.flush();
    delay(100);
    noteOff(0, val, 64);
    MidiUSB.flush();
   delay(100);
  // controlChange(0, 10, 65); // Set the value of controller 10 on channel 0 to 65
}

_pmj

Thanks, that's good to know the board can be used for MIDI using the USB or a 5-pin DIN socket. 

I'm principally looking to control hardware synthesisers that have a 5-pin din input, so that would be the Micro's main use.

Grumpy_Mike

So you will need to have an external physical MIDI interface. See this project:-
http://www.thebox.myzen.co.uk/Hardware/MIDI_Shield.html

_pmj

#5
Sep 07, 2016, 07:16 pm Last Edit: Sep 07, 2016, 07:23 pm by _pmj Reason: Added URL
Ah, OK.  Was under the impression that a 5-pin DIN output socket can be wired to the Micro via a 220 ohm resistor and the various pots and switches connected to the analogue in/outs as per this example:

http://www.instructables.com/id/Send-and-Receive-MIDI-with-Arduino/

Is that not the case?  It would be an output only controller and not connected to the external synths while the code sketch was being uploaded.

PieterP

Yes, the MIDI connector is connected as shown in that Instructable, to pin 1 of the Arduino.

But why are you using analog inputs? And the Arduino doesn't have analog outputs, only digital (PWM is also digital).

On the Micro, it doesn't matter if you connect something to pins 0 & 1 while uploading, because the USB connection is used for upload. Other boards like the Uno or Mega do need pins 0 & 1 for uploading.
In your MIDI sketches, you should replace Serial with Serial1 for the same reason: Serial is the USB connection, and Serial1 is the serial connection on pins 0 & 1.

_pmj

But why are you using analog inputs?
What I'm doing is making a MIDI controller which has 4 x potentiomters, 2 x joysticks and 2 x momentary switches and connecting these to the analogue connectors on the Micro.  

I then need to come up with some code that can send CC messages out to a hardware synth from the Micro via a 5-pin MIDI DIN connector, hence me looking around for code examples for MIDI CC that will work with the Micro.

In your MIDI sketches, you should replace Serial with Serial1 for the same reason: Serial is the USB connection, and Serial1 is the serial connection on pins 0 & 1.
Thanks, will bear this in mind.

PieterP

What I'm doing is making a MIDI controller which has 4 x potentiomters, 2 x joysticks and 2 x momentary switches and connecting these to the analogue connectors on the Micro. 

I then need to come up with some code that can send CC messages out to a hardware synth from the Micro via a 5-pin MIDI DIN connector, hence me looking around for code examples for MIDI CC that will work with the Micro.
Of course, I'm sorry, I confused this with another MIDI thread ;) .

Grumpy_Mike

#9
Sep 08, 2016, 12:43 am Last Edit: Sep 08, 2016, 12:44 am by Grumpy_Mike
The big problem with Instructables is that by and large they are written by people who do not know what they are doing. We are rather fed up with acting as Instructables correctors here. My advice is never look to that site unless you know more than the person who wrote it so you can spot the mistakes.
That method of directly outputting to a MIDI socked is a bit of a fudge. It will work a lot of the time but there are some devices where it will not work because the voltage / current requirement of the MIDI standard is not met.

_pmj

That method of directly outputting to a MIDI socked is a bit of a fudge. It will work a lot of the time but there are some devices where it will not work because the voltage / current requirement of the MIDI standard is not met.
So safer / less of a fudge if I was to either use the MIDIUSB connection as you outlined or employ the MIDI shield?  I know far less than whoever wrote that instructable so any help appreciated.

Grumpy_Mike

Quote
So safer / less of a fudge if I was to either use the MIDIUSB connection as you outlined or employ the MIDI shield
Yes I always use that configuration when I need a 5 pin MIDI socket. As you only want to send then it is just the send side of the circuit. There is no point buying a MIDI shield as you only need part of it.

The code example I posted should get you started as it contains an example of sending CC messages.

_pmj

The code example I posted should get you started as it contains an example of sending CC messages.
Yes, I uploaded that sketch and it worked fine, many thanks. 

I can see the structure of the sketch and what it's doing but not sure how to relate that to the output from a potentiomenter.  I'm guessing that you define the number of potentiometers and then apply the CC commands as per your example but unsure how to bolt all that together at this stage.

Grumpy_Mike

#13
Sep 08, 2016, 09:26 am Last Edit: Sep 08, 2016, 09:27 am by Grumpy_Mike
Read a pot with the analogRead command, this gives a value between 0 and 1023 so divide that by eight to get it in the range 0 to 127 and use that number as the value prameter to the CC message.

_pmj

Thanks again - found one of your other posts with this code which looks like it should do the job.

Code: [Select]
AnalogValue0 = analogRead(0);
 //  convert to a range from 0 to 127:
 cc = AnalogValue0/8;
 // check if analog input has changed
 if (abs(lastAnalogValue0 - cc)>1 {
   MIDI.sendControlChange(16,cc,1);
   // update lastAnalogValue zero variable
   lastAnalogValue0 = cc;

AnalogValue1 = analogRead(1);
 //  convert to a range from 0 to 127:
 cc = AnalogValue1/8;
 // check if analog input has changed
 if (abs(lastAnalogValue1 - cc)>1 {
   MIDI.sendControlChange(17,cc,1);
   // update lastAnalogValue one variable
   lastAnalogValue1 = cc;

// and so on


Where MIDI.sendControlChange parameters = (CC number, Control Value, MIDI Channel) - that correct?

Go Up