How long does the arduino nano board last?

So you have 459 bytes available.

When setup() is executed, it's called from a function called main() (hidden for you). To know where to continue after setup is finished, the return address is placed on the so-called stack; on a 328P that will cost 2 bytes so you now have 457 bytes left; they will become available again once setup() is finished.

The same happens when loop() is executed, so you keep having either 459 bytes available or 457 bytes available.

So now in loop() you have e.g. a local counter; when loop() is executed, that local counter 'lives' on the stack and assuming it's an int that costs 2 bytes (so down to 455 bytes). If you have a local array of e.g. 10 floats declared in loop(), that will cost 40 bytes.

Next in loop() you call Serial.println() to print that counter; the return address where to continue after the call needs to be remembered so after your print statement the next statement can be executed. Another 2 bytes down the drain so you now have 453 bytes left. Once the Serial.println() returns, you have 455 bytes left.

Now lets assume that an interrupt occurs while the Serial.println() is being executed. For the program to know where to continue after the interrupt handler is finished the return address is placed on the stack when the interrupt happens; again 2 bytes down. So 453 - 2 equals 451 bytes left. Further the interrupt handler will save a number of registers on the stack (not sure how many, let's say 10), so now you only have 441 bytes left.

Once the interrupt is handled the saved bytes are restored, the interrupt handler returns to where it interrupted the normal program flow and the 2 plus 10 bytes are available again. So back to 453 bytes, when Serial.println() ends back to 455 and when loop() ends the two bytes allocated to the counter are available again (so 457 bytes) and once returned to main() the return address is available again and you have the original 459 bytes.

So now let's assume that you wrote you own function that is called from loop() and calls digitalWrite(). The same story, you will loose 2 extra bytes in this case.

So that is one part.

The next 'problem' can be dynamic memory allocation. The IDE does not report on that. If you use e.g. the Adafruit Neopixel library with 100 NeoPixels, that will cost 300 bytes (which are allocated on the heap) so you are down to 159 bytes before even getting to loop(); those 300 bytes will permanently live.

Note that heap and stack grow towards each other; your problems come when they collide and placing something on the stack overwrites a variable on the heap or you modify a variable that is on the heap but because the e.g. the return address is now there you corrupt the return address and once the functionality that placed that return address on the stack ends (function cal, interrupt) it does not continue where it is supposed to continue.

And lastly it depends of optimisations that the compiler applies to your code. You might not find a call to setup() and/or loop() in the executable file that you upload but it can be a jump so the 2 bytes that were lost with a call are not lost as there is no need to return to a specific address.

Life is complicated.

Something to study:

I have tried the following method of string wrapping as advised by the Arduino documentations , which to use the following function around all the strings like this

Serial.println(F(distance_cm));

but it gave me the following error











In file included from C:\Program Files\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:28:0,
                 from sketch\Maestronics_Final_N_printer_height.ino.cpp:1:
C:\Users\DELL\Desktop\Maestronics_Final_N_printer_height\Maestronics_Final_N_printer_height.ino: In function 'void HeightX()':
C:\Program Files\Arduino\hardware\arduino\avr\cores\arduino/WString.h:38:74: error: initializer fails to determine size of '__c'
 #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
                                                                          ^
C:\Users\DELL\Desktop\Maestronics_Final_N_printer_height\Maestronics_Final_N_printer_height.ino:497:18: note: in expansion of macro 'F'
   Serial.println(F(distance_cm));
                  ^
C:\Program Files\Arduino\hardware\arduino\avr\cores\arduino/WString.h:38:74: error: array must be initialized with a brace-enclosed initializer
 #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
                                                                          ^
C:\Users\DELL\Desktop\Maestronics_Final_N_printer_height\Maestronics_Final_N_printer_height.ino:497:18: note: in expansion of macro 'F'
   Serial.println(F(distance_cm));
                  ^
exit status 1
Error compiling for board Arduino Nano.

So what is the problem , as I'm using Arduino IDE 1.8.19 on windows 7 machine

The F macro is used for fixed text (string literals), not for variables.

@sterretje
is there any function to perform the same thing for variables ?

Maybe it's time to show your sketch (just in case, don't forget to use code tags when posting code); somebody might have thoughts how it can be optimised. I have a bit of difficulty to believe that a keypad, a LCD and a HX711 (?) takes up that much RAM.

Not really. Variables live in RAM; since they're variable (what's in a word!), they need to be modified, on the fly, and fast. This precludes them from being stashed away into a remote and slow place where the running code can't really change their values.

Letters? :rofl: :rofl: :rofl:

Sorry, could not resist.

3 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.