28 votes
Accepted

How can I declare an array of variable size (Globally)

Your question has 2 parts actually. 1/ How can I declare the constant size of an array outside the array? You can either use a macro #define ARRAY_SIZE 10 ... int myArray[ARRAY_SIZE]; or use a ...
user avatar
  • 8,829
20 votes
Accepted

Does an Arduino get worn out by too much command execution?

No, the code doesn't "wear out" the MCU. In general no matter what you are doing roughly the same level of activity is being performed. Even delay() does lots of work while it's "idling". There are ...
user avatar
  • 103k
16 votes
Accepted

What are the traditional ways to optimize program memory usage?

What are the generally practiced methods for the optimization program memory usage? First, note you are searching for ways to lower SRAM memory. This contains global (variable) memory and heap space (...
user avatar
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 <...
user avatar
  • 36.8k
10 votes
Accepted

OOP vs procedural programming on arduino

Your assumption is quite wrong: The majority of libraries use OOP. The majority of in-built device drivers use OOP. The core is filled with many OOP helper objects and classes. 99.99% of all ...
user avatar
  • 103k
8 votes
Accepted

How to read code from Arduino Uno to Arduino IDE?

Can we read and get C codes from compiled codes from Arduino hardwares? While it is possible, even trivial, to disassemble machine code, it is very difficult to convert the assembly code into a ...
user avatar
7 votes

How to increase life of EEPROM?

The other answer mentioned some general ideas; here are a couple of more-specific notes. • You can direct your writes of single bytes through a routine that reads the EEPROM cell before writing to it,...
user avatar
7 votes

What are the traditional ways to optimize program memory usage?

I just want to add a single bullet to Michel Keijzers’ excellent answer: think about every single item you are storing in memory and ask yourself the question: do I really need to keep this in RAM? ...
user avatar
  • 36.8k
7 votes
Accepted

Does the compiled binary of a sketch include uncalled functions from a library?

In general only the functions actually used by your code are included in the final linkage of the binary. However it's not always possible for the compiler to know what is used and what isn't. In the ...
user avatar
  • 103k
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 ...
user avatar
  • 171
6 votes
Accepted

How to enable maximum Dead Code Removal?

Enable your IDE's verbose of the compile via the preferences. From there you can see exactly what and how it is being compiled. In short the IDE (and toolchain) will compile everything and anything ...
user avatar
  • 2,443
6 votes

Shortening large chunks of code

Yes you can, and the solution is very simple. It's in two parts, and they make up part of the absolute fundamental building blocks not just in C, but in almost any programming language. You can read ...
user avatar
6 votes

Overview of compiled code size

One approach is to look at the assembly listing of the program. After compiling the program (via the Verify or Download button) locate the directory where the .cpp, .hex, and .elf files for the ...
user avatar
6 votes
Accepted

How do I set a constant in setup() that I can use in loop()?

You can't, and there's other things wrong with your code. int sensorValue1 = analogRead(A0); int sensorValue2 = analogRead(A1); Calling functions from the global scope is an incredibly bad idea. ...
user avatar
  • 103k
6 votes
Accepted

Storing an array of function pointers

This all gets very much trickier than just an array of function pointers. Your functions are methods in unknown objects. As such you can't have one single type that stores pointers to the functions ...
user avatar
  • 103k
6 votes

Does an Arduino get worn out by too much command execution?

It's not. Well, it might slowly wear out if you run it like 20 years....(like most other physical products)? At least it does not rely on code complexity but how many writing operation done in the ...
user avatar
6 votes

Does an Arduino get worn out by too much command execution?

For a hobbyist: About the only life limiting software related issue might be writing to the FLASH memory as fast as you can from inside a program. But few programs make use of variables that need to ...
user avatar
  • 6,591
5 votes

Storing an array of function pointers

This is not a direct answer to your question, as I have no such answer, and I believe the answer you are seeking would be of no use to you. The problem with your question is that your method of ...
user avatar
  • 36.8k
5 votes
Accepted

How to increase life of EEPROM?

I can think of a few: don't power it on unless you have to; don't write to it unless you have to; write as little data to you as you can - compress the data; only write to it during brown out or ...
user avatar
  • 2,680
5 votes

Does an Arduino get worn out by too much command execution?

... (or generally any other micro-controller) ... The other answers are great, but there is one small exception. Ferroelectric RAM (FRAM) is a form of memory that combines the non-volatility of ...
user avatar
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 ...
user avatar
  • 35.3k
5 votes
Accepted

Storage usage of methods compared to copied code

It.... depends A call to function involves a few things Save the context Move the parameter(s) to the appropriate registers Enter the function Execute Eventually save the return value to a register ...
user avatar
  • 2,678
4 votes
Accepted

Simple question about && vs ||

Think about it "logically" (pun intended...): You read a character. Say it's "A". You then compare it to both '\r' and '\n'. char c = Serial.read(); if (c != '\r' || c != '\n') { // <- This ...
user avatar
  • 103k
4 votes

How to int variables with similar names without tons of code?

The word you are looking for is array. An array is like a list of values all of the same type. For instance, your LED pins could be specified as: int leds[14] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,...
user avatar
  • 103k
4 votes
Accepted

What is better: one sprintf() or multiple strcat() and itoa()?

Why do you feel you need to use either method? Neither are needed. Just print the individual parts: Serial.print('X'); Serial.print(first); Serial.print('Y'); Serial.print(second); Serial.println('T')...
user avatar
  • 103k
4 votes

Storing an array of function pointers

I'm not sure if it will not blow your memory limit as every pointer takes up 16 bits. But in C you usually do it this way: void func1() { puts("Hello"); } void func2() { puts("World!"); } ...
user avatar
  • 1,475
4 votes
Accepted

Set EEPROM from PC before program

The simplest way would be to have two sketches. One that will store all the strings into EEPROM. After that you can upload the second actual sketch that uses the values stored in EEPROM. You ...
user avatar
  • 11.1k
4 votes
Accepted

Is it possible to pass HIGH and LOW values as parameter in a function?

digitalWrite expects the pin and value to be uint8_t type. So you can certainly pass LOW or HIGH to functions. For example: void runMotor(int _dir, int _step, int wait, uint8_t value){ ...
user avatar
  • 35.3k
4 votes
Accepted

Const data optimisation for Arduino

This question is not really about the Arduino. It is about GNU C/C++ compiler optimizations. By adding the keyword const the compiler can do a lot of optimization. One is that the value does not ...
user avatar
  • 7,851

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