I wrote a long and inefficient code that takes up 97% of the storage stage on my Arduino nano and 34% of the dynamic memory. Now that my code works, I'm trying to make my code more efficient to save on memory space. There are a number of sections of my code that I could replace with functions, but after attempting to replace some of the code with functions, it seemed like I didn't save any space.
Are functions just to make programming easier so you don't have to retype the same part over and over, or will replacing sections of code will a function call actually save space? I'm just not sure from a compiler perspective, if replacing code with a function call save memory. Thanks in advance!
For example, I wrote 80 functions. One such function (below) blinks LED's and moves servo motors to music, but this function alone takes up ~6% of my memory space. I could write a function that just turns the lights on "DarkTurquoise" and call that function three times instead of writing three for loops.
void Scene49()///////////////////////////////WILL WILL ROCK YOU
{
int loop1 = 0;
INITIALIZE_VARIABLES();
startSoundInBackground(48);
delay(800);
while (loop1 < 4)
{
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::DarkTurquoise;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(150);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::DarkTurquoise;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(100);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::DarkTurquoise;
}
FastLED.show();
delay(400);
FastLED.clear (); //TURN LED's off /OFF
FastLED.show();
delay(510);
////////////////////////////////////
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Gold;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(150);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Gold;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(100);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Gold;
}
FastLED.show();
delay(400);
FastLED.clear (); //TURN LED's off /OFF
FastLED.show();
delay(510);
////////////////////////////////////
////////////////////////////////////
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Lime;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(150);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Lime;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(100);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Lime;
}
FastLED.show();
delay(400);
FastLED.clear (); //TURN LED's off /OFF
FastLED.show();
delay(510);
///////////////////////////////////
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Magenta;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(150);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Magenta;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(100);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Magenta;
}
FastLED.show();
delay(400);
FastLED.clear (); //TURN LED's off /OFF
FastLED.show();
delay(510);
///////////////////////////////////
loop1++;
}
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::DarkTurquoise;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(150);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::DarkTurquoise;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(100);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::DarkTurquoise;
}
FastLED.show();
delay(400);
FastLED.clear (); //TURN LED's off /OFF
FastLED.show();
delay(510);
////////////////////////////////////
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Gold;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(150);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Gold;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(100);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Gold;
}
FastLED.show();
delay(400);
FastLED.clear (); //TURN LED's off /OFF
FastLED.show();
delay(510);
////////////////////////////////////
////////////////////////////////////
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Lime;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(150);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Lime;
}
FastLED.show();
delay(150);
FastLED.clear (); //TURN LED's off //OFF
FastLED.show();
delay(100);
for (int i = 0; i < NUM_LEDS; ++i) //ON
{
leds[i] = CRGB::Lime;
}
FastLED.show();
delay(400);
FastLED.clear (); //TURN LED's off /OFF
FastLED.show();
SWITCH_FAST();
delay(5000);
FastLED.clear (); //TURN LED's off
FastLED.show();
}