user40449: How to reduce sketch size?
I got an error that “Sketch uses 30,044 bytes (104%) of program storage space. Maximum is 28,672 bytes.”
I can't reduce function in sketch.I can't change ATmega32u4,now.
The sketch has function of measuring sensor,logging on SD,connection with BLE module.
So please tell me hack.
Or,how to increase flash memory size?
I tried to change type of String to Char.

user4628: There's no magic bullet. You have to work through your code and see what you can remove safely. If you have duplicate code, put it in a function and call this. If you Serial.print a lot of text, considering saving up on that. etc etc etc

user30005: 
How to reduce sketch size?

without your code, tough to be specific. but generally:
1) try not to use floating pointmath;
2) try not to use printf() or its derivative;
3) try to minimize large arrays;
4) use functions instead of macros;
5) use modular programming;
...

Or,how to increase flash memory size?

1) use a bigger chip;
2) use onboard eeprom;
3) use outboard eeprom / flash / fram / ....
4) divide up the tasks among multiple chips;
...

user313: If you're using donated libraries to run your hardware (such as the Dallas Temperature library) or provide software features (like SimpleTimer), the authors often provide a full complement of features, some of which you may not need. I often make my own versions of these libraries that provide the functionalities I must have, and cut out the others. I can always add one or two of them back into whichever sketch needs them, without burdening other sketches with their overhead.
In SimpleTimer, for instance, I seldom have need for enable/disable timer or for a timer that runs N-times and then stops, so I have a version of the library with those functions removed.
If you've declared some text strings or other data to live in Flash, and you have more spare RAM than spare Flash, let some of those objects live in RAM instead. Or reduce the length of output strings that are stored in Flash by writing more tersely, even at the cost of clarity in the output, or eliminating that outpu altogether (which eliminates not only the string data but the code to print it, as well).
Can you reduce any code segments or algorithms with more space-efficient code? Can you eliminate some functionality to save code space?
Note that trimming buffers and arrays won't help because those are in RAM, and data packing, such as storing a number of booleans as distinct bits within a word, will actually increase your code size (this is a common way to reduce RAM requirements when you can afford the code to do the packing/unpacking, but you have the opposite problem!).

user31105: Some generic techniques you can try:

Remove duplicate code and put it in a function.
If you have duplicate code that is ALMOST duplicate, parameterize it (i.e. add a function, make the difference a parameter(s) of the function and call from various locations with different argument(s).
Use a micro controller with more Flash memory.
Minimize the data storage, limiting array sizes for example (if these are in flash RAM).
If you use precalculated arrays, remove them and create a function calculating them real time (this will be a performance cost of course).
Check for dead code inside functions. A smart compiler will already remove uncalled functions, but do not check inside functions.
Remove or minimize debug/print statements. Each character cost a byte, which adds up quickly with many strings.
Try to remove complete libraries, like removing the Serial for print statements used only for debugging.


40449: <p>How to reduce sketch size?</p>

<p>I got an error that “Sketch uses 30,044 bytes (104%) of program storage space. Maximum is 28,672 bytes.”</p>

<p>I can't reduce function in sketch.I can't change ATmega32u4,now.
The sketch has function of measuring sensor,logging on SD,connection with BLE module.</p>

<p>So please tell me hack.</p>

<p>Or,how to increase flash memory size?</p>

<p>I tried to change type of String to Char.</p>
 4628: <p>There's no magic bullet. You have to work through your code and see what you can remove safely. If you have duplicate code, put it in a function and call this. If you <code>Serial.print</code> a lot of text, considering saving up on that. etc etc etc</p>
 30005: <blockquote>
  <p>How to reduce sketch size?</p>
</blockquote>

<p>without your code, tough to be specific. but generally:</p>

<p>1) try not to use floating pointmath;</p>

<p>2) try not to use printf() or its derivative;</p>

<p>3) try to minimize large arrays;</p>

<p>4) use functions instead of macros;</p>

<p>5) use modular programming;</p>

<p>...</p>

<blockquote>
  <p>Or,how to increase flash memory size?</p>
</blockquote>

<p>1) use a bigger chip;</p>

<p>2) use onboard eeprom;</p>

<p>3) use outboard eeprom / flash / fram / ....</p>

<p>4) divide up the tasks among multiple chips;</p>

<p>...</p>
 313: <p>If you're using donated libraries to run your hardware (such as the Dallas Temperature library) or provide software features (like SimpleTimer), the authors often provide a full complement of features, some of which you may not need. I often make my own versions of these libraries that provide the functionalities I must have, and cut out the others. I can always add one or two of them back into whichever sketch needs them, without burdening other sketches with their overhead.</p>

<p>In SimpleTimer, for instance, I seldom have need for enable/disable timer or for a timer that runs N-times and then stops, so I have a version of the library with those functions removed.</p>

<p>If you've declared some text strings or other data to live in Flash, and you have more spare RAM than spare Flash, let some of those objects live in RAM instead. Or reduce the length of output strings that are stored in Flash by writing more tersely, even at the cost of clarity in the output, or eliminating that outpu altogether (which eliminates not only the string data but the code to print it, as well).</p>

<p>Can you reduce any code segments or algorithms with more space-efficient code? Can you eliminate some functionality to save code space?</p>

<p>Note that trimming buffers and arrays won't help because those are in RAM, and data packing, such as storing a number of booleans as distinct bits within a word, will actually increase your code size (this is a common way to reduce RAM requirements when you can afford the code to do the packing/unpacking, but you have the opposite problem!).</p>
 31105: <p>Some generic techniques you can try:</p>

<ul>
<li>Remove duplicate code and put it in a function.</li>
<li>If you have duplicate code that is ALMOST duplicate, parameterize it (i.e. add a function, make the difference a parameter(s) of the function and call from various locations with different argument(s).</li>
<li>Use a micro controller with more Flash memory.</li>
<li>Minimize the data storage, limiting array sizes for example (if these are in flash RAM).</li>
<li>If you use precalculated arrays, remove them and create a function calculating them real time (this will be a performance cost of course).</li>
<li>Check for dead code inside functions. A smart compiler will already remove uncalled functions, but do not check inside functions.</li>
<li>Remove or minimize debug/print statements. Each character cost a byte, which adds up quickly with many strings.</li>
<li>Try to remove complete libraries, like removing the Serial for print statements used only for debugging.</li>
</ul>
