Two quick basic questions relating to digital/analog Wite

Hi all

Apologies in advance, as I am sure these will have been answered somewhere on here before (probably a number of times).

  1. If i am using a pin for pwm that also gets "turned off and on" a number of times, is using analogWrite(0) the same as digitalWrite(LOW)? Or is one better over the other? (I have this idea that digitalWrite(LOW) is better, but it is causing me to write more elaborate methods than prob needs to be, coming from an idea in my head that may not actually be true ha).

  2. I am using quite a lot of digitalRead() checks... I am wondering if there is benifits over all to getting rid of the checks? So if I have a pin as low and send it low again is that a more intensive operation for the arduino than checking with digitalRead() then not sending it, vs the two operations (check then set pin low) if it was actually needed. (i think that made sense).


Edit: trying to clarify 2.

void ChangePinState1(int pin, bool state)
{
digitalWrite(pin, state);
}

void ChangePinState2(int pin, bool state)
{
if(digitalRead(pin) != state)
{
digitalWrite(pin,state);
}
}

Is there any benifit to using the second method over the first?


Cheers Scott,

P.s just incase... By "better" I mean how hard I am making the arduino work.

  1. analogWrite(pin,0) simp!y calls digitalWrite (pin,0). Define "better"

  2. I don't understand the question.

Thanks AWOL.

I had a feeling someone may ask me to define "better" which is why i added the p.s.

I just thought that analog calls are more intensive than digital calls and wasn't sure about analogWrite(0).

No worries on the second point.

(1) As AWOL wrote, analog writing a pin to LOW simply calls digitalWrite for that pin to be a LOW.

(2) digitalWrite is somewhat intensive because it has to map the pin to the appropriate port and bit for the processor that is in use. However, you should concentrate on getting your program correct and not worry yet about optimizing it.

Donald Knuth wrote the following statement on optimization:

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."

if I have a pin as low and send it low again

If you previously wrote LOW to the pin, either with digitalWrite(LOW) or analogWrite(0) then it will not have changed since then, so why do you need to check by reading its state ? Keep a variable in the same state as the pin and you can use that value later if you need to know it.

Cheers guys that was helpfull.

I have been writing up stuff for two motor controllers (each using 4 pins) and using IR also. The code was getting a bit messy and while I was trying to clean it up a bit those questions sprang to mind.

scottbnoob:
P.s just incase... By "better" I mean how hard I am making the arduino work.

The Arduino CPU is powered-off, sleeping, or running, which could make a difference in how 'hard' it works.

All your alternatives belong to the class running.
It is not harder for a CPU to delay than to multiply.

Whandall..... It seems like you may have taken a bit of an excption with my p.s statement for not linking it back to my questions. If so, lighten up. It did make me laugh a little tho so thanks :slight_smile:

There is nothing to make a CPU (ATmega) work 'harder'.

It runs, or it doesn't. That's it.

Oh right, I thought you were joking/nitpicking... :confused:
Is a cpu doing two calculations not using twice as much processing power than if it were doing one calculation? (or is that not defined as "working harder"?)
Is what vaj4088 said correct?

Tasks can take a different numbers of cycles, shure.

So there is sometimes a more efficient way (in cycles per operation) to accomplish things,
but this only matters if there are many things to do in a tight time frame (the 3%).

The CPU just does not care what it executes, or how efficient the executed code is.

Okay thanks for that Whandall.

"The CPU just does not care what it executes, or how efficient the executed code is".

It was me who cared, in that I had some ideas in my head about how I should be doing certain things (as per my original questions), but then was questioning if they really mattered.

Anyway cheers all :slight_smile: