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.

Single button short/long press, switch/dim
#1
Hi

I'm quite new at this, and believe I completed my first real coding in LUA. The reason for this script was to be able to do lots of things using just one dumb push-button, connected to an input module. I scripted so that a short press switches the light ON/OFF, and a long press toggles between increasing and decreasing dimming, from a 1 bit input to a 4 bit dimming object. 

No question involved, just wanted to share, and hopefully maybe help some of you.

NOTE: Future expansion of the script involves changing the effekt whether it's day or it's night, where my girlfriend wakes up, gotta pee, and want to turn on several different lights on a very low brightness. Therefore the script, and not just the usage of the build-in dimming function.

Code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
tryk = event.getvalue()             --- The pushbutton itself --- trykgrp = '0/3/1'             --- Time in seconds, to destinguish between short- and long-press --- longpress = 0.5             --- Latest Dimming Direction. true: UP, false: DOWN. --- dimdir = storage.get('dimdirwalkin') if dimdir == nil then   dimdir = false end             --- Dimming object, usually 4-bit relative dimming object on fx DALI Gateway --- dimobject = '0/0/32'             --- Feedback from light switch. ON/OFF--- lysgrp = '0/0/35' lys = grp.getvalue(lysgrp)             --- Switch output ON/OFF --- switch = '0/0/31'        --- Determins whether it has is dimming, and therefore needs a stop-signal. true: dimming, false: not dimming (can switch).--- dimming = storage.get('dimmingwalkin') if dimming == nil then   dimming = false end                         --- Script begins ---             --- It's a short press, and is currently NOT dimming, therefore acting as switch --- if tryk == false and dimming == false then   if lys == true then     tscommand = false   elseif lys == false then     tscommand = true   end   grp.write(switch, tscommand) end             --- Was dimming and needs a stop, hence the release of push-button --- if tryk == false and dimming == true then   if dimdir == false then       value = 8   elseif dimdir == true then     value = 0   end   if value == 8 then     dimdirnew = true   elseif value == 0 then     dimdirnew = false   end     grp.write(dimobject, value)                 --- Updates latest dimming direction, and sets it out of dimming state ---     storage.set('dimdirwalkin', dimdirnew)   storage.set('dimmingwalkin', false) end              os.sleep(longpress)             --- If push-button is still pressed, it's interpretted as long-press --- tryk = grp.getvalue(trykgrp) if tryk == true then   if dimdir == false or lys == false then       value = 9   elseif dimdir == true and lys == true then     value = 1   end   grp.write(dimobject, value)   tryk = grp.getvalue(trykgrp)           --- Sets the dimmingstate, to get a stop-signal on release ---   storage.set('dimmingwalkin', true) end
Reply


Forum Jump: