Thanks for the advice, I have altered the script according to your suggestion (and simplified it a bit more):
#define PWM_PIN 3 // pin 3 because: pwm
void setup() {
pinMode(PWM_PIN, OUTPUT); // set PWM_PIN as an output
analogWrite(PWM_PIN, 0); // set initial value to zero
delay(1000);
}
void loop(){
int val = analogRead( A0 ); // read potentiometer
val = map(val, 0, 1023, 0, 255); // map the analog value from max 1023 to max 255
analogWrite(PWM_PIN, val ); // send value to the mosfet
delay(100); // lower the polling rate a little
}
The tabbing is a bit strange, it looks better in the IDE.
Somehow the script doesn't work as well with turning on the fan, so I must be doing the mapping wrong. Maybe it's because I'm now trying it with a 1K Ohm potentiometer. I'll switch it out for a 10K one and try again.
Update:
Switched out the 1K for a 10K pot, but no success, it seems that the setup only runs intermittently, I'm not sure what could be the problem, reseating the wires also didn't help, going to try it with a different Arduino now to see if the Uno is dying.