How does an instantiated class' 'loop()' get called by Arduino framework

I'm using a class that has its own loop() function defined, but I don't see when that would get called and by whom.

I know my main program's loop function is called, but what about a class that also has a loop function. Where / How often would that get called?

Thanks

snoop911:
I'm using a class that has its own loop() function defined, but I don't see when that would get called and by whom.

I know my main program's loop function is called, but what about a class that also has a loop function. Where / How often would that get called?

Thanks

what class?

no example?

no code?

The loop() function in your class will only get called when YOU call it.

The answer is simple: never, unless YOUR code calls it. There is no magic in naming a function loop(). Arduino knows about only the one loop() function it requires. You can have 10 others in different places, and the Arduino environment will never so much as look at them, much less call them.

Regards,
Ray L.

Not sure I see the problem. The loop() that every Arduino program has is called from main.cpp. If you have a class with a method called loop(), then your code accesses it through the instantiation of the class object. For example, if the object of the class is named myObject, then a call to the class loop() method becomes myObject.loop(). There is no conflict between loop() and myObject.loop(). It also follows that you must explicitly call myObject.loop() with your own code.

I use this all the time - breaking my sketch up into components that each have their own loop(). You call the loop() of your components in the main loop. You can do this individually, by making arrays of objects, even by creating virtual methods.

It's very simple, very basic time-slicing and works great.

class WindowWiper {
  public:
  void loop() {
    …;
  }
}

class BugsprayDispenser {
  public:
  void loop() {
    …;
  }
}


WindowWiper front, rear;
BugsprayDispenser dispenser;

void setup() {
  front.attach(FRONT_WINDOW);
  rear.attach(REAR_WINDOW);
  dispenser.attach(DISPENSER_BUTTON, DISPENSER_NOZZLE);
}

void loop() {
  front.loop();
  rear.loop();
  dispenser.loop();
}

A more serious example is in post #10 on this this thread.

Cool, that makes sense.. Reason I ask, is that I'm currently porting a CC3100/MQTT example (MQTT_Continuous_Publish_Potentiometer) that's based on the MSP430/Energia:

http://energia.nu/wordpress/wp-content/uploads/2014/09/PubSubClient.zip

I understand Energia follows Arduino/Wiring very closely, with a nearly identical IDE feel, and similar setup/loop constructs.

Since a PubSubClient object gets created, but it's loop() is never explicitly called, I assumed that there was some magic going on under the hood!

Thanks for clarifying!