Go Down

Topic: Can I make this fading code more efficient? (Read 415 times) previous topic - next topic

swifty

Jan 27, 2015, 10:17 am Last Edit: Jan 27, 2015, 10:19 am by swifty
Hi all,

I'm fairly new to Arduino programming and am currently working on my first real project with it :)

I'm making a lamp for my son's bedroom and have incorporated a 'star projector' into the lamp - this runs several LEDs (ws2801) which fade between green and blue in a loop.
Whilst the LEDs are running I also play a musicbox melody on loop to help put him to sleep - this is using the tmrpcm library to play a wav. file from an SD card.

The problem I'm facing is when the LEDs fade the music is stuttering - I guess the fade code is taking too long to execute so is blocking the tmrpcm library from playing the audio.

Here is the code I'm using for the fade - can anyone suggest how I can make this more efficient?
Essentially - I would like to turn 2x LEDs Green for say a second, then fade another 2x LEDs on to Blue, once they are on fade the 2x Green LEDs off and then reverse the loop..

Currently blinkStars gets fired every 250ms via this timer library.

Code: [Select]
void blinkStars() {
int Rnew;
int Gnew;
int Bnew;
if (brightness == 100) {
  brightness = 0;
  brightness2 = 100;
  if (blinkstate == 4) {
  blinkstate = 1;
  }
  else
  {
  blinkstate = blinkstate + 1;
  }
}
else
{
  brightness = brightness + 1;
  brightness2 = brightness2 - 1;
}
 switch(blinkstate){
    case 1:    Rnew = 0 * brightness / 100;
               Gnew = 0 * brightness / 100;
               Bnew = 255 * brightness / 100;
               strip.setPixelColor(16, Rnew, Gnew, Bnew);
               strip.setPixelColor(18, Rnew, Gnew, Bnew);
               strip.show();
               Rnew = 0 * brightness2 / 100;
               Gnew = 255 * brightness2 / 100;
               Bnew = 0 * brightness2 / 100;
               strip.setPixelColor(17, Rnew, Gnew, Bnew);
               strip.setPixelColor(19, Rnew, Gnew, Bnew);
               strip.show();
            break;
    case 2:    Rnew = 0 * brightness / 100;
               Gnew = 255 * brightness / 100;
               Bnew = 0 * brightness / 100;
               strip.setPixelColor(17, Rnew, Gnew, Bnew);
               strip.setPixelColor(19, Rnew, Gnew, Bnew);               
               strip.show();
               Rnew = 0 * brightness2 / 100;
               Gnew = 0 * brightness2 / 100;
               Bnew = 255 * brightness2 / 100;
               strip.setPixelColor(16, Rnew, Gnew, Bnew);
               strip.setPixelColor(18, Rnew, Gnew, Bnew);               
               strip.show();
            break;
 }
 elapsed = elapsed + 250;
}


Any suggestions would be appreciated :)

- Edit - Just realised I can ditch all of the '0 * brightness2 / 100;' calculations, and just use a static value of 0 - since they will never change..

PaulS

Quote
Just realised I can ditch all of the '0 * brightness2 / 100;' calculations, and just use a static value of 0 - since they will never change..
Instead of diddling with brightness and brightness2 (dumb name - if one needs a number, BOTH need numbers), update Rnew, Gnew, and Bnew directly.

Some explanation of blinkstate would be in order.

Mostly, though, it isn't the calculations that are causing the issues. It is the setPixelColor() function that takes time, and causes the delays that cause the music player problems.

You need a music shield that handles ALL the timing and music playing functionality.

Go Up