32

Optimisation Low-level programming for embedded systems is quite different from programming for general purpose devices, such as computers and cell phones. Efficiency (in terms of speed and space) is far more important because resources are at a premium. That means the very first thing to do if you run out of space is to look at what parts of your code you ...


25

When you upload your code to your Arduino, say an Uno for example, it'll tell you how many bytes it uses up out of the 32K available. That's how much flash memory you have (think computer hard disk). While your program is running, it's using whats called SRAM, and there is much less of that available. Sometimes you'll notice your program behaving oddly at a ...


17

In addition to what others have said (on which I fully agree), I would advise to read this adafruit article about memory; it's well written, explains a lot of things about memory and provide hints on how to optimize it. At the end of the read, I think you would get a quite complete answer to your question. To sum it up, you have 2 possible optimization ...


12

You second code may be smaller in size but due to the function call overhead the max execution speed is reduced. Does this matter in your case? No, because you have huge delays anyway, but if the code was an actual series of repeated calculations that should be executed as fast as possible then it would make a difference. As a general rule, smaller code is ...


10

Find the io__.h file for your microcontroller, on Linux it is located in /usr/lib/avr/include/avr, on Windows it will be in a somewhat similar location. Scroll down to the part that says /* Constants */. There are couple interesting macros defined there, FLASHEND being the one you should be interested in. You can use it for example as follows: #if FLASHEND ...


10

There are two things to do if you run out of storage: Somehow "optimize" your code so it needs less storage; or at least uses less of the particular kind of storage that you ran out of (and uses more of the kind of storage that you still have plenty of). Or, Add more storage. There are lots of tips online on how to do the first (and for the vast majority ...


9

sizeof doesn't return the number of elements. It returns the number of bytes. Since they are uint16_t arrays each element is 2 bytes - hence twice the size. The reason your last number is only 40928 is because of integer wraparound. You only provide a 16-bit unsigned variable to store it in, so all you get is the lower 16 bits of the answer. 92000 + 40000 +...


8

Generally speaking, smaller is better. However, there is a point where too small actually makes the program run slower. My suggestion is if you are working on a sketch and it is blatantly obvious that you are repeating code over and over again, I would rip that out and put it into a function, not only does it make the program smaller, it makes it easier to ...


5

You don't need to abandon the Arduino IDE. Let me quote from my forum post about sketch sizes. Introduction Every now and again this subject pops up on the Arduino forum. Why does it take 1000 bytes to blink an LED? Why, oh why? It is obviously very bloated, eh? Example: blink Let's check that claim first. The "blink" sketch, as shipped with the IDE (...


4

The sizeof operator's result is number of bytes, not number of array elements. Your arrays in the program above have two bytes per element, hence twice as many bytes as elements. If you want to report the number of elements, divide the total size by the number of bytes per element. For example: nElements = sizeof BuffA / sizeof BuffA[0]; As uint16_t ...


4

Paul covered the most important points. This answer is intended to be a complement to his. First, you should keep in mind the general rules, which are more important than the more specific ones. The general rule for saving RAM is: be aware of what you are storing, and do not store anything you do not really need. The general rule for saving flash is: be ...


4

The AdaFruit Trinket just doesn't have a lot of memory - 8kb, of which 3kb is used by the bootloader. The order I look for things to reduce the footprint of an Arduino program are: data (e.g. large strings) libraries your code (especially doing the same things several times, which might be combined into loops or functions). In this case, you don't have ...


4

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,...


4

all constant data are part of the sketch. where else should they be? in runtime they are loaded into RAM. PROGMEM directive prevents the loading into RAM.


4

What you can do in addition to the items you mentioned: SRAM reduction Use the smallest amount of data type for integers, e.g. use an 8 bit data type instead of the default integer, this saves you one byte for every variable. See remark below about types. I made my own types (if not existing) for int32_t, uint32_t, int16_t, uint16_t, int8_t and uint8_t and ...


3

As already mentioned by AMADANON Inc. in his answer, you should qualify as const all the constant variables at the beginning of the program. This is probably the biggest space saver. You can gain something like 100 bytes by using direct port access instead of pinMode/digitalRead/digitalWrite. In the case of the ATtiny85, you only have port B, and the ...


3

An option not already mentioned, but may be worth mentioning, would be to remove the bootloader and program the chip directly with a hardware programmer (or another Arduino). That will increase the available sketch memory from 5kiB to the full 8kiB on the chip.


3

pulseIn() may block up to 1 second (by default), which would happen everytime the HC-SR04 receives no echo, which might be the case if ultrasounds do not get properly reflected on a flat surface or if the nearest echoing surface is over ~4 meters away from it. In this situation, not only would pulseIn block for one second, but it would also return 0 to ...


2

When considering optimization, think carefully about what resource is most valuable. You can optimize for code size; this will let you put more code onto your processor. What will the rest of the space be used for? If your code doesn't fit onto your arduino, and your alternative is buying a larger chip, then this is worth the effort. You can optimize for ...


2

Your refactor should be done simply because it's better code, not because it's smaller. The advantage is that it's easier to modify. If making the code size smaller results in a loss of clarity (not so in your case), then you should probably view it as a premature optimization.


2

Why does the same block of code act differently in different sketches? There are different interrupt sources in the sketches that may affect pulseIn(). Try using a more advanced method to measure the pulse width such as Timer Capture. Cheers!


2

Keep in mind that RAM is even less. 32KB is a fair lot of code, since C/C++ is highly efficient. "Don't save **** in RAM." If you need to load images into a display, save the images on an SD card. And "stream" it to the display. You can read and transmit the bytes one by one, or 10 by 10 or something. Also, if you need long strings, you can use the "...


2

Short answer: Yes. At the end of a successful compilation (not upload), at least in ArduinoIDE 1.6.9, the final 2 messages look like: Sketch uses 23,442 bytes (72%) of program storage space. Maximum is 32,256 bytes. Global variables use 1,093 bytes (53%) of dynamic memory, leaving 955 bytes for local variables. Maximum is 2,048 bytes. In this case, ...


2

If you like the Arduino IDE, you also can do the following: Do not use the library functions, but use registers directly. The compiler is smart enough that it does not link functions you do not use with your executable/binary. Optimize your code by Checking if you can simplify algorithms Remove unnecessary statements Remove global variables (maybe move ...


2

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 ...


2

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


2

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 ...


1

I have managed some 33% downsizing, hurrah! Adding to the current answers: the biggest reduction came by ridding the floating points. I have come across the answer here and put avr-objdump to good use. That's how I have had the shock of my life (no hardware FPU built into these $1 MCUs :-o) and also that is how you can see other offenders sneaking into your ...


1

A variable of uint16_t has 16 bits, thus two bytes, so for e.g. BuffA[46000] this means 46000 * 2 = 92000 bytes. The variable all is also 16 bits and can contain only as max value 65,535. 92000 + 40000 + 40000 = 172000 bytes but that does not fit in 16 bits. Therefore the value is clipped ... make it a uint32_t variable. The total is 172.000 bytes, this ...


1

A compiler translates c/cpp to target assembly language. To keep it short, some c/cpp language statements or keywords can be used to shorten the code (in c/cpp)... but the mechanics behind all that to translate to assembly can generate more instructions than you thought. It would maybe be easier to explain if you put some example...


Only top voted, non community-wiki answers of a minimum length are eligible