Led on Led send

What is up with MIDPOINT and manipulating val? What is up with the averaging code?

The code code does something. You have not described that.
You want it to do something more. You haven't described what it doesn't do.

The delay() is going to kill you. Read, understand, and embrace the code in Blink Without Delay.