Midi controller error

Hi All. I'm ner to this and so I apologise if this is a dumb mistake. I've got code to (or I's like to) on the press of any of my 4 switches change a patch or send any commnad really in the long run. the code nelow does accept a button press but using hairless midi monitor it says +9.445 - Error: got unexpected data byte 0x0.

what am i doing wrong? Ideally I'm looking for a simple you have x amount of buttons an each is hard coded in the code.

any help appreciated :slight_smile: thanks

#include <Bounce2.h>
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE()

// Define MIDI channel and control change number for patch changes
const int midiChannel = 1;  // MIDI channel (1-16)
const int patchChangeCC = 0; // Control Change number for patch changes (0-119)

// Define the digital pins for the switches
const int switch1Pin = 2;
const int switch2Pin = 3;
const int switch3Pin = 4;
const int switch4Pin = 5;

// Create Bounce objects for each switch
Bounce switch1 = Bounce();
Bounce switch2 = Bounce();
Bounce switch3 = Bounce();
Bounce switch4 = Bounce();

// Variable to store the current patch number
int currentPatch = 1;

void setup() {
  // Initialize MIDI
  MIDI.begin(midiChannel);

  // Set the switches as inputs
  pinMode(switch1Pin, INPUT_PULLUP);
  pinMode(switch2Pin, INPUT_PULLUP);
  pinMode(switch3Pin, INPUT_PULLUP);
  pinMode(switch4Pin, INPUT_PULLUP);

  // Set up the Bounce objects
  switch1.attach(switch1Pin, INPUT_PULLUP);
  switch1.interval(10);
  
  switch2.attach(switch2Pin, INPUT_PULLUP);
  switch2.interval(10);
  
  switch3.attach(switch3Pin, INPUT_PULLUP);
  switch3.interval(10);
  
  switch4.attach(switch4Pin, INPUT_PULLUP);
  switch4.interval(10);
}

void loop() {
  // Update the Bounce objects
  switch1.update();
  switch2.update();
  switch3.update();
  switch4.update();

  // Check for switch changes and send MIDI patch change messages
  if (switch1.fell()) {
    currentPatch = 1;
    sendPatchChange();
  }

  if (switch2.fell()) {
    currentPatch = 2;
    sendPatchChange();
  }

  if (switch3.fell()) {
    currentPatch = 3;
    sendPatchChange();
  }

  if (switch4.fell()) {
    currentPatch = 4;
    sendPatchChange();
  }

  // Other code can be added here as needed
}

void sendPatchChange() {
  MIDI.sendControlChange(patchChangeCC, currentPatch - 1, midiChannel); // Subtract 1 to use values 0-3 for patches
  delay(250);  // Add a delay to debounce the switches and avoid sending multiple messages
}

Have you changed the baud rate in serialMIDI.h ?

no. I left it as default

What Arduino are you running this on?

Uno.

Cheers

What version of the MIDI.h file are you using?
I can't get your code to compile at all with the latest version.

What is more I can't even get the basic I/O example from the library's example to compile for a Uno.

EDIT.
Sorry forget that. I had been working with a modified version of the Library that some one had "fixed" to allow it to run on an R4 Arduino. Seems like that was my problem. So as you were.

OK so you have to add

Serial.begin(115200);

As the last line in your setup function.

As mentioned by
@Deva_Rishi

Sorry that was just to get the Basic I/O example to work. Now looking at your code.

The method supported by the library is actually to modify the line in serialMIDI.h

struct DefaultSerialSettings
{
    /*! Override the default MIDI baudrate to transmit over USB serial, to
    a decoding program such as Hairless MIDI (set baudrate to 115200)\n
    http://projectgus.github.io/hairless-midiserial/
    */
    static const long BaudRate = 31250;  // modify this !
};

But yes after the call to midi.begin(); you can also call

Serial.begin(115200);

and actually that is more elegant and should work just as well.

Sorry but I can't reproduce what you are seeing.
Even after adding the Serial.begin(115200); after the midi.begin .

Basically you do have an error in that you are trying to send

and with setting the :-

you would be sending 0 and patch changes should be in the 1 to 128 range.
In fact all your switches set the current patch before they call sendPatchChange(); so I don't see the point in that, you will always be sending the same number.

However, I can't get Hairless to connect to the serial port with your code, so I can't see the errors you report.

Sorry.

thanks guys :slight_smile:

Ok, as I said I'm new and I have no idea what I am doing. How do I get it to send a commnad to change then?

I want to either have PC change 1-4 or CC change 16,17 etc

thanks again

What do you mean by "etc"?
You have to have a variable for the current CC change. Do not set the CC number before the change call, change it after. That way you can control the range of CC numbers that you use.
So after you send the CC number, then increment the CC number for next time. Do not change the CC number in the sendPatchChange function, like you do currently by subtracting one from it.

This means that if you go over the maximum number in the range you want to produce, then you can use an if statement and switch the next CC number to the minimum number you want to use.

ok so what I mean is I would like to program each button has a set number . it would be midi channel 1, commnad below and the range doesn't matter as it's 0-127

example 1:

switch 1 - pc 0
switch 2 - pc 1
switch 3 - pc 2
switch 4 - pc 3

example 2:

switch 1 - cc 16
switch 2 - cc 17
switch 3 - cc 18
switch 4 - cc 19

Thanks

Why does this list say - PC next to it, I thought this was an Arduino switch you were pressing?

Same as you have it at the moment but do not use the currentPatch - 1, just currentPatch.

I assume it's an abbreviation for program change or patch change.

1 Like

It is indeed :slight_smile:

So how do you expect the same lot of four switches to give you both control change and also give you program change messages?

So you have a function called sendPatchChange that actually sends a control change but with all the syntax of a patch change.

Do you not think this needs sorting out?

So I sorted out the code for setting the control message and value when you press and release any of the four buttons:-

#include <Bounce2.h>
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();

// Define MIDI channel
const int midiChannel = 1;  // MIDI channel (1-16)

// Define the digital pins for the switches
const int switch1Pin = 2;
const int switch2Pin = 3;
const int switch3Pin = 4;
const int switch4Pin = 5;

// Create Bounce objects for each switch
Bounce switch1 = Bounce();
Bounce switch2 = Bounce();
Bounce switch3 = Bounce();
Bounce switch4 = Bounce();

// Variable to store the current patch number
int currentPatch = 1;

void setup() {
  // Initialize MIDI
  MIDI.begin(midiChannel);
  Serial.begin(115200);

  // Set the switches as inputs
  pinMode(switch1Pin, INPUT_PULLUP);
  pinMode(switch2Pin, INPUT_PULLUP);
  pinMode(switch3Pin, INPUT_PULLUP);
  pinMode(switch4Pin, INPUT_PULLUP);

  // Set up the Bounce objects
  switch1.attach(switch1Pin, INPUT_PULLUP);
  switch1.interval(10);
  
  switch2.attach(switch2Pin, INPUT_PULLUP);
  switch2.interval(10);
  
  switch3.attach(switch3Pin, INPUT_PULLUP);
  switch3.interval(10);
  
  switch4.attach(switch4Pin, INPUT_PULLUP);
  switch4.interval(10);
}

void loop() {
  // Update the Bounce objects
  switch1.update();
  switch2.update();
  switch3.update();
  switch4.update();

  // Check for switch changes and send MIDI control message messages
  if (switch1.fell()) {
    sendControlChange(3, 8);
  }

  if (switch2.fell()) {
    sendControlChange(4, 10);
  }

  if (switch3.fell()) {
    sendControlChange(5, 60);
  }

  if (switch4.fell()) {
    sendControlChange(6, 100);
  }

  // Other code can be added here as needed
}

void sendControlChange(char controllerNumber, char controllerValue) {
  MIDI.sendControlChange(controllerNumber, controllerValue, midiChannel); // Subtract 1 to use values 0-3 for patches
  delay(250);  // Add a delay to debounce the switches and avoid sending multiple messages
}

This now works with hairless. This is the messages you get for each of the four buttons.

Now you need to sort out how you want to change the patch or voice of the MIDI sound generator.
An other four buttons or something else?

By the way this code is very poor, when ever you find yourself using three or more lines that basically look the same, it is time to learn how to use arrays. This will be even more important if you add another four buttons to your code.

Thanks. As I said I am brand new to this. Thelast time I coded was over 30 years ago!

It's a steep learning curve.

Anyway it's appreciated

OK.

So maybe next should be perhaps two extra switches one to increment the voice number and another to decrease it.

If it were me part of that function would be that after the patch change is sent, then you send a noteOn and noteOff message for perhaps a middle C so you can here the change in voice as you a temp through them.

Have a go at that and see what you can make of it.

Don't forget to post the new code if you meant to ask questions about it.

1 Like

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