This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

convert percentage to 3 digital outputs
#1
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
Reply
#2
You have gaps between 30..35 and 70..75. Is this intended? Do you want to have hysteresis between these values?
Reply
#3
i have gaps, because i want to ensure that the digital outputs will never be energized simultaneously.
Hysterisis is a good idea.
Reply
#4
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
Reply
#5
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.
Reply
#6
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
Reply
#7
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.
Reply
#8
It should be fine if the % value probably comes from a PID script where control value is sent every X seconds.
Reply


Forum Jump: