Go Down

Topic: which less power consumption? (Read 280 times) previous topic - next topic

TTU.ABD

Is second code less power consumption than first code?

first code:
Code: [Select]
void loop() {
int x = analogRead(A0);
x=map(x,0,1023,0,255);
analogWrite(11,x);
}


second code:
Code: [Select]
void loop() {
int x = analogRead(A0);
x=map(x,0,1023,0,255);
if(x!=p)
{analogWrite(11,x);
p=x;}
}


Grumpy_Mike

Code does not affect the power consumption of this processor at all.

TanHadron

Well, not in this case anyway.  You could possibly affect power consumption by changing the duty cycle of the PWM output.

But the analogWrite() function simply changes the duty cycle by updating the appropriate OCR value for the timer in use.  if x == p, that value is already the same value and it won't change anything.

Well, if analogWrite() is given a value of 0 or 255, it uses digitalWrite() HIGH or LOW, but then if x == p it will just write that value again and that won't change anything, either.

So no, you're not going to use any more power if you call analogWrite() with the same parameters as the previous time. You might waste some processor time, but that's it.

Go Up