How to code Arduino that works like IEC 61131-3 - Logic Controllers ?

Hello people,

I have a question regarding programming the arduino, so I have an arduino nano and I want to have to use it similar like a logic controller, using multiple timers according to the inputs in a non-blocking type of way. I have added to the attachment an example interface that lets me use this, to summarize the picture and my intentions of use:

  • The Controller will have 4 inputs, two of them starts the operation and two of them stops the operation.
  • Each of the Start Buttons (Blue Buttons in the attachment) starts a timer that counts down from a given value, and as the timer is on, the OUTPUT section (can be a motor, light bulb etc.) is turned on and the other start button does the same function with its own timer.
  • The Stop Buttons can be pressed any time to stop the timer from counting thus stopping the output signal

Extra function (different from the attachment):
I would like to also be able to take other inputs and run some functions as well while the timer is working and/or not working.

My intention is to use at least 3-4 different timers this way on an Arduino Nano, I’ve tried the coding with While loops, non-blocking delays with milis() etc. but can’t seem to achieve it, can you help me on this and suggest any examples.

Thanks in advance

I would suggest you read the tutorial Several things at the same time to learn how to structure your code so you repeatedly spin through loop() doing small, incremental things such as checking the timers, reading the sensors, etc.

OSs like Linux need to support many timed events. they do so with a single hardware timer

the approach is to sort the events chronologically and create a linked list. each element on the list identifies the event and the difference in time from the preceding event.

arduino code can wait for the first event on the list to expire, process the event and then start waiting for the next on the list based on the difference until the next event.

new events can be inserted in the list, determining the delta time for the event being inserted and subtracting that delta from the following element.

multiple events can have the same time where the delta for the 2nd event is zero

using this approach, you can efficiently time as many events as you like

You need bunch of timers?

I have code that'll do exactly that. But every time I offer it up, everyone beats me up about it.

-jim lee

Thanks for your replies

I'll try to do it the way "Several things at the same time" thread offers, I now see where I did things wrong/complicated.

gcjr:
OSs like Linux need to support many timed events. they do so with a single hardware timer

the approach is to sort the events chronologically and create a linked list. each element on the list identifies the event and the difference in time from the preceding event.

arduino code can wait for the first event on the list to expire, process the event and then start waiting for the next on the list based on the difference until the next event.

new events can be inserted in the list, determining the delta time for the event being inserted and subtracting that delta from the following element.

multiple events can have the same time where the delta for the 2nd event is zero

using this approach, you can efficiently time as many events as you like

I don't know if I understood correctly but I think you mean to say that I can maybe assign starting and ongoing values and time the other objects as a difference from the ongoing value of the previous operation I guess

jimLee:
You need bunch of timers?

I have code that'll do exactly that. But every time I offer it up, everyone beats me up about it.

-jim lee

I would be grateful if you share your approach as well, I would like to see all the opinions and choose the best one for my application, this way my coding understanding will develop as well

Hi,
Have you googled;

arduino plc

Tom… :slight_smile:

ky-yil:
I don't know if I understood correctly but I think you mean to say that I can maybe assign starting and ongoing values and time the other objects as a difference from the ongoing value of the previous operation I guess

lets say you have events 10, 21 and 33 msec from now. create a list with an element for each event identifying the event and the delta from the previous event. the delta for the 1st event is 10, 11 for the 2nd event and 12 for the 3rd event

a timer (e.g. using millis()) waits 10 msec for the 1st event, then starts waiting for 11 msec to expire.

with a few calculations new events can be appended or inserted into the list

Are you comfortable with using classes, objects, libraries, c++ things like that? If so..

So you have two things that once activated do some action for x amount of time then stop. Or if the stop call is made, they stop early. When restarted do they start from the beginning or remember where they left off?

-jim lee