Serial.write messes up the Serial Monitor??

Hello everyone!
So I'm new to arduino and my first bigger project is a midi controller with an ultrasonic sensor.
I wrote the program from the ground up and using serial.println I tested the ultrasonic sensor and everything worked fine.
But once I added the serial.write command to send midi data over USB to my computer the strange symbols and characters started showing up in the Serial Monitor. I'm at a complete loss, hours of googling didn't give me any solutions for this and I have no idea why this is happening. Before this project I already built a simple Midi Controller using a potentiometer and everything worked fine.

Here is my code:

int controlChange = 176; // MIDI Kanal 1
int controllerNummer = 21;
int controllerWert = 0;
int triggerPin = 3;
int echoPin = 2;
long messung = 0;
long ergebnis = 0;

void setup() {
Serial.begin(9600);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {

digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);

messung = pulseIn(echoPin, HIGH);
ergebnis = (messung/2)/29;
if (ergebnis<100){
controllerWert = map(ergebnis,0,50,0,127);
constrain(controllerWert,0,127);

Serial.write(controllerWert); // This is the midi value I am trying to send!
Serial.println(controllerWert);
}
}

I also attached a screenshot of my Serial Monitor giving me those weird values I was talking about.

I really hope someone can help me with this.

Serial Monitor.png

I'm at a complete loss

You send binary data to a program that expects ASCII data, and you don't understand why the program that expects ASCII data doesn't deal with the binary data in the way that you expect?

Well, how is it supposed to know what you expect it to do with the binary data?

First, quote tags are NOT code tags. Have a look at How to use the forum and edit your post accordingly.

And to the problem, I think your expectation is Serial.write() is wrong. It writes raw data to serial. Aka, if that data isn't a valid printable ASCII character then yes, it will print garbage :slight_smile:

Serial.write writes a byte; serial monitor interprets (or tries to) that as an ascii character.

Please replace the quote tags in your post by code tags. And you are aware that you can copy content from the serial monitor and paste it here; better than posting screenshots.

  constrain(controllerWert,0,127);

It is pointless to constrain the value, when you don't care what the constrained value is.