control Constant Current-Electromagnet

allrigth seems the mosfet was half broken.

its the 3rd one i have destroyed since this morning. now it works fine.
trying to find why they keep breaking

this is the ultrasimple code to send values from vvvv to arduino. It shouldn't cause any issues for all i know

const int bufferSize = 20; // how big is the buffer

char buffer[bufferSize]; // Serial buffer
char commandBuffer[10]; // array to store a command
char pinBuffer[3]; // array to store a pin number
char valueBuffer[10]; // array to store a value
int ByteCount; // how many bytes arrived
boolean ledON; // state of the LED
int pinNumber; // pinNumber
int value; // brightness value

void setup()
{
//start the serial communication at the speed of 9600 baud
Serial.begin(9600);
}

void loop()
{
SerialParser();

//if something arrived
if (ByteCount > 0)
{
if (ledON)
{
analogWrite(pinNumber, value);
}
else
{
digitalWrite(pinNumber, LOW);
}
}
}

void SerialParser()
{
ByteCount = -1;

// if something has arrived over serial port
if (Serial.available() > 0)
{
//read the first character
char ch = Serial.read();

//if it's 's', then it's the start of the message
if (ch == 's')
{
//read all bytes of the message until the newline character ('\n')
ByteCount = Serial.readBytesUntil('\n',buffer,bufferSize);

//if the number of arrived bytes > 0
if (ByteCount > 0)
{
// copy the string until the first ','
strcpy(commandBuffer, strtok(buffer, ","));

// copy the same string until the next ','
strcpy(pinBuffer, strtok(NULL, ","));
strcpy(valueBuffer, strtok(NULL, ","));
if (strcmp(commandBuffer, "LED_ON") == 0)
{
ledON = true;
}
else
{
ledON = false;
}

// convert the string into an 'int' value
pinNumber = atoi (pinBuffer);

// convert the sting into a 'float' value and bring it to (0..255) range;
value = atof(valueBuffer) * 255;
}

// clear contents of buffer
memset(buffer, 0, sizeof(buffer));
Serial.flush();
}
}
}