Receiving pitch bend and handling problems

Hi everyone.
I just tried with Midi library getting incoming pitch bend data and use it for fading led. But I cannot get it to proper work.
Could you guys give me some advice how to do? How to write function?

#include <MIDI.h>

const int led = 6;

void setup{
pinMode(led, OUTPUT);

void HandlePitchBend(byte channel, byte bend){

unsigned int fade = map(bend,-8192.0,0.8191,0,255);

analogWrite(led, fade);


void loop(){;

You pass into the function HandlePitchBend() a byte (unsigned 8 bits, 0-255) and then you map that into fade expecting the bend variable to be between -8192.0 and 0.8191. Please review the function definition of map() as it does not take floating point numbers and your variable you are ‘mapping’ is not a floating point number to start with.

I successfully fading led using CC messages even with note on, if note = 127, the led is full lighting and is proportional fade to 0 completely dark according to incoming notes. But with pitch bend I cannot.

Ok I close topic, I get it to work. I writing again function which works like I except.

void HandlePitchBend(int channel, int bend){
int fade = constrain(map(bend,-8192,8191,0,127),0,127);

analogWrite(led, fade);

blh64 thank you for advice.