analogWrite Question

Hi All,

I just had a quick question regarding AnalogWrite

The method help says that After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin).

I am just doing a basic example for my kids on how the Arduino works.

what I would like to do is

void setup(){ 

analogWrite(9, 100);

}

void loop(){

}

Not

void setup(){ 


}

void loop(){

analogWrite(9, 100);


}

I don't understand why I can't just set analogWrite in the setup loop.
Why do I have to put it in the loop method and have it called over and over?

The help file says

After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin).

Please explain.

Much appreciated

Stuart

You can. Please post code that actually compiles. It is void not Void.

Thanks for that Nick,

Just a typo as I did not copy and paste.

My question still remains, any ideas.

Please post the actual code you say causes problems.
Say which version of the IDE you are using.

Stuartmp:
Just a typo as I did not copy and paste.

Please copy and paste, then we can advise you.

As I said in my earlier reply.

My question still remains, any ideas.

Yes, I suggest you post the code you are using.

How to use this forum

Please use code tags.

Read this before posting a programming question

For all we know, you did something else in loop() that cancelled the analogWrite. That's why we ask you to copy and past the code you actually used, not just vaguely describe it.

Sorry, I am using AtmelStudio IDE

Both blocks of code work.

However the in the first block of code the LED only flashes for a very brief moment.

(This is my point, I would assume it would just stay on!)

In the second block of code, the LED stays on because its in a loop which continuously calls analogWrite.

This is my question, why doesn't the first block of code keep the LED on continuously until some other call to that pin. Just like it says in the help file.

I just tried it in the Arduino IDE 1.6.5 and it works fine as expected.

so I just went back to AtmelStudio and tried it again, still only a flash.

Then I tried to compile it in the release configuration and it works.

It is something to do with the Debug configuration.

I turned off 'Report Digitals' and its working fine.

Sorry to bother you all.

It must be when the 'Report Digitals' dialog loads it resets the pins.

It must be when the ‘Report Digitals’ dialog loads it resets the pins.

It would have to make a call to pinMode() to make the pin input, so that it could then meaningfully call digitalRead(), which would, of course, turn off PWM on that pin.