Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
12 votes

Optimizing code for ATtiny10

As I stated in a comment, this device would be too small for me to consider programming it using an Arduino core. I would rather stick with the avr-libc and direct port manipulation: #include <...
Edgar Bonet's user avatar
  • 43.2k
9 votes

Sizeof variables and Due's RAM

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 ...
Majenko's user avatar
  • 105k
7 votes

Optimizing code for ATtiny10

If the button is pressed by a human and your clock is high enough (MHz range), you can use a trick and get rid of the RNG. You can replace it with a free running counter like this: unsigned char count ...
Redy000's user avatar
  • 171
5 votes

What programming practices should be adopted while coding sketches for Arduino?

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 ...
Edgar Bonet's user avatar
  • 43.2k
5 votes
Accepted

Porting code from Arduino IDE to other IDEs - expected code size reduction

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 ...
Nick Gammon's user avatar
  • 38.3k
5 votes
Accepted

PROGMEM doesn't cause Sketch size to change

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.
Juraj's user avatar
  • 18k
5 votes
Accepted

Optimizing code to use less Flash Memory and SRAM

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 ...
Michel Keijzers's user avatar
4 votes

How to reduce sketch size?

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 ...
Michel Keijzers's user avatar
4 votes
Accepted

How can I reduce the size of my sketch?

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 ...
AMADANON Inc.'s user avatar
4 votes

Sizeof variables and Due's RAM

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 ...
James Waldby - jwpat7's user avatar
4 votes
Accepted

Large arrays crash the arduino

In the edited version of your question, you wrote: I am using memcpy_P() in order to copy the arrays from PROGMEM to RAM. The whole purpose of PROGMEM is to avoid having to store the data in RAM. If ...
Edgar Bonet's user avatar
  • 43.2k
3 votes

What programming practices should be adopted while coding sketches for Arduino?

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. ...
aaa's user avatar
  • 2,695
3 votes

How can I reduce the size of my sketch?

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 ...
Edgar Bonet's user avatar
  • 43.2k
3 votes

How can I reduce the size of my sketch?

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 ...
Majenko's user avatar
  • 105k
3 votes

Why does HC-SR04 return 0s in this sketch

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 ...
jfpoilpret's user avatar
  • 9,132
3 votes

Optimizing code for ATtiny10

Are you sure the pins are correct? Maybe use a smaller random number generator per https://arduino.stackexchange.com/a/18092/6628 ? static uint8_t lfsr = 0x01; const byte LFSR_MASK = 0x8e; void setup(...
Dave X's user avatar
  • 2,332
2 votes

Why does HC-SR04 return 0s in this sketch

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 ...
Mikael Patel's user avatar
  • 7,969
2 votes
Accepted

How can I tell how large my program is?

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. ...
JRobert's user avatar
  • 15.3k
2 votes

Porting code from Arduino IDE to other IDEs - expected code size reduction

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 ...
Michel Keijzers's user avatar
2 votes

How to reduce sketch size?

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 ...
dannyf's user avatar
  • 2,770
2 votes

How to reduce sketch size?

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, ...
dda's user avatar
  • 1,588
2 votes

How to reduce sketch size?

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,...
JRobert's user avatar
  • 15.3k
2 votes

HELP with making code size smaller for Attiny13a

The first thing I would try is to get rid of the Arduino core, and program instead at the avr-libc level: use direct port access instead of digitalRead()/digitalWrite() setup a timer and measure time ...
Edgar Bonet's user avatar
  • 43.2k
1 vote

Reduce Sketch Size Help!

Next time, select your code and press ctrl-K... Also align your code (indent it well). However, you don't need help actually as you have plenty of memory left. As as written: You only use 12% of ...
Michel Keijzers's user avatar
1 vote

Memory size issue with the struct

In your screen shots you are using string literals that are waaaay longer than in your sample code. I suspect using PROGMEM could be a way to work around. But why bothering with constant strings and ...
Kwasmich's user avatar
  • 1,513
1 vote

Is it necessary to reduce the size of libraries?

The Arduino IDE uses the gcc compiler. Gcc is an optimizing compiler, and is quite good at it. So basically it's going to do a good job of identifying and including only the functions you actually ...
Rob Sweet's user avatar
1 vote
Accepted

Is it necessary to reduce the size of libraries?

Normally a library supports various kinds of components; because mostly those components share a lot functionality, so it prevents to make very many different libraries. Note that all functions that ...
Michel Keijzers's user avatar
1 vote

What is the easiest way to count how many global variables are in your sketch code?

1 - The number of variables is not important, but the number of used memory (in bytes) is important. That is shown at the bottom of the Arduino IDE by the compiler when verifying or uploading a sketch....
Jot's user avatar
  • 3,246
1 vote

Porting code from Arduino IDE to other IDEs - expected code size reduction

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 ...
kellogs's user avatar
  • 127
1 vote

Sizeof variables and Due's RAM

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 + ...
Michel Keijzers's user avatar

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