Go Down

Topic: pwm output o'reilly book (Read 1 time) previous topic - next topic

mr W

Apr 11, 2010, 12:35 am Last Edit: Apr 11, 2010, 12:36 am by mr_W Reason: 1
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++)



Apr 11, 2010, 01:10 am Last Edit: Apr 11, 2010, 01:11 am by digitalColeman Reason: 1
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...

mr W

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.


Code: [Select]

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>[glow]=[/glow]0 to make the LED turn all the way off.
The art of getting good answers lies in asking good questions.

Rusty in Texas

Apr 11, 2010, 10:31 pm Last Edit: Apr 11, 2010, 10:33 pm by Fe2o3Fish Reason: 1
Unless, of course, 'i' has been declared 'unsigned char' in which case the loop will never stop.  But  the LED will turn off completely.  :-D


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...
The art of getting good answers lies in asking good questions.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131