# Why does this take more byte?

I am only replacing a portion of my code where I want to use the first method but as you can see it is making my sketch size little bigger

``````//if(iBPM >= 40 || iBPM <= 170 )
{
char iBPMwav[50];
String thisString = String(iBPM);
String stringThree =  "n" + thisString;
stringThree =  stringThree + ".wav";
stringThree.toCharArray(iBPMwav, 50);
tmrpcm.play(iBPMwav);
}
``````

`Sketch uses 29678 bytes (100%)`

This is simple but at the end, I want to go from the number 40 up to 170, and this is why I want to use the first method.

``````//if(iBPM >= 40 || iBPM <= 170 )
{
if(iBPM == 71)
tmrpcm.play("n71.wav");
else if(iBPM == 72)
tmrpcm.play("n72.wav");
else if(iBPM == 73)
tmrpcm.play("n73.wav");
else if(iBPM == 74)
tmrpcm.play("n74.wav");
else if(iBPM == 75)
tmrpcm.play("n75.wav");
else if(iBPM == 76)
tmrpcm.play("n76.wav");
else if(iBPM == 77)
tmrpcm.play("n77.wav");
else if(iBPM == 78)
tmrpcm.play("n78.wav");
else if(iBPM == 79)
tmrpcm.play("n79.wav");
else if(iBPM == 80)
tmrpcm.play("n80.wav");
else if(iBPM == 81)
tmrpcm.play("n81.wav");
else if(iBPM == 82)
tmrpcm.play("n82.wav");
else if(iBPM == 83)
tmrpcm.play("n83.wav");
else if(iBPM == 84)
tmrpcm.play("n84.wav");
else if(iBPM == 85)
tmrpcm.play("n85.wav");
else if(iBPM == 86)
tmrpcm.play("n86.wav");
else if(iBPM == 87)
tmrpcm.play("n87.wav");
else if(iBPM == 89)
tmrpcm.play("n89.wav");
else if(iBPM == 90)
tmrpcm.play("n90.wav");
else if(iBPM == 91)
tmrpcm.play("n91.wav");
else if(iBPM == 92)
tmrpcm.play("n92.wav");
else if(iBPM == 93)
tmrpcm.play("n93.wav");
else if(iBPM == 94)
tmrpcm.play("n94.wav");
else if(iBPM == 95)
tmrpcm.play("n95.wav");
else if(iBPM == 96)
tmrpcm.play("n96.wav");
else if(iBPM == 97)
tmrpcm.play("n97.wav");
else if(iBPM == 98)
tmrpcm.play("n98.wav");
else if(iBPM == 99)
tmrpcm.play("n99.wav");
else if(iBPM == 100)
tmrpcm.play("n100.wav");
else if(iBPM == 101)
tmrpcm.play("n101.wav");
else if(iBPM == 102)
tmrpcm.play("n102.wav");
else
tmrpcm.play("n170.wav");
}
``````

`Sketch uses 29024 bytes (101%)`

• You are using the String class which is pretty mighty and not light weight. It is always a good idea NOT to use the String class on MCUs. Jul 4, 2019 at 10:05
• Are you aware that `iBPM >= 40 || iBPM <= 170` is always true? Jul 4, 2019 at 11:02
• @EdgarBonet No, `I didn't know` Jul 5, 2019 at 6:08
• Then you should learn what the operators `>=`, `<=` and `||` mean. Jul 5, 2019 at 9:24
• @EdgarBonet Yes I did look that up that after you mentioned and learned from somewhere else. Tell me about it what a huge mistake boy ha ha Jul 6, 2019 at 21:09

## 2 Answers

I suggest having simpler file names (same size, always 3 digit numbers)

``````/// Return a text "n000.wav" ... "n255.wav"
/// depending on the parameter
char* makeFileName(byte i) {
static char name[9] = "nxxx.wav";
byte digit = i/100;
name[1] = '0' + digit;
i -= digit*100;
digit = i/10;
name[2] = '0' + digit;
i -= digit*10;
name[3] = '0' + i;

return name;
}
``````
• Looks smart to me. I will try it today. Jul 4, 2019 at 20:02
• Alright, I like your idea.. changing the name of the files will be like loosen flywheel for me. Instead, I made your code into two versions the first for `"nxx.wav"` and one for `"nxxx.wav"` Jul 5, 2019 at 6:37

While the Strings class makes a C/C++ program easy to understand and maintain, as stated in the comments, using the Strings class is a memory intensive option which should be should be avoided when memory is in short supply. Such as on the embedded processors used in most Arduino platforms.

Here are 2 examples where Stings is used and not used. Note the reduction from 2326 bytes to 734 bytes of program memory necessary to store the sketch in the embedded processor.

``````void setup()
{
}

void loop()
{
uint8_t iBPM;
uint8_t pointer;
for(iBPM = 40; iBPM <= 170; iBPM++)
{
char iBPMwav[50];
String thisString = String(iBPM);
String stringThree =  "n" + thisString;
stringThree =  stringThree + ".wav";
stringThree.toCharArray(iBPMwav, 50);
//  tmrpcm.play(iBPMwav);
}
}
``````

Sketch uses 2326 bytes (7%) of program storage space. Maximum is 32256 bytes. Global variables use 27 bytes (1%) of dynamic memory...

``````void setup()
{
}

void loop()
{
uint8_t iBPM;
uint8_t pointer;
for(iBPM = 40; iBPM <= 170; iBPM++)
{
char iBPMwav[50];
pointer = 0;
char temp_str[10];
//  String thisString = String(iBPM);
//  String stringThree =  "n" + thisString;
//  stringThree =  stringThree + ".wav";
//  stringThree.toCharArray(iBPMwav, 50);
itoa(iBPM, temp_str, 10);
if(iBPM < 100)
{
strncpy(&iBPMwav[pointer],"n0",2);
pointer += 2;
strncpy(&iBPMwav[pointer],temp_str,2);
pointer += 2;
}
else
{
strncpy(&iBPMwav[pointer],"n",1);
pointer += 1;
strncpy(&iBPMwav[pointer],temp_str,3);
pointer += 3;
}
strncpy(&iBPMwav[pointer],".wav",4);
//  tmrpcm.play(iBPMwav);
}
}
``````

Sketch uses 734 bytes (2%) of program storage space. Maximum is 32256 bytes. Global variables use 15 bytes (0%) of dynamic memory...

• BTW: String use even more RAM than compiler can see. And String+String is awfully complicated, creating a lot of garbage :) Jul 4, 2019 at 17:19