hilukasz:
The code implies it should go all the way down.
Not at all...
This sequence:
digitalWrite(spin,HIGH);
delayMicroseconds(sp*freq);
digitalWrite(spin,LOW);
Doesn't take 0 clock cycles to execute. The LED will be on for the time it takes to do multiply sp*freq, call/return from delayMicroseconds, do the digitalWrite(LOW), etc.
You might also get a CPU interrupt in the middle of it, slowing it down even more and causing bright flashes.
Try this:
void spwm(int freq,int spin,int sp){
if (sp!=0) {
//on
digitalWrite(spin,HIGH);
delayMicroseconds(sp*freq);
// off
digitalWrite(spin,LOW);
delayMicroseconds(sp*(255-freq));
}
}