Logic Machine Forum
convert percentage to 3 digital outputs - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: convert percentage to 3 digital outputs (/showthread.php?tid=4090)



convert percentage to 3 digital outputs - stavros - 14.06.2022

Hello,
I would like to convert a numeric (percentage) to 3 digital outputs (group addresses). For example i want to make the first DO to be 1 when the percentage is between 5-30%, the second between 35-70% and the third between 75-100%.
I want this to control o fcu.
Is there any idea how to do this.
Thank you


RE: convert percentage to 3 digital outputs - admin - 14.06.2022

You have gaps between 30..35 and 70..75. Is this intended? Do you want to have hysteresis between these values?


RE: convert percentage to 3 digital outputs - stavros - 14.06.2022

i have gaps, because i want to ensure that the digital outputs will never be energized simultaneously.
Hysterisis is a good idea.


RE: convert percentage to 3 digital outputs - RomansP - 14.06.2022

Hi, is that what you need?

Code:
percent_val = event.getvalue() -- your value

--the first 5-30%, the second between 35-70% and the third between 75-100%.

first_obj = '5/0/1' -- address of first object
second_obj = '5/0/2' -- address of second object
third_obj = '5/0/3'  -- address of third object

if(percent_val >= 0 and percent_val < 5) then -- turn off
  grp.write(first_obj, 0) -- first object set to 0
  grp.write(second_obj, 0) -- second object set to 0
  grp.write(third_obj, 0) -- third object set to 0
elseif( percent_val >= 5 and percent_val <= 30 ) then
  grp.write(first_obj, 1) -- first object set to 1
  grp.write(second_obj, 0) -- second object set to 0
  grp.write(third_obj, 0) -- third object set to 0
elseif( percent_val >= 35 and percent_val <= 70 ) then
  grp.write(first_obj, 0)
  grp.write(second_obj, 1)
  grp.write(third_obj, 0)
elseif( percent_val >= 75 and percent_val <= 100 ) then
  grp.write(first_obj, 0)
  grp.write(second_obj, 0)
  grp.write(third_obj, 1)
end



RE: convert percentage to 3 digital outputs - Erwin van der Zwart - 14.06.2022

I would improve this a bit as now you can have (shortly) 2 high contacts..

First write to false before writing anything to true (5-30% and 35-70% are not doing that)

Even a better way to do this is to have a “switch on” delay in the ETS application so you have a second pauze before a contact is closed.


RE: convert percentage to 3 digital outputs - RomansP - 15.06.2022

Erwin van der Zwart, thank you for your feedback.

Code:
percent_val = event.getvalue() -- your value

--the first 5-30%, the second between 35-70% and the third between 75-100%.

first_obj = '5/0/1' -- address of first object
second_obj = '5/0/2' -- address of second object
third_obj = '5/0/3'  -- address of third object

if(percent_val >= 0 and percent_val < 5) then -- turn off
  grp.write(first_obj, 0) -- first object set to 0
  grp.write(second_obj, 0) -- second object set to 0
  grp.write(third_obj, 0) -- third object set to 0
elseif( percent_val >= 5 and percent_val <= 30 ) then
  grp.write(second_obj, 0) -- second object set to 0
  grp.write(third_obj, 0) -- third object set to 0
  os.sleep(1) -- delay to do not have two objects on simultaneously
  grp.write(first_obj, 1) -- first object set to 1
elseif( percent_val >= 35 and percent_val <= 70 ) then
  grp.write(first_obj, 0)
  grp.write(third_obj, 0)
  os.sleep(1)
  grp.write(second_obj, 1)
elseif( percent_val >= 75 and percent_val <= 100 ) then
  grp.write(first_obj, 0)
  grp.write(second_obj, 0)
  os.sleep(1)
  grp.write(third_obj, 1)
end



RE: convert percentage to 3 digital outputs - Erwin van der Zwart - 15.06.2022

I would not use os.sleep in this script as you can have weird behavior when the input value is changed within a second (script can run in paralell), use 1 second “on delay” in the application of your actuator.


RE: convert percentage to 3 digital outputs - admin - 15.06.2022

It should be fine if the % value probably comes from a PID script where control value is sent every X seconds.