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() 
void loop() 

Is there any method to avoid that ?

  • 1
    You should re-factor your code so it only uses one setup and loop function. – Majenko Apr 6 at 19:57
  • have you considered using multitasking? – Sim Son Apr 6 at 21:10
  • @SimSon The Arduino doesn't support multitasking. It's a very basic microcontroller. It has a setup() function and a loop() function and that's it. – Duncan C Apr 6 at 22:27
  • the OP seems to use an ESP8266. As far as I know there are libraries for multitasking. At least for the ESP32 I know for sure. Even on an atmega there are ways to implement multitasking, in my understanding the principle of multitasking has not much to do with the actual hardware. – Sim Son Apr 6 at 22:35
  • @SimSon ESP32 has two cores, ESP8266 only one, so they're not the same – Jaromanda X Apr 7 at 1:10

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 ArduinoObjects (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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.