analogWrite(fanPin,0); will turn the fan off, so it's doing exactly what you're telling it to
Put an analogWrite(fanPin,0); in the "Fan Off" section.
Take it out of the rest of the sections. Take the delays out, too. No need to delay that I can see at least.
The output pin will put out whatever you tell it to until you tell it to do something else, so if it writes 150 to the pin (bit over half, 127 is technically half) it will continue to do so until it is told to write something else to it.
Rather nice, as that means it will simply sit at whatever speed while the Arduino checks the environment or whatever else it's doing.
Downside: When you want it to sleep due to no light, you'll need to put a line in to shut the fan down if it's running. Having an analogWrite(fanPin,0); outside the if(LDR > whatever) statement will do that for you.
Or you can be snazzy and under the closing } for the if statement you can put
Code:
else{
analogWrite(fanPin,0);
}
That way you aren't turning the fan off at the end of the pass through your loop, only to turn it back on again during the next loop.
Party!
As a total sidenote, a resistor somewhere in the 100-470 ohm range between the Arduino and the Mosfet gate pin is a good idea. Not really needed at the standard PWM speeds (500Hz), but very much required at high speeds and a good habit to get into.
Were it a straight transistor (rather than a mosfet) having no resistor would blow the Arduino to bits (ok, would kill the pin).