1

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 ?

8
  • 1
    You should re-factor your code so it only uses one setup and loop function.
    – Majenko
    Apr 6, 2019 at 19:57
  • have you considered using multitasking?
    – Sim Son
    Apr 6, 2019 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, 2019 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, 2019 at 22:35
  • @SimSon ESP32 has two cores, ESP8266 only one, so they're not the same Apr 7, 2019 at 1:10

2 Answers 2

1

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.

0

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. 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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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