pwm output o'reilly book

working my way slowly through the o'reilly book. i have asked about this recently but in this case (as an experiment) it seems that the analog function doesn't just continue regardless of what the rest of the programme is doing.

an led connected to pwm output 9 will grow brighter and dim but not not totally go dark , there will a small amount of current flowing. the led on the board (13) will go on for 5 seconds but the led on 9 will not continue fluctuating it will wait until the led on 13 has turned off. by rights the arduino should understand i want the led to fluctuate reagardless of what the rest of the programme is doing?

the 5000 delay is part of a programme designed to turn a light on for 5 seconds and then switch it off.

interestingly enough as mentioned the led doesn't ever switch off totally. by the time the 5 second delay comes into play the led has already started growign brighter again.

if a remove the 5 second delay on off part of the programme everything is "normal" but i've got no proof that the pwm output is ignoring the rest of the programme and just doing what it has been told to do.

any thoughts?

#define led 9
#define ledx 13

int i=0;

void setup()
{
pinMode(led, OUTPUT);
pinMode(ledx, OUTPUT);

}

void loop()
{
for (i =0; i<255; i++)
{
analogWrite(led,i);
delay(10);
}

for(i=255;i>0;i--)
{
analogWrite(led,i);
delay(10);
}
digitalWrite(ledx,HIGH);
delay(5000);
digitalWrite(ledx,LOW);

}

the delay command actually pauses the whole chip from doing anything, and thus beyond the simplest linear program, you would never want to use delay. so if you read your program in a linear way, you see that the pwm led lights up and then dims (but never reaches 0 because of the way you set up the for loop) and then the pin 13 turns on, waits 5 sec, then turns off and the program then loops back to the pwm for loops. you would have to setup a counter variable and use if statements or look at a time variable (millis(); ) to have the two things turn on and off independently of each other. hope that helps...

i was really experimenting with the pwm programme in the book the only thing i did was add the delay to spice things up and see what it would do and if the pwm output would continue.

for(i=255;i>0;i--)

The middle clause of a for loop is a while condition. The body of the loop is executed while i is greater then 0.

The loop body is not executed when i is zero, so, you never turn the LED all the way off before the 5 second pause.

The other for loop does turn it all the way off, but only for 10 milliseconds, which is not long enough for you to see.

Change the while clause to i>=0 to make the LED turn all the way off.

Unless, of course, 'i' has been declared 'unsigned char' in which case the loop will never stop. But the LED will turn off completely. :smiley:

Unless, of course, 'i' has been declared 'unsigned char' in which case the loop will never stop.

i was declared an int. But, signed or unsigned, the value never goes negative, so the loop will stop. Unless, I'm missing something...