So I'm new in the arduino world but I have a grasp of object oriented programming, and decided to write a little library for running games on an arduino mega using a display. My question is, I'm creating an "Entity" class (to spawn multiple enemies, players, etc) outside the setup() and the loop() functions, so they dont overwrite themselfs when the program is running (don't know if there's a better way), and I want to store position information, like xPos and yPos, but I need to calculate a lot of other stuff based on those two variables, and creating more variables makes the code more easy to read/write.

But I don't know how this would affect the memory usage or speed, because I ran into some problems earlier when I tried to create a big array, it end up using like 300% of the dynamic memory, so I guess that when I declare a global byte, for example, the arduino allocates the whole memory as if you would use the biggest byte possible.

Sorry for bad english :s

  • Maybe try to change some constant variables to #define values, because they need nearly no storage. Also a ESP32 has 4MB of flash, with that much more place (the Mega has 256KB) place shouldn't be a problem anymore. – Python Schlange Sep 23 at 18:28
  • Well, I want to know if should I avoid creating more variables that I know they can be calculated easily, but makes the code more readable, or if I just stick to the most basic variables. – Juan Sep 23 at 18:40
  • Thanks @PythonSchlange, I'll keep that in mind, I never understood the use of #define till now. – Juan Sep 23 at 18:49
  • On an Arduino Mega, the “biggest byte possible” is 8-bits wide. – Edgar Bonet Sep 23 at 18:51
  • 1
    The question is one of time vs. space. If time is more important then use up memory to hold the values. If memory is more important then you use some time to recalculate things each time. Only you really know what your project does and what its needs are. There simply is not one right answer to this question. – Delta_G Sep 23 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.