I want to run multiple sketches on one sketch. I use the method of multiple void loops. It works but esp8266 take a lot of time to execute commands.
void Setup()
{
setup1();
setup2();
}
void loop()
{
loop1();
loop2();
}
Is there any method to avoid that ?
2 Answers 2
If the setup()
or loop()
functions call delay()
then of course that will delay getting to the next function and will slow down the whole process.
The only way to deal with that is to rewrite the code, combining what you're calling and fixing it so that it takes into account the other work it needs to do.
Same thing if you call any blocking functions, like reading from Serial
until you get a newline.
If that's not the case... well, the ESP8266 is a small, not particularly fast processor. If there are no delays or blocking functions then your code is running as fast as your code might run and you'll just have to live with it.
As long as you're not using delay()
or other blocking code, as John mentions in his answer, that approach will work fine. I have a fairly large project where I defined a class ArduinoObject
. The ArduinoObject
class has a setup method and a loop method, and the main sketch creates and manages an array of ArduinoObject
s (and another stack of a subclass of ArduinoObject
called a MenuObject
that manages menus on the LCD screen the project uses.)
At times there are a dozen or more ArduinoObjects
being called on each pass through the sketch's loop()
function and it works just fine. The key thing is to write your sub-loops to be FAST. They should do a tiny amount of work on each pass, and then a little more on the next pass (if needed.) You never want one of your sub-loops to take enough time to make the other sub-loops "stutter".
setup()
function and aloop()
function and that's it.