Problem with MIDI control

Hello Arduino forum,

and I hope you had a nice Christmas.

Anyway, I've been tinkering around with the Uno board and ran into some problems when using a potentiometer as MIDI control.

^Plot made using circuits.io

From there I've been using Hairless MIDI serial bridge and loopMidi as a MIDI server.

The code looks like this :

const int sensorPin = A0;

void setup(){
    Serial.begin(9600);
      
    }

void loop(){
  int sensorVal = analogRead(sensorPin);

  Serial.write(sensorVal);
  //Serial.println(sensorVal);

  delay(10);

}

So basically it uses a serial port at 9600 baud and the analog pin A0 as the input.

I found that the 10ms delay was the most stable solution, however I couldn't get it to map to Ableton Live or any soft synths for some reason.. the MIDI indicators did flash though and by accident I got an erratic movement on a mapped knob but that's about it.

With this method there's a lot of goofing around with the serial ports and the baud rates because if you try to alter the code and upload it the IDE will notify that the port is busy until you close the connections. (edit : the serial MIDI bridge is set to 115200 by default so that should be checked first..)

Any improvement ideas?

-ef

EDIT : I also found (using the serial plotter) that the pot is very unstable... what could be the cause for it?

EDIT 2 : The code is copied/modified from the TMP36/Love-o-meter code which is used to track temperature.

EDIT 3 : Fixed the code..

EDIT 4 : I've also been working on a logging script for Live in Powershell and I actually tried to get it writing a log file from the serial monitor but it didn't work..

EDIT 5 : Nevermind the ground connection; it's there to clear things up because I made the circuit on a breadboard.

EDIT 6 : I tried removing the Serial.println and it worked to some extent..

You're just sending ASCII over a Serial connection, that's not MIDI. Take a look at my MIDI controller library.

Pieter

PieterP:
You're just sending ASCII over a Serial connection, that's not MIDI. Take a look at my MIDI controller library.

Pieter

Thanks..

I just noticed the Logo stuff and added the

Serial.write

to it but it's still very unstable and the serial MIDI bridge crashes quite often.

Here's an article I wrote on sending MIDI using an Arduino. It's still a work in progress, but the first four chapters (on the MIDI protocol, MIDI hardware, sending MIDI and MIDI controllers) are finished. If you want to use Hairless, you don't have to worry about the MIDI hardware, just make sure that the baud rate in your sketch matches your setting in Hairless.

https://tttapa.github.io/Arduino/MIDI/Chap00-Arduino-MIDI.html

Pieter

I also tried Serial-MIDI converter but it didn't install..

EDIT : I also tried Jack but it's a PITA to setup... also there should be a scanner for i/o (like Linux nmap or something like that)

First off the analogRead returns a number between 0 and 1023 and MIDI parameters are from 0 to 127.
Next you need to tell the MIDI that you are going to send controller messages on a specific channel. Then you need to tell it the controller message what number controller to use and finally you need to send the value of the controller.

So the code to send a MIDI message should be

 //  send a MIDI message
 void midiSend(byte cmd, byte data1, byte data2) {
  cmd = cmd | byte(midiChannel);  // merge channel number
  Serial.write(cmd);
  Serial.write(data1);
  Serial.write(data2);
}

Where midiChannel is a global variable, set it to zero

And the data value is the value you read from the pot divide by 8.

So basically it uses a serial port at 9600 baud

Don't use that use 115200 at the Arduino.

for the unsable pot there is a library called "ResponsiveAnalogRead" which i used for stabeling the pots it works like a charm. As for the code...

#include <ResponsiveAnalogRead.h>

int val = 0;
int lastVal = 0;
int val1 = 0;
int lastVal1 = 0;

const int Pot = A0;
const int Pot1 = A1;


ResponsiveAnalogRead PotRead(Pot, true);

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
PotRead.update();

val = PotRead.getValue()/8;
if(val != lastVal)

  {MIDImessage(176,123,val);}
  lastVal = val;
  
}
void MIDImessage(byte command, byte data1, byte data2)
{
  Serial.write(command);
  Serial.write(data1);
  Serial.write(data2);
  
}

This wored like a charm for me.

Fascinating.. I'll look to it once I get a few things sorted out.

I tried the MIDIUSB library and I'm getting an exit status 1 and an error compiling for board Arduino/Genuino Uno.

efinque:
I tried the MIDIUSB library and I'm getting an exit status 1 and an error compiling for board Arduino/Genuino Uno.

Of course, the Arduino UNO doesn't have USB interface in the main MCU, so it doesn't support the MIDIUSB library.

PieterP:
Of course, the Arduino UNO doesn't have USB interface in the main MCU, so it doesn't support the MIDIUSB library.

Sorry... how stupid of me.