Tricks to reduce compiled size?

MMh, i am runnig out of Memory.

Project is almost complete, but there are some things i want to do now, but got only some Bytes.

Are there any tricks to reduce some bytes?

Switch off functionality, ripp things off in Librarys or on hidden places?

ArduinoStandard functionality...

mmh, i not using the Bootloader...
i don't need ADC, Serial and some other things...
I don't know if this is compiled every time, or only if i use this features...

I will now optimize code itself, but there are not very much things to optimize.

Simple: Please give Hints to reduce compiled size.

Thank you!

Greetings
ChrisS

i am runnig out of Memory

RAM or flash?

Flash.

mmh, i saw that gcc has an -0s parameter... does Arduino use it?

Simple: Please give Hints to reduce compiled size.

Some simple ways to reduce the code size (the amount of Flash used)...

  • Shorten or eliminate string constants
  • Shorten or eliminate initialized arrays
  • Reduce variables to the smallest datatype possible. For example: Manipulating an int (short, unsigned) requires two machine instructions (one for each byte). Manipulating a byte (char, uint8_t) uses a single machine instruction. If an int is manipulated 100 times approximately 200 machine instructions will be generated. Change it to a byte and the amount of code generated should be roughly halved to 100 machine instructions.

For more specific advice, you really should post your Sketch.

Thank you!

Today i got less time for this...
I think 1st Step is messy code itself.

  1. Because of using LCD i thought about to store every static message in EEPROM... burn EEPROM with another sketch, so i could get rid of insketch mesages.

  2. LCD.Print LCD.setCursor...
    mmh, i think i should create a new function like lcd.write(String,0,1);
    -this should do?!?!? reducing sketchsize, but also binarysize?

  3. Some if else to case... this should reduce a little bit... mmh....

Then i will try your attempt...

Thank you...

Greetings
ChrisS

ok....

possible a attempt to reduce size:

void l(int p){
int ez;
int x;
int y;

x=EEPROM.read(p);
y=EEPROM.read(p+1);

for (ez=p+2;  ez<=p+18;  ez++) {
    lcd.setCursor((ez-(p+2))+x,y);
    lcd.print(EEPROM.read(ez));
    }
}

Ok, Stringmessages are burned in EEPROM.
Fixed size 18 Bytes.
First and Second Byte are x and y Position on LCD followed by 16 Bytes Message.

Possible to optimize this code?

p=position of 18 Byte Message in EEPROM (Adress)
ez=counter to read Byte by Byte
x=x position on lcd
y=y position on lcd

So:

lcd.setCursor(0,0);
lcd.print("hello world!");
lcd.setCursor(0,1);
lcd.print("mmh, where am I?");

becomes:

l(0);
l(18);

Should be smaller....

some more improvements avaliable?

Thanks!
ChrisS

l(0);

Eww. The Arduino uses a compiler, so the code that is actually stored on the hardware does not contain variable names, and you do NOT save any space by using shorter, harder-to-understand names for your functions or variables. You don't save any space by omitting comments, either. (in some systems, notably those based on a BASIC interpreter , the actual code you typed WAS stored on the micro, and you could speed things up by making it as short as possible...)

Other than that, the idea of storing strings in EEPROM is fine, but you're not going to save any more than the 1K size of the EEPROM, which isn't very much.

It would probably be useful to look at the code you have, so we can see what sorts of things are taking up space, and what sort of space-saving techniques you're already using...

"l" like lcd... understandable... :wink:

ok... tanks... i misunderstood ....

I will post tomorrow the complete code....

I want to integrate this function first....

Yes, i save less space with that EEPROM function, but its a ATmega8 thingy... i got 7,8 KB Flash used... and have to implemet 3 new functions... so every Byte saved is a good thing...

See you tomorow... thank you!

Greetings
ChrisS

actually, piling code into functions and therefore calling a larger number of functions can increase code size and memory utilization.

so, besides being horribly cryptic, your l() function can result in increased FLASH and RAM use.

-j

Some more things to bring memory consumption down:

  1. Do not use digitalRead/Write. Use direct port manipulation instead
  2. Strip down the Serial library if you need only parts of it

And best approach: drop relative memory consumption by using a bigger processor. Unless you intend to create thousands of devices it may be cheaper to go for more memory instead of pushing everything to the limits.

Udo

One more thing that might or might not work. When I started to decrease ram consumption by combining flags into bit fields this also decreased flash consumption. This may not always work but it might be worth a try.

Udo

The function was buggy.... i have correct it... it works now for me...

@Udo
Danke Udo für Deine Tipps!!!!
Ich setzte momentan ganz bewußt auf einen ATmega8... hier ist zum testen auch noch eine modified-pico 328p.... ich will möglichst effizient an Dinge heran gehen, herumschlampen kommt später... :wink: und da es ein Hobby ist, habe ich viel Zeit... man lernt viel wenn man sich mit Dingen auseinandersetzt bzw. auseinandersetzen muß... in diesem Falle Speicher...

