control Constant Current-Electromagnet

Such complex coding, way beyond my ability to understand. Might I suggest you just write a very simple testing sketch where you read a analog input pin that has a pot wired to it. Then divide the results by four and output the value via a analogWrite() command to your mosfet. That would allow you to power your magnet with values form 0 to 255 so you can measure the current value going to the magnet as you rotate the pot.

In the future if posting code please enter it into a code window using the # edit option..

Lefty