Pages: [1]   Go Down
Author Topic: which less power consumption?  (Read 179 times)
0 Members and 1 Guest are viewing this topic.
jordan
Offline Offline
Jr. Member
**
Karma: 0
Posts: 56
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is second code less power consumption than first code?

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

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

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 508
Posts: 31405
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Sr. Member
****
Karma: 19
Posts: 486
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: