1

I want to avoid global variables when using my own classes in Arduino. Here is a example.

void setup(){
/* setup here */
  classA objectA;
}

void loop(){
  objectA.someMethod();
}

I know that my compiler will complain about it (out of scope, etc) but is it possible to do that or just forget it and use globally?

Thanks in advance!

marquesm91
  • 135
  • 1
  • 5
  • Use globals. They're the recommended way. There are other ways, but global static declaration is the right way of doing it. – Majenko Sep 07 '16 at 22:39
  • @Majenko even if my classes getting bigger? I don't have to worry about that? – marquesm91 Sep 07 '16 at 22:40
  • 1
    Even if your classes getting bigger. By declaring it as a global with static declaration (`MyClass foo;`) you are reserving the RAM at compilation time. All other ways of doing it involve dynamic RAM allocation and that is bad on a microcontroller. – Majenko Sep 07 '16 at 22:42
  • @majenko Are you familiar with any patterns to help organize larger, non-trivial programs? Polluting the global space is frowned upon in most other problem domains. – Andrew Allbright Sep 08 '16 at 01:44
  • A program small enough to fit in the Arduino Uno is probably too small for global name space pollution to be a very serious concern. By using objects, you are already in a sense organizing the name space. If you do worry about your global name space, you could put all your globals in their own name space, or inside a big “app” object. – Edgar Bonet Sep 08 '16 at 08:07

1 Answers1

1

If you want a variable to be available within more than one function it has to be global. There is no other possible way of doing it.

If you are only using the variable in the loop() function and nowhere else you can declare it static in loop(), which has the same effect of making it global, but it is only possible to see it within loop().

You can make a pointer global, then create a new object in setup() (using new...) and assign it to the pointer, but that has the potential of making a mess of your heap and should be avoided unless absolutely necessary. Also it's pointless, because you don't gain anything over having the object statically created in the global scope.

Majenko
  • 103,876
  • 5
  • 75
  • 133
  • Seems enough for me. You convinced me! :) – marquesm91 Sep 07 '16 at 22:42
  • I read to avoid new and delete when coding for arduino. It is not my priority. I am just curious if is possible to do what I asked. – marquesm91 Sep 07 '16 at 22:45
  • You still end up with a global variable, just a different type of variable. – Majenko Sep 07 '16 at 22:46
  • This answer isn't satisfying. There has to be a way to balance programmer efficiency (e.g. organize logical bits of code in classes) with memory constraints. Is there not a compiler that can optimize the end-product in the global namespace way you suggest, but also provide the developer-friendly code organization that @marquesm91 is asking for? – Andrew Allbright Sep 08 '16 at 01:46
  • It is worth noting that the global constructors get called before the initializations done by the Arduino core and `setup()`. That's why many standard objects have a `begin()` method to do any initialization that needs to be deferred until the core is ready. – Edgar Bonet Sep 08 '16 at 08:13
  • @AndrewAllbright: The C++ compilers have a `namespace` keyword. – Edgar Bonet Sep 08 '16 at 08:13
  • 1
    You could completely ignore the global space and the `setup()` function and just treat `loop()` like `main()`. Stick a `while(1)` at the end of `loop()` to stop it looping, define all your variables as local to `loop()`, then pass them all as parameters to where they are going. What do you end up with? You end up with global now inside `loop()` and allocated on the stack instead of the BSS. Big deal. Just stick with global, it's easier. – Majenko Sep 08 '16 at 09:52