User Tools

Site Tools


useful_programs

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
useful_programs [2019/09/16 15:10] – [Bit functions] emozolyakuseful_programs [2020/01/10 09:33] – [Universal Timer (TON / TOFF)] emozolyak
Line 57: Line 57:
 function outBit(condition, alias, b) -- output bool condition as 0 /1 into a bit of a internal register  function outBit(condition, alias, b) -- output bool condition as 0 /1 into a bit of a internal register 
      local new_value = outbit(condition, R(alias), b)      local new_value = outbit(condition, R(alias), b)
-     return new_value+     W(alias, new_value)
 end  end 
    
Line 274: Line 274:
  
 <code lua> <code lua>
- function Timer (bool_input, onDelay, offDelay, tmrAlias)+function Timer (bool_input, onDelay, offDelay, tmrAlias)
 --                bool      sec to ON  sec to OFF  string alias --                bool      sec to ON  sec to OFF  string alias
 + 
 +local now , nowString, curTimeStamp, curTmrState =  
 +os.time(), os.date("%c", now), R(tmrAlias), TRUE(tmrAlias .."_out"    
  
-local now , nowString, curTimeStamp, curTmrState =  os.time() +    DBG("Timer State Stamp", "|", tmrAlias, curTmrState, os.date("%c", curTimeStamp)) 
-                                                    os.date("%c", now),  +
-                                                    R(tmrAlias), +
-                                                    TRUE(tmrAlias .."_out"     +
-                                           +
-DEBUG("curTimeStamp - ", os.date("%c", curTimeStamp) ) +
-DEBUG ("cur "..tmrAlias.." State =  "..tostring(curTmrState))                  +
 -- protects from malfunctions on very first run  -- protects from malfunctions on very first run 
 if curTimeStamp == 0 then  if curTimeStamp == 0 then 
-    DEBUG("curTimeStamp was zero in this timer!")+    DBG("curTimeStamp was zero in this timer!")
     WriteReg(tmrAlias, now)     WriteReg(tmrAlias, now)
     return nil, 0 -- countdown      return nil, 0 -- countdown 
 end  end 
 + 
     -- in and output are equal                                                          -- in and output are equal                                                     
-    if (bool_input == curTmrState) then  + 
-        WriteReg(tmrAlias, now)                           DEBUG("timer input match state "+if (bool_input == curTmrState) then  
 +        WriteReg(tmrAlias, now) ; DBG("timer inputs match"
         return curTmrState, 0   -- as bool          return curTmrState, 0   -- as bool 
-     +         
-    -- TON      +                            -- TON      
-    elseif bool_input then  +elseif bool_input then  
-         if ((now - GetReg(tmrAlias)) > onDelay) then  +         if ((now - curTimeStamp) > onDelay) then  
-             WriteReg(tmrAlias .."_out", 1 +             SET(tmrAlias .."_out") ; WriteReg(tmrAliasnow; DBG("detected ON state after delay")
-                                                        DEBUG("detected ON state after delay (input, now, tmr, onDelay ):", bool_input, nowString, onDelay)+
              return true, 0                return true, 0  
         else          else 
-            local countDown = onDelay - (now - curTimeStamp) +            local countDown = onDelay - (now - curTimeStamp) ; DBG("countdown to On in "countDown)
-            DEBUG("countdown to On in " .. tmrAlias .. " ".. countDown)+
             return curTmrState, countDown             return curTmrState, countDown
         end          end 
 + 
 +                            -- TOFF     
 +elseif not bool_input then 
          
-    -- TOFF      +        if ((now - curTimeStamp) > offDelay) then    
-    elseif not bool_input then  +            RESET(tmrAlias .."_out") ; WriteReg (tmrAliasnow; DBG("detected OFF state after delay"
-        if (now - GetReg ( tmrAlias ) > offDelay) then    +            return false, 0   
-            WriteReg (tmrAlias .."_out", 0) +
-            DEBUG("detected OFF state after delay (input, now, tmr, offDelay ):", bool_input, nowString, offDelay+
-            return false  +
         else          else 
-            local countDown = offDelay - (now - curTimeStamp) +            local countDown = offDelay - (now - curTimeStamp) ; DBG("countdown to OFF in "countDown)
-            DEBUG ("countdown to OFF in " .. tmrAlias .." ".. countDown)+
             return curTmrState, countDown             return curTmrState, countDown
         end          end 
useful_programs.txt · Last modified: 2024/03/19 09:39 by emozolyak

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki