Go Down

Topic: NEWBIE: Add Sparkfun Midi-shield in a sketch (Read 234 times) previous topic - next topic

TheRebell3110

Hello,

i'm a newbie in arduino programming!

How can i include the sparkfun midishield in Pieter P's "Midi-Chord-Buttons" Sketch
Quote
#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

PieterP

#1
Jan 24, 2021, 11:20 am Last Edit: Jan 24, 2021, 11:20 am by PieterP
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:
Code: [Select]
#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 SoftwareSerialMIDI_Interface instead of the HardwareSerialMIDI_Interface.


Pieter

TheRebell3110

#2
Jan 24, 2021, 12:16 pm Last Edit: Jan 24, 2021, 12:16 pm by TheRebell3110
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

PieterP

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.

TheRebell3110

Hi, me again...

in attachment is the description of the idea
Thanks Pieter

TheRebell3110


PieterP

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.

TheRebell3110


PieterP

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

TheRebell3110


TheRebell3110

Hi Pieter,

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

PieterP

That's certainly possible. What have you tried?

TheRebell3110


PieterP

#13
Jan 27, 2021, 04:23 pm Last Edit: Jan 27, 2021, 04:25 pm by PieterP
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:
Code: [Select]
#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.

TheRebell3110

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

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

Go Up