I have built in my function.... saved 162 Bytes...
function size is 40 Bytes, saved "Text to EEPROM" 112 Bytes...

Code looks better now....
l(100); instead of lcd.setCursor(0,0);lcd.print("kjhkjhkj"); and so on.

I've lernd some using of eeprom... nice Sideeffekt...

Tommorow as promissed i will cleanup the code, and post it here....

I am sure there are many Mistakes... and many Improvements possible... i am a Arduino Starter... so i can learn much more in future... possibly with your help an explanation....

Good night!!! Have nice dreams out there, in Arduino Community!!!!

ChrisS

but its a ATmega8 thingy

[use] a bigger processor.

Really! Trying to squeeze an application into 8k of memory when you can get 14k or 30k for pretty close to the same price is probably NOT a good use of your time.

piling code into functions and therefore calling a larger number of functions can increase code size

Theoretically. I suspect that if you call a function more than once, and it calls more than one other function, it would be awfully rare for the function-based code to increase code size. gcc does have automatic inlinining of functions as part of its optimization process; I'm not sure exactly when it is used.

I don't think so.

You are right, it is not >neccessary< to have much work of reducing size... while i can get a MC with much more memory....

BUT, there is a reason for me, to do it...
(I got already a modified-pico with 328p. (and much more memory)....)
I am a Arduino starter. I have to lern, to do things. I have to lern to code on an effizient way.

I don't have to, but i want to.

I don't think, that it is a good way to have 40 times written "lcd.setCursor and lcd.print" in my code only in fact that it is the easiest way. I think reducing binarycode and sketchcode by a function which make it better readable, smaller and reducing size... is a slightly better way...

You are right... much work for less savings, but into that work i've lerned how: -to store Strings in EEPROM, in what size relations and savings are posiible, - i now got a function which let me do some better and smaller stuff on 328p with more memory...

With this small and old mc i have to do better coding, because of limited recources. Learning to code efficient at first, i think that is the right way. Then, after lerned howto i easy could do better an much bigger things... because ive lernd how to improve.

Some years ago, i've done the same with applicationcoding on PC.
Had already an 486 66Mhz but for coding i used a 8086 4Mhz.
So Applications become very fast and efficient on 486, because the Base of coding mashine was much older and smaller.

Sure, too much work for too less savings.
But if I use this function on an 328p the relation of savings are much higher because of memory size and possiblilities.

if i code the easiest way writing 120x lcd.print on an 328p i would
never lern what to do, if some "good" coding is needed.

Hope, you understand why i do such hard and unneccesary stuff with atmega8. I want to lern efficient coding for using it on better MCs to get better coding and so claimed expanded usage of features.


Size reduced by this function.
That is a proof of itself.

While moves 162 Bytes to EEPROM, i saved 112 Bytes in Flash.

Imagine i got 1K Text on 328p.
Then Flashsaving is 960Bytes... nearly 1K.

I think as result of one implemented very simple function that is efficient saving. ("poor" on atmega8, but usefull for future projects with beter MCs)

For usage in lcd.print and lcd.setcursor things i will use it in future,
because in my opinion that is a better way to do lcd.prints, lcd.setCusror whith half of the Sketchcode and saving nearly the size of eeprom in flash.

So, i will do some code cleaning and think about it now.. so i can post it here today... hope time doesn't run out today... maybe till the next days, you could improve my "Firststeps Noobcode" if you want to :slight_smile:

Greetings
ChrisS

Hi ChrisS,

If you have statements like ".print(value, BYTE)" in your code i found that using ".write(value)" uses a lot less sketch size. (as in compiled size) Not sure about ram usage.

My sketch with a lot of ".print(value, BYTE)" statements shrunk from 10000 bytes to something like 7000 bytes using .write(value).

Don't know if it applies to you,

Jeroen

discussion serial.print vs. serial.write http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1257276571

Thank you.

I actually use only lcd.print one time in the new function...
i will test it out... possibly I got a Byte more out of it...

Greetings
ChrisS

Ok,

first question: How to stay in a loop till button is pressed?

My attempt to do this is a "do while" with alltime true condition and break it by button press. Looks crazy...

void stayinlooptillpress(){

     do
           {  
      
            Serial.println("I swear, i will stay here!");
 

                           if ( button.isPressed() ) {
                                                   break;
                         }


            }  while (0==0);

}

While condition looks a little bit crazy... :slight_smile:
Whats the right way to do this... ?
-Stay in place (loop),
-exit loop by Buttonpress....

Greetings
ChrisS

You would need something in the inner block to break out of the do/while loop.

Easier, though, is something like this:

void stayinlooptillpress()
{
   while(!button.isPressed())
  {
     // Do nothing
  }
}

You would need something in the inner block to break out of the do/while loop.

You mean, like the "break" statement that's there? :wink:

Yiiihaaa!!!, that looks good...

thankyou!!!!

Greetings ChrisS