Brett Reinhard - 1 year ago 82
C Question

# Arduino (C? Based) Programming for Energy Efficiency

I have a question I haven't really been able to find an answer to this specific question.

I recently started a project to setup an Arduino Board to control various LED's to transition through the spectrum from Red to Yellow, Yellow to Green,
Green to Teal, Teal to Blue, Blue to Magenta, Magenta to Red.
Uses Pointers to Map Each RGB Value for full spectrum, based
off 255 indexed array.

My question is in regards to writing code that is energy efficient. I need it to be as efficient as possible because my project will be running off 4 rechargeable AA batteries (1.5V AA each).

How can I write energy efficient code? Or is this a question of futility?

I am running off the assumption (could be very inaccurate) that after the setup is completed there will be less energy consumed during the loop process.

The code in question is below:

``````int const RED = 3 /* Pin 3 */, BLUE = 5 /* Pin 5 */, GREEN = 6 /* Pin 6 */;
int* _RED[1536];
int* _GREEN[1536];
int* _BLUE[1536];
int RGB[256];

void setup() {
for (int i = 0; i<256;++i){
RGB[i] = i;
}
//RED TO YELLOW
for (int i = 0 ; i<256;++i){
_RED[i] = &RGB[255];
_GREEN[i] = &RGB[i];
_BLUE[i] = &RGB[0];
}
//YELLOW TO GREEN
for (int i = 256;i < 512; ++i){
_RED[i] = &RGB[511-i];
_GREEN[i] = &RGB[255];
_BLUE[i] = &RGB[0];
}
//GREEN TO TEAL
for (int i = 512; i <768;++i){
_RED[i] = &RGB[0];
_GREEN[i] = &RGB[255];
_BLUE[i] = &RGB[255+(i-767)];
}
//TEAL TO BLUE
for (int i = 768; i < 1024; ++i){
_RED[i] = &RGB[0];
_GREEN[i] = &RGB[1023-i];
_BLUE[i] = &RGB[255];
}
//BLUE TO MAGENTA
for (int i = 1024; i < 1280; ++i){
_RED[i] = &RGB[(255+(i-1279))];
_GREEN[i] = &RGB[0];
_BLUE[i] = &RGB[255];
}
//MAGENTA TO RED
for (int i = 1280; i < 1536; ++i){
_RED[i] = &RGB[255];
_GREEN[i] = &RGB[0];
_BLUE[i] = &RGB[1535-i];
}
//COMPLETES FULL SPECTRUM EXCLUDING WHITE AND BLACK
}

void loop(){
for(int i = 0; i <1536; ++i){
analogWrite(RED,*_RED[i]);
analogWrite(GREEN,*_GREEN[i]);
analogWrite(BLUE,*_BLUE[i]);
delay(100);
}

}
``````

or would it be better to it as follows:

``````int const Red = 3,  /* Pin 3 */ Blue = 5, /* Pin 5 */ Green = 6; /* Pin 6 */
void RGB(int R, int G, int B) {
analogWrite(Red,R); analogWrite(Green,G); analogWrite(Blue,B); delay(25);}
void setup() {}
void loop() {
int r=0,g=0,b=0;
r = 255;g = 0;b = 0; for (int i = 0; i < 256; ++i){RGB(r,i,b);}
r = 255; g = 255;b = 0; for (int i = 255; i >= 0; --i){RGB(i,g,b); }
r = 0;g = 255;b = 0; for (int i = 0; i < 256; ++i){RGB(r,g,i);}
r = 0;g = 255;b = 255; for (int i = 255; i >= 0; --i){RGB(r,i,b);}
r = 0;g = 0;b = 255; for (int i = 0; i < 256; ++i){RGB(i,g,b);}
r = 255;g = 0;b = 255; for (int i = 255; i >= 0; --i){RGB(r,g,i);}
delay(1000);
}
``````

Thanks,
Brett

Both of these programs will have roughly equal (and rather high) energy usage, as the Arduino `delay()` function does not put the processor into a sleep state. If power consumption is important, you will need to put the processor into sleep mode yourself. Unfortunately, the Arduino framework does not expose any way to do this; you will need to drop down to the appropriate AVR registers and intrinsics to do this.