Arduino Midi Library Serial Error

Hi, I seem to be having some trouble getting the Arduino midi library to print correctly to the serial, I get random symbols appearing like this "⸮$⸮$ ⸮$⸮$ ⸮$⸮$ ⸮$⸮$ ⸮$⸮$" instead of the midi notes I want printing. Because of this I cant then connect to hairless Midi and then connect to Ableton. I did have this script working at one point and I haven't changed it since so I'm not sure why the serial is not working. I also have the serial set to a baud rate of 115200 which I need.

This is the code I'm using below, any help would be greatly appreciated thanks!!

> /*
  Based on Sketch built by Gustavo Silveira (aka Music Nerd)
  Modified by Dolce Wang

  This code is only for Arduinos that use ATmega328 (like Uno, Mega, Nano, Beetle...)

*/


// Change values with //*** 


// LIBRARIES

#include <MIDI.h> // by Francois Best
MIDI_CREATE_DEFAULT_INSTANCE(); 


// BUTTONS
const int NButtons = 1; //***  total number of push buttons
const int buttonPin[NButtons] = {2,}; //*** define digital pins connected from button to Arduino; (ie {10, 16, 14, 15, 6, 7, 8, 9, 2, 3, 4, 5}; 12 buttons)
                                        //** Button NOTE will go up chromatically.  ie. if button is digi pin 2, C; Pin 3, C#; Pin 3, D; etc
                                   
int buttonCState[NButtons] = {};        // stores the button current value
int buttonPState[NButtons] = {};        // stores the button previous value


// debounce
unsigned long lastDebounceTime[NButtons] = {0};  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    //* the debounce time; increase if the output flickers

// POTENTIOMETERS
const int NPots = 1; //*** total numbers of pots (slide & rotary)
const int potPin[NPots] = {A0}; //*** Analog pins of each pot connected straight to the Arduino i.e 4 pots, {A3, A2, A1, A0};
                                          // have nothing in the array if 0 pots {}

int potCState[NPots] = {0}; // Current state of the pot; delete 0 if 0 pots
int potPState[NPots] = {0}; // Previous state of the pot; delete 0 if 0 pots
int potVar = 0; // Difference between the current and previous state of the pot

int midiCState[NPots] = {0}; // Current state of the midi value; delete 0 if 0 pots
int midiPState[NPots] = {0}; // Previous state of the midi value; delete 0 if 0 pots

const int TIMEOUT = 300; //** Amount of time the potentiometer will be read after it exceeds the varThreshold
const int varThreshold = 10; //** Threshold for the potentiometer signal variation
boolean potMoving = true; // If the potentiometer is moving
unsigned long PTime[NPots] = {0}; // Previously stored time; delete 0 if 0 pots
unsigned long timer[NPots] = {0}; // Stores the time that has elapsed since the timer was reset; delete 0 if 0 pots


// MIDI
byte midiCh = 1; //** MIDI channel to be used; You can add more if you need to reorganize or have a billion buttons/pots
byte note = 36; //** First note to be used for digital buttons, then go up chromatically in scale according to the sequence in your "buttonPin" array
                // you can look up on a Midi Note chart; 36=C2; 60=Middle C
byte cc = 1; //** First MIDI CC to be used for pots on Analog Pins in order of the "potPin" array; then goes up by 1

// SETUP
void setup() { 
  MIDI.begin();
  Serial.begin(115200); //**  Baud Rate 31250 for MIDI class compliant jack | 115200 for Hairless MIDI

  
  // Buttons
  // Initialize buttons with pull up resistors
  for (int i = 0; i < NButtons; i++) {
    pinMode(buttonPin[i], INPUT_PULLUP);
  }

}


// LOOP
void loop() {

  buttons();
  potentiometers();

}


// BUTTONS
void buttons() {

  for (int i = 0; i < NButtons; i++) {

    buttonCState[i] = digitalRead(buttonPin[i]);  // read pins from arduino


    if ((millis() - lastDebounceTime[i]) > debounceDelay) {

      if (buttonPState[i] != buttonCState[i]) {
        lastDebounceTime[i] = millis();

        if (buttonCState[i] == LOW) {

          // Sends the MIDI note ON accordingly to the chosen board

MIDI.sendNoteOn(note + i, 127, midiCh); // note, velocity, channel


        }
        else {

          // Sends the MIDI note OFF accordingly to the chosen board

MIDI.sendNoteOn(note + i, 0, midiCh); // note, velocity, channel

        }
        buttonPState[i] = buttonCState[i];
      }
    }
  }
}


// POTENTIOMETERS
void potentiometers() {


  for (int i = 0; i < NPots; i++) { // Loops through all the potentiometers

    potCState[i] = analogRead(potPin[i]); // reads the pins from arduino

    midiCState[i] = map(potCState[i], 0, 1023, 0, 127); // Maps the reading of the potCState to a value usable in midi

    potVar = abs(potCState[i] - potPState[i]); // Calculates the absolute value between the difference between the current and previous state of the pot

    if (potVar > varThreshold) { // Opens the gate if the potentiometer variation is greater than the threshold
      PTime[i] = millis(); // Stores the previous time
    }

    timer[i] = millis() - PTime[i]; // Resets the timer 11000 - 11000 = 0ms

    if (timer[i] < TIMEOUT) { // If the timer is less than the maximum allowed time it means that the potentiometer is still moving
      potMoving = true;
    }
    else {
      potMoving = false;
    }

    if (potMoving == true) { // If the potentiometer is still moving, send the change control
      if (midiPState[i] != midiCState[i]) {

        // Sends the MIDI CC 
        MIDI.sendControlChange(cc + i, midiCState[i], midiCh); // cc number, cc value, midi channel
            

        potPState[i] = potCState[i]; // Stores the current reading of the potentiometer to compare with the next
        midiPState[i] = midiCState[i];
      }
    }
  }
}

Yes you will. This is because the values you get from MIDI messages do not make any sense when printed as ASCII characters.

You have to use some code the other side of hairless like MidiMonitor to decode these values and see the MIDI messages.

You may want to read this before you proceed:-
how to get the best out of this forum
Also it will tell you how to post code correctly for this forum by using code tags.

Who wrote that? The Beetle does NOT use a ATmega328 processor.

Hi thanks for the tips, il look into MidiMonitor. Have you had this issue before and is it an easy fix? I just got the code off of some Arduino forum

I looked into the midi monitor but since hairless Midi cant read the ASCII Signals Nothing seems to happen. Do you know exactly why It would be printing in ASCII and how I could translate that to Midi for Hairless Midi to Interpret?

This is why debugging MIDI can be tricky.
If you post the code properly I could have a look at it. But as it stands it is way too fragmented to copy.

In general you are trying to get too much done for an initial first attempt at communicating. I think you are miss understanding how to use this library. Just start with some code that sends a couple of random notes. Or one of the simpler examples that come with the library.

Hi I understand, I've formatted the code if you could have a look that would be greatly appreciated. At this point all I want is to trigger a midi note with a button and get it into a DAW. So I don't really need the potentiometers just a button. Thanks for the help

OK it is long and way over complex code in the first place but I find it works for the single button it is written for. I removed the call in the loop function to look at the potentiometers.

I used it to feed into midi monitor, here is a screen dump.

I also connected it into Ablton Live to play the single note.

There is no need to use MIDI monitor as hairless in effect does that for you. There is no need to open up the serial monitor at all. In fact if you do it will stop hairless from being able to open it up.
So not sure why you are not getting anything.
These tests are done on a Mac, what are you using?

Edit. Have you checked the preferences on the hairless app that the baud rate is set to 115200 and no other value?

How have you wired the push button? It should be from the input to ground.

great thank you, I have cleaned it up and got it working. I had the serial monitor open, as soon as I closed it, It started working. Thank you so much for your help, as I've found it really hard to find resources to fix issues with hairless midi.

1 Like

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