NEWBIE: Add Sparkfun Midi-shield in a sketch

Hello,

i'm a newbie in arduino programming!

How can i include the sparkfun midishield in Pieter P's "Midi-Chord-Buttons" Sketch

#include "MIDIButtonChord.hpp"
#include "Notes.hpp"

constexpr uint8_t channel = 1; // MIDI channel to send on

MIDIButtonChord buttons[] = {
{Chords::Major, 2, note(C, 4), channel}, // Major C chord in the 4th octave, button between pin 2 and ground, MIDI channel 1
{Chords::Minor, 3, note(D, 4), channel},
{Chords::Minor, 4, note(E, 4), channel},
{Chords::MajorFirstInv, 5, note(F, 4), channel},
{Chords::MajorSecondInv, 6, note(G, 4), channel},
{Chords::MinorSecondInv, 7, note(A, 4), channel},
{Chords::Diminished, 8, note(B, 4), channel},
};

/*

  • You can change the chords afterwards using
  • buttons[0].setChord(Chords::MajorSeventh);
  • for example.
    */

void setup() {
MIDI.begin();
for (MIDIButtonChord &button : buttons)
button.begin();
}

void loop() {
for (MIDIButtonChord &button : buttons)
button.update();
}

Thanks for helping
Marco

MIDI-Chord-Buttons.zip (5.27 KB)

The code you found seems to be quite old. The MIDI chord buttons have since been added to the Control Surface library.

The Sparkfun MIDI shield seems to connect to the TX and RX pin of the hardware UART. On an Arduino UNO, this means you have to use the “Serial” port in your sketch.

For example:

#include <Control_Surface.h> // https://github.com/tttapa/Control-Surface

HardwareSerialMIDI_Interface midi { Serial };

using namespace MIDI_Notes;

NoteChordButton buttons[] {
  { 2,  note(C, 4),  Chords::Major }, // Major C chord in the 4th octave, button between pin 2 and ground
  { 3,  note(D, 4),  Chords::Minor },
  { 4,  note(E, 4),  Chords::Minor },
  { 5,  note(F, 4),  Chords::MajorFirstInv },
  { 6,  note(G, 4),  Chords::MajorSecondInv },
  { 7,  note(A, 4),  Chords::MinorSecondInv },
  { 8,  note(B, 4),  Chords::Diminished },
};

void setup() {
  Control_Surface.begin();
}

void loop() {
  Control_Surface.loop();
}

/*
 * You can change the chords afterwards using 
 *    buttons[0].setChord(Chords::MajorSeventh);
 * for example.
 */

On boards with native USB support (e.g. an Arduino Leonardo), the hardware UART will be Serial1, so you would have to change the code accordingly.
If you have changed the jumpers on the MIDI shield to use SoftwareSerial, you can use the [iurl=https://tttapa.github.io/Control-Surface-doc/Doxygen/d6/d6d/classSoftwareSerialMIDI__Interface.html]SoftwareSerialMIDI_Interface[/iurl] instead of the HardwareSerialMIDI_Interface.

Pieter

Thanks Pieter,

I've paste your code in a new sketch and compiled it on the arduino with success!

I think on the sparkfun midi-shield, i've a run and programm switch (software serial and hardware serial switch)
i don't need to change the code
is it right?

I have a mega 2560

Greetings

TheRebell3110:
I think on the sparkfun midi-shield, i've a run and programm switch (software serial and hardware serial switch)
i don't need to change the code
is it right?

I'm not entirely sure what you mean here, but if you set the switch to the “hardware serial” position after uploading, you should be able to use the code without any modifications.

Hi, me again...

in attachment is the description of the idea
Thanks Pieter

sorry here...

The image is hardly readable, could you export it as an SVG file? If that's not possible, use PNG at a higher resolution. JPEG is meant for photos, not diagrams and text.

Yes, in pdf !
or png

Arduino Picture Chord Model (1).pdf (237 KB)

Much better, thanks! I'll have a more detailed look at it tomorrow.

Thanks Pieter!

That's great

Hi Pieter,

Have you looking over my shema diagram?
It is possible to programming such a code?
Greetings
Marco

That's certainly possible. What have you tried?

How can i Start....?

TheRebell3110:
How can i Start....?

Break it up into manageable smaller chunks.

One aspect is MIDI output, actually sending the notes/chords. As you've found, that's relatively easy using the NoteChordButton class. It might be worthwhile to learn how to send the MIDI notes yourself, without relying on that class.
Have a look at the Send-MIDI-Notes.ino example.
Sending chords is very similar, a chord is just a collection of intervals relative to a root note:

#include <Control_Surface.h>

USBMIDI_Interface midi;

using namespace MIDI_Notes;

// Note and chord to send
MIDIAddress rootnote = note(C, 4);
auto chord           = Chords::Major;
uint8_t velocity     = 0x7F;

Button btn {2}; // push button between ground and pin 2

void setup() {
  midi.begin();
  btn.begin();
}

void loop() {
  midi.update();
  btn.update();
  if (btn.getState() == Button::Falling) {       // if button is pressed
    midi.sendNoteOn(rootnote, velocity);
    for (int8_t interval : chord)
      midi.sendNoteOn(rootnote + interval, velocity);
  } else if (btn.getState() == Button::Rising) { // if button is released
    midi.sendNoteOff(rootnote, velocity);
    for (int8_t interval : chord)
      midi.sendNoteOff(rootnote + interval, velocity);
  }
}

This example also shows you how to read input from a push button, which will come in handy when programming the user interface later.

Next, you could look at how to handle MIDI input, for instance using the MIDI-Input.ino example.

Then learn how to write text to your display, use the examples that come with the library for your specific display.

Finally, you can start programming the actual logic for the menu system. This is most likely the hardest part, and you'll have to watch out not to end up with a huge pile of spaghetti code. Start with simple, smaller user interfaces first. Try to plan out the architecture in advance, and regularly take some time to refactor the code you already have.

Hello Pieter,
Thanks for the answer, i ´ll try it tomorrow!

Perhaps i find somebody who can write for me thé code.
Greetings

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.