I want to introduce a boolean that is the first iteration of loop and then false every subsequent iteration. I want it invisible, so I have to modify loop in some way. Is that possible?
1 Answer
The way to do this isn't to overload the loop function, the way to do this is to abuse the linker and replace main().
The main.cpp file that gets used in the Arduino make prototypes main as int main(void)
. On my system at least the linker prefers the longer prototype, so you can just do this:
int main(int argc, char* argv[])
{
init();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
//The one shot loop...
loop();
if (serialEventRun) serialEventRun();
//Put whatever else you need to do between before the unending loop here.
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
Both get compiled, but the int main(int argc, char* argv[])
prototyped version gets linked as the entry point, so your "invisibile" code gets executed.
setup()
called once the first time, andloop()
called forever afterwards? What's the point? You should edit your question to make it much clearer what you really want.