Creating a Midi Keyboard with USB-MIDI

Hey Y'all! I'm currently trying to create a midi keyboard using the USB-MIDI Library. Right now, I have seven buttons going into different digital ports on a Leonardo. The goal is that when I press the button, it will play that note. I'm close, but I am running into an issue.

When I press a button, it does play that note. However, it then will start cycling through the scale without the other buttons being pressed. Does anyone have an idea of what may be causing this? This is the code I have right now:

#include <USB-MIDI.h>

USBMIDI_CREATE_DEFAULT_INSTANCE();

int C = 2;
int D = 3;
int E = 4;
int F = 5;
int G = 6;
int A = 7;
int B = 8;

void setup() {
// put your setup code here, to run once:

pinMode (2, INPUT);
pinMode (3, INPUT);
pinMode (4, INPUT);
pinMode (5, INPUT);
pinMode (6, INPUT);
pinMode (7, INPUT);
pinMode (8, INPUT);

}

void loop() {
// put your main code here, to run repeatedly:

//C
if (C, HIGH) {
MIDI.sendNoteOn(60, 100, 1);
delay(1000);
}
else {
MIDI.sendNoteOff(60, 0, 1);
}

//D
if (D, HIGH) {
MIDI.sendNoteOn(62, 100, 1);
delay(1000);
}
else {
MIDI.sendNoteOff(62, 0, 1);
}

//E
if (E, HIGH) {
MIDI.sendNoteOn(64, 100, 1);
delay(1000);
}
else {
MIDI.sendNoteOff(64, 0, 1);
}

//F
if (F, HIGH) {
MIDI.sendNoteOn(65, 100, 1);
delay(1000);
}
else {
MIDI.sendNoteOff(65, 0, 1);
}

//G
if (G, HIGH) {
MIDI.sendNoteOn(67, 100, 1);
delay(1000);
}
else {
MIDI.sendNoteOff(67, 0, 1);
}

//A
if (A, HIGH) {
MIDI.sendNoteOn(69, 100, 1);
delay(1000);
}
else {
MIDI.sendNoteOff(69, 0, 1);
}

//B
if (B, HIGH) {
MIDI.sendNoteOn(71, 100, 1);
delay(1000);
}
else {
MIDI.sendNoteOff(71, 0, 1);
}

}

if (C, HIGH)

what is this ? where you seen such strange syntax?

Change all if /else to the following form:

  //C
  if (digitalRead (C)== HIGH) {
    MIDI.sendNoteOn(60, 100, 1);
    delay(1000);
  }
  else {
    MIDI.sendNoteOff(60, 0, 1);
  }
#include <USB-MIDI.h>

USBMIDI_CREATE_DEFAULT_INSTANCE();

const byte C = 2;
const byte D = 3;
const byte E = 4;
const byte F = 5;
const byte G = 6;
const byte A = 7;
const byte B = 8;

void setup() {
  pinMode (2, INPUT);
  pinMode (3, INPUT);
  pinMode (4, INPUT);
  pinMode (5, INPUT);
  pinMode (6, INPUT);
  pinMode (7, INPUT);
  pinMode (8, INPUT);
}

void loop() {
  if (digitalRead(C) == HIGH) MIDI.sendNoteOn(60, 100, 1);
  else MIDI.sendNoteOff(60, 0, 1);

  if (digitalRead(D) == HIGH) MIDI.sendNoteOn(62, 100, 1);
  else MIDI.sendNoteOff(62, 0, 1);

  if (digitalRead(E) == HIGH) MIDI.sendNoteOn(64, 100, 1);
  else MIDI.sendNoteOff(64, 0, 1);

  if (digitalRead(F) == HIGH) MIDI.sendNoteOn(65, 100, 1);
  else MIDI.sendNoteOff(65, 0, 1);

  if (digitalRead(G) == HIGH) MIDI.sendNoteOn(67, 100, 1);
  else MIDI.sendNoteOff(67, 0, 1);

  if (digitalRead(A) == HIGH) MIDI.sendNoteOn(69, 100, 1);
  else MIDI.sendNoteOff(69, 0, 1);

  if (digitalRead(B) == HIGH) MIDI.sendNoteOn(71, 100, 1);
  else MIDI.sendNoteOff(71, 0, 1);
  delay(1000);
}

This does not do what you might think it does, cppreference.com - comma operator

As pointed out by others, you need to actually read the pin, e.g. if (digitalRead(C) == HIGH).

Mark constants with the const keyword, i.e. const int pin_C = 2;. Also consider using clearer variable names.

Do you have external pull-down or pull-up resistors? If not, you should wire up your buttons between the digital pin and ground, and then enable the built-in pull-up resistor using pinMode(pin_C, INPUT_PULLUP).

This will block your entire sketch, preventing any other switches to be polled, which is probably not what you want.

You'll have to detect when a button becomes pressed (released) rather than when it is pressed (released).


Try this: Control Surface: NoteButton.ino

#include <Control_Surface.h> // https://github.com/tttapa/Control-Surface
 
// Instantiate a MIDI over USB interface.
USBMIDI_Interface midi;
 
// Instantiate an array of NoteButton objects
NoteButton buttons[] {
  {
    2,                             // Push button on pin 2
    {MIDI_Notes::C(4), CHANNEL_1}, // Note C4 on MIDI channel 1
  },
  {3, MIDI_Notes::D(4)}, // etc. (shorthand with default channel 1)
  {4, MIDI_Notes::E(4)},
  {5, MIDI_Notes::F_(4)},
  {6, MIDI_Notes::G(4)},
  {7, MIDI_Notes::A(4)},
  {8, MIDI_Notes::B(4)},
};
 
void setup() {
  Control_Surface.begin(); // Initialize Control Surface
}
 
void loop() {
  Control_Surface.loop(); // Update the Control Surface
}
#include <USB-MIDI.h>

USBMIDI_CREATE_DEFAULT_INSTANCE();

const byte C = 2;
const byte D = 3;
const byte E = 4;
const byte F = 5;
const byte G = 6;
const byte A = 7;
const byte B = 8;

void setup() {}

void loop() {
  if (digitalRead(C) == HIGH) MIDI.sendNoteOn(60, 100, 1);
  if (digitalRead(D) == HIGH) MIDI.sendNoteOn(62, 100, 1);
  if (digitalRead(E) == HIGH) MIDI.sendNoteOn(64, 100, 1);
  if (digitalRead(F) == HIGH) MIDI.sendNoteOn(65, 100, 1);
  if (digitalRead(G) == HIGH) MIDI.sendNoteOn(67, 100, 1);
  if (digitalRead(A) == HIGH) MIDI.sendNoteOn(69, 100, 1);
  if (digitalRead(B) == HIGH) MIDI.sendNoteOn(71, 100, 1);
  delay(1000);
  MIDI.sendNoteOff(60, 0, 1);
  MIDI.sendNoteOff(62, 0, 1);
  MIDI.sendNoteOff(64, 0, 1);
  MIDI.sendNoteOff(65, 0, 1);
  MIDI.sendNoteOff(67, 0, 1);
  MIDI.sendNoteOff(69, 0, 1);
  MIDI.sendNoteOff(71, 0, 1);
}

That did the trick! Thank you!

Thanks for the help! That worked!

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