beubeu84
Hi everyone,
I have an Arduino library that I would like to include in a custom function block. Is there a way to do this? Or is there a way to call an Arduino extension periodically?
I'm experiencing freezes on my ESP32, which I think are because my Arduino code has a function that takes from 300 to 500ms to run, and I call my main program every 30ms.
Thanks in advance for any help.
Reply 0 0
fg71
If you are using a delay() in your Arduino extension the MCU could freezes. The reason for that is because the runtime is synchronous process.
If you like to have a delay() in your Arduino extension run loop you need to use millis()
Here is a example in Arduino IDE to do that Blink Without Delay | Arduino Documentation
Reply 0 0
thiagoralves
Putting your code in a function block would make no difference if it still takes 300-500ms to run. There is no multiprocessing in the runtime, so things will run in series anyway, inside a function block or in an Arduino extension. What you can do is have a counter in your Arduino extension so that you don’t call this slow function every time, but every 10 or 100 scan times. By the way, this is an awful lot of time for a function. Ideally you should look into ways of optimizing it so that it takes less time to run
Reply 0 0
beubeu84
Dear fg71 & thiagoralves,
Thank you for your replies.
To be more precise, and maybe this will help others: I am managing three groups of lights.
I read the consumed current of each group with three ACS712 sensors.
To do so, I use the ACS712.h Arduino library.
Every call to this library takes a customizable time; only after this time has elapsed does the program continue to run. I need to set this time to 80ms.
 
@thiagoralves, I thought the Arduino program was running in parallel with the OpenPLC program, thanks for the clarification.
So i created three boolean variables managed by OpenPLC to read each ACS712 every 300ms.
Now, the program runs smoothly, without any "freezes." And an update of the consummed current every 900ms is OK in my case.
However, it would be very nice to be able to include the library in a custom function block to call it directly in the PLC editor
Reply 0 0
thiagoralves
I'm glad it is working now. On the parallel thing, it is impossible to run two things at the same time with just one CPU. Modern OSes do something called "preemptive multitasking" that would switch from one task to another in a time slice, giving the illusion of parallelism even with one CPU core. In simple terms, this is quite expensive to do on a little "Arduino" board. The OpenPLC runtime can still handle multiple tasks though, and will switch from one task to the other, but this only happens at the end of a task (cooperative multitasking), never in the middle of it. So if you have anything too long in any of your tasks, it will hang the entire thing. That's why you experienced those freezes when you ran a very long function in your Arduino Extension.
Reply 0 0
Reply