As of May 31, 2023, we have updated our Code of Conduct.
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 ...
Majenko's user avatar
  • 104k
17 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 (...
Michel Keijzers's 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 <...
Edgar Bonet's user avatar
  • 40.2k
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 ...
Majenko's user avatar
  • 104k
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 ...
Ignacio Vazquez-Abrams's 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,...
James Waldby - jwpat7's 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? ...
Edgar Bonet's user avatar
  • 40.2k
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 ...
Majenko's user avatar
  • 104k
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
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. ...
Majenko's user avatar
  • 104k
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 ...
CharlieHanson's 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 ...
James Waldby - jwpat7's user avatar
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 ...
Majenko's user avatar
  • 104k
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 ...
Bumsik Kim's 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 ...
st2000's user avatar
  • 6,901
6 votes

How to move a servo quickly and without delay function

The quickest way possible from LightON to LightOFF is simply servo1.write(lightOFF); without loops or delays, in one go; no need to do it degree by degree. For example: int lightON = 180; int lightOFF ...
ocrdu's user avatar
  • 1,672
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 ...
Edgar Bonet's user avatar
  • 40.2k
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 ...
dannyf's user avatar
  • 2,750
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 ...
Ignacio Vazquez-Abrams's 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 ...
Nick Gammon's user avatar
  • 36.4k
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 ...
frarugi87's user avatar
  • 2,711
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,...
Majenko's user avatar
  • 104k
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 ...
Majenko's user avatar
  • 104k
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')...
Majenko's user avatar
  • 104k
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!"); } ...
Kwasmich's user avatar
  • 1,513
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 ...
Gerben's user avatar
  • 11.2k
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){ ...
Nick Gammon's user avatar
  • 36.4k
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 ...
Mikael Patel's user avatar
  • 7,929
4 votes

Is there a more elegant way to check highest / lowest sample data?

This is a variation on the struct idea with an object-oriented flavor. If you put the current, low and high values of any sensor inside a struct, you could also morph this struct into a class and ...
Edgar Bonet's user avatar
  • 40.2k

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