Thanks again mike, I was able to implement your code. Thanks for that.

I'm still having issues with max and min values coming in from the ir sensors,

As the values come in I then compare them to a max and min value if the incoming value is greater then the max I rewrite the max value to the new one, and then the same for the min value.

I then use the distance between the two to map it to 0-127 on my cc midi value.

But every now and again a greatly abnormal value comes in and redefines the max or min leaving me with the ability to turn the cc value say between 40-90% and never going back to zero, unless I get an abnormal value again?

I'm trying to implement a second level of logic to count the number of times a value comes in, if I get the same value too often I then consider the value as a realistic max or min and rewrite the max or min again.

Am I going about it the right way or does anyone have better way to self correct an ir input?