okay, so I think I understand you. so what would you think would be the code that would work for the first 3 rows?
I'm not sure what you are asking, but here's your sketch with some modifications. It should send note 120 messages on and off.
This is how you did it in the earlier sketches, as far as I can see. N_BUTTONS was used so you weren't going off the end of the array I don't think.
Arrays work, there should be no problem using an array element instead of a constant, unless of course you try an array element that does not exist.
#include <MIDI.h>
#define button 2
MIDI_CREATE_DEFAULT_INSTANCE();
int port1[3] = {115, 120, 125}; //… three elements initialized
void setup()
{
MIDI.begin(MIDI_CHANNEL_OFF);
pinMode (button, INPUT_PULLUP);
}
void loop()
{
Serial.println(digitalRead(button));
delay(100);
if (digitalRead(button) == HIGH) {
MIDI.sendNoteOn(port1[1], 127, 1);
}
else {
MIDI.sendNoteOff(port1[1], 0, 1);
}
}
Sorry to be on one note here, but i continue to wonder how you can use USB and serial TX0 and expect separate MIDI messages, can someone explain how it is? I can almost understand using USB MIDI and serial monitor output at the same time, but placing MIDI messages on both seems like it just couldn't work.
a7
Hi, I don't know the true difference yet, but I do know that it somehow has to work. A youtuber (from witch I eddited the code) also made it. (https://www.youtube.com/watch?v=ZjzAyPGLrOA) so I dont know why it doesn't work.
Hi everybody,
I am working on a project that sends a midi signal through a usb port and a physical din port when a button is pressed. I have the two separate things working (the usb part and the physical din part) but when I want to combine those, only the usb part works. the code I am using to try and make this work looks like this:
#include <MIDIUSB.h>
#include <MIDI.h>
const int N_BUTTONS = 2;
int ButtonPin[N_BUTTONS] = { 2, 3 };
int buttonNote[N_BUTTONS] = {36, 38, };
int ButtonPortNote[N_BUTTONS] = {120, 121,};
int ButtonState[N_BUTTONS] = { 0 };
int ButtonPState[N_BUTTONS] = { 0 };
MIDI_CREATE_DEFAULT_INSTANCE();
unsigned long lastDebounceTime[N_BUTTONS] = { 0 };
unsigned long debounceTimer[N_BUTTONS] = { 0 };
int debounceDelay = 30;
int BUTTON_CH = 0;
void setup() {
Serial.begin(9600);
for (int i = 0; i < N_BUTTONS; i++) {
pinMode(ButtonPin[i], INPUT_PULLUP);
}
MIDI.begin(MIDI_CHANNEL_OFF);
}
void loop(){
for (int i = 0; i < N_BUTTONS; i++) {
ButtonState[i] = digitalRead(ButtonPin[i]);
debounceTimer[i] = millis() - lastDebounceTime[i];
if (debounceTimer[i] > debounceDelay) {
if (ButtonState[i] != ButtonPState[i]) {
lastDebounceTime[i] = millis();
if (ButtonState[i] == LOW) {
noteOn(BUTTON_CH, buttonNote[i], 127);
MIDI.sendNoteOn(ButtonPortNote[i], 127, 1);
MidiUSB.flush();
Serial.print("Button ");
Serial.print(i);
Serial.print(" ");
Serial.println("on");
} else {
noteOn(BUTTON_CH, buttonNote[i], 0);
MIDI.sendNoteOn(ButtonPortNote[i], 0, 1);
MidiUSB.flush();
Serial.print("Button ");
Serial.print(i);
Serial.print(" ");
Serial.println("off");
}
ButtonPState[i] = ButtonState[i];
}
}
}
}
//-------
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = { 0x09, 0x90 | channel, pitch, velocity };
MidiUSB.sendMIDI(noteOn);
}
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = { 0x0B, 0xB0 | channel, control, value };
MidiUSB.sendMIDI(event);
}
the code for the usb midi looks like this:
#include <MIDIUSB.h>
const int N_BUTTONS = 2;
int ButtonPin[N_BUTTONS] = { 2, 3 };
int buttonNote[N_BUTTONS] = {
36,
38,
};
int ButtonState[N_BUTTONS] = { 0 };
int ButtonPState[N_BUTTONS] = { 0 };
unsigned long lastDebounceTime[N_BUTTONS] = { 0 };
unsigned long debounceTimer[N_BUTTONS] = { 0 };
int debounceDelay = 30;
int BUTTON_CH = 0;
void setup() {
Serial.begin(9600);
for (int i = 0; i < N_BUTTONS; i++) {
pinMode(ButtonPin[i], INPUT_PULLUP);
}
}
void loop() {
for (int i = 0; i < N_BUTTONS; i++) {
ButtonState[i] = digitalRead(ButtonPin[i]);
debounceTimer[i] = millis() - lastDebounceTime[i];
if (debounceTimer[i] > debounceDelay) {
if (ButtonState[i] != ButtonPState[i]) {
lastDebounceTime[i] = millis();
if (ButtonState[i] == LOW) {
noteOn(BUTTON_CH, buttonNote[i], 127);
MidiUSB.flush();
Serial.print("Button ");
Serial.print(i);
Serial.print(" ");
Serial.println("on");
} else {
noteOn(BUTTON_CH, buttonNote[i], 0);
MidiUSB.flush();
Serial.print("Button ");
Serial.print(i);
Serial.print(" ");
Serial.println("off");
}
ButtonPState[i] = ButtonState[i];
}
}
}
}
//-------
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = { 0x09, 0x90 | channel, pitch, velocity };
MidiUSB.sendMIDI(noteOn);
}
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = { 0x0B, 0xB0 | channel, control, value };
MidiUSB.sendMIDI(event);
}
and the code that I am using for checking that the physical port is working looks like this:
#include <MIDI.h>
#define button 2
MIDI_CREATE_DEFAULT_INSTANCE();
int port1[button] = {120};
void setup()
{
MIDI.begin(MIDI_CHANNEL_OFF);
pinMode (button, INPUT_PULLUP);
}
void loop()
{
Serial.println(digitalRead(button));
delay(100);
if (digitalRead(button) == HIGH) {
MIDI.sendNoteOn(port1[button], 127, 1);
}
else {
MIDI.sendNoteOff(120, 0, 1);
}
}
here are some pictures of how the project is wired and how it looks.
Maybe it is something simple, but I am out of ideas . If you have any ideas of how I can make this work I would be very thankfull.
You have to decide which library you want to use (for USB MIDI, bc DIN5 connector is just UART)
what? you have not activated Serial
Okay, I'm a bit of a noob, so how do I do that?
what? to decide? just say magic words "MIDI.h i choose you"
#include <MIDI.h>
#define button1 2
#define button2 3
byte buttonNote[] = { 36, 38 };
MIDI_CREATE_DEFAULT_INSTANCE();
void setup(){
Serial1.begin(31250);
MIDI.begin(MIDI_CHANNEL_OFF);
pinMode (button1, INPUT_PULLUP);
pinMode (button2, INPUT_PULLUP);
}
void loop(){
if (!digitalRead(button1)) {
MIDI.sendNoteOn(buttonNote[0], 127, 1);
note(0x90, buttonNote[0], 127);
}
else {
MIDI.sendNoteOff(buttonNote[0], 0, 1);
note(0x90, buttonNote[0], 0);
}
if (!digitalRead(button2)) {
MIDI.sendNoteOn(buttonNote[1], 127, 1);
note(0x90, buttonNote[1], 127);
}
else {
MIDI.sendNoteOff(buttonNote[1], 0, 1);
note(0x90, buttonNote[1], 0);
}
}
void note(byte cmd, byte pitch, byte velocity) {
Serial1.write(cmd);
Serial1.write(pitch);
Serial1.write(velocity);
}
@larsg ,
Your two or more topics on the same or similar subject have been merged.
Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.
Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.
Repeated duplicate posting could result in a temporary or permanent ban from the forum.
Could you take a few moments to Learn How To Use The Forum
It will help you get the best out of the forum in the future.
Thank you.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.