Optimizations (And The Project Now Has a Name…)

So a few problems with the code in the last post.
  1. I copied stuff from an older debug version.  Clearly the "state" variables made no sense.  Sorry about that.
  2. The code was suboptimal.  Literally…

The original version of the code used a lot of int types.  But since the highest value I was ever using was 255, I was easily able to convert all my ints to bytes.  I also was able to greatly optimize my throb() method.  Instead of doing two for() loops, I was able to replace it with a single do/while and just call into solid() with the correct parameters.  Here’s the end result:

   1: void throb(byte redVal, byte greenVal, byte blueVal)
   2: {
   3:   float maxVal = 255;
   4:   byte i = 0;
   5:   bool countDown = false;
   7:   do
   8:   {
   9:     solid((byte)(i*(redVal/maxVal)), (byte)(i*(greenVal/maxVal)), (byte)(i*(blueVal/maxVal)));
  11:     if (true == countDown)
  12:     {
  13:       delay(5);
  14:       i--;
  15:     }
  16:     else
  17:     {
  18:       delay(10);
  19:       i++;
  21:       if (255 == i)
  22:       {
  23:         countDown = true;
  24:       }
  25:     }
  26:   } while (i>0);
  28:   solid(0,0,0);
  29:   delay(5);
  30: }

With all of these changes, I was able to reduce the compiled sketch size from 4024 bytes down to 3548 bytes.  A savings of 476 bytes (12%)!  Is there anything else that can be shaved off?

I finally created a new project on CodePlex for this and called it Yaam Yet Another Ambient Munny.  The code will be up there once I get SubVersion installed. munny

[Update 2009-04-11 11:10PM: Yaam is now live on CodePlex!  Go to http://yaam.codeplex.com to view the Arduino source.]

This entry was posted in Arduino. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s