缩短Lua代码灌溉计时器

时间:2017-04-02 07:46:26

标签: lua

您好我是编码的新手,并开始为使用Lua的Control By Web产品上运行的灌溉控制器执行一些代码。

我的规格是按照以下操作

    • 为每个阀门运行时间设置一个寄存器。然后触发序列,使它们一个接一个地浇水。

    • 能够暂停序列(定时器)并关闭阀门 暂停并重新启动定时器并在未启用时打开阀门。

    • 能够随时手动启动阀门

    • 同时发生多个序列 脚本。

我已经完成了以下脚本,但允许更多的8KB。我确信有办法让它变小,但在现阶段超出我的范围。

如果有人愿意去。我愿意支付修复此代码的费用。 干杯 乔恩

-- Sequence Timer for the control of 4 valves and fertigation on Control By Web X600


-- Flag Variabls for each Valve relay
flagV1 = 0  --Valve 1
flagV2 = 0
flagV3 = 0
f = 0

-- Flag Variables for Valve sequence

g = 0   -- sequence active 
h = 0   -- valve 1 Finished
i = 0   -- valve 2 Finished
j = 0   -- valve 3 Finished
k = 0   -- valve 5 Finished


-- Flag Veriables for fert relays
f_Agitate = 0
f_Inject = 0

-- flag Virables for fert seqance
   flagFertRunning = 0
    flagAgitateFin = 0
     flagInjectFin = 0
while true do

    print ("flagV1 V1 ",flagV1)
    print ("flagV2 V2 ",flagV2)
    print ("flagV3 V3 ",flagV3)
    print ("g triggrer ",g)

  if reg.rArea1active == 1 and
    reg.rTriggerArea1 == 1 then
      g = 1
     else if reg.rArea1active == 0 then   -- reset the seqence
      g = 1
      h = 1
      i = 1
      j = 1
      k = 1
     end
   end

  -- start valve 1
  if g == 1 and reg.rRunTimeV1 > 0 and 
     h == 0 then -- only start if this valve hasn't already run
     io.valve1 = 1
     else if reg.rRunTimeV1 <= 0 then
     h = 1
     end
  end

-- ******control for valve 1

    -- Moves the run time into the timer if relay tuned on and timer O
            if io.valve1 == 1 and
                reg.rTimeToOffV1 == 0 and
                               flagV1 == 0 then
                               flagV1 = 1   -- Valve has been opened 
                  reg.rTimeToOffV1 = reg.rRunTimeV1
                print ("V1 if-1 ",io.valve1)
            end

    -- count down if relay on
            if reg.rTimeToOffV1 > 0 and
                io.valve1 == 1 then
                         flagV1 = 1 -- Valve has been opened 
                if reg.t3second == 0 then
                     reg.rTimeToOffV1 = reg.rTimeToOffV1-.5
              print ("V1 time to off " ,reg.rTimeToOffV1) 
              end

  -- Start fert
                  if reg.rFertV1 == 1 and
                   reg.rTimeToOffV1 <= reg.rFertStartTime then
                     reg.rRunFert = 1
                  end

            end

    -- turns relay off if timer 0
         if  reg.rTimeToOffV1 <= 0 and
                                flagV1 == 1 then
                       io.valve1 = 0
                     reg.rRunFert = 0
                reg.rTimeToOffV1 = 0
                    if io.valve1 == 0 then flagV1 = 0 -- timer finished valve closed                
                    end                
                    if g == 1 then h = 1   --valve 1 finished lock                
                    print ("V1 if-3 ",io.valve1)
                   end
          end

  -- start valve 2
   if g == 1 and reg.rRunTimeInject > 0 and 
      h == 1 and
      i == 0 then -- only start if this valve hasn't already run
      io.valve2 = 1
      else if reg.rRunTimeInject <= 0 then
      i = 1
      end
   end



-- *****control for Valve 2

    -- Moves the run time into the timer if relay tuned on and timer O
      if io.valve2 == 1 and
         reg.rTimeToOffV2 == 0 and
                            flagV2 == 0 then
                              flagV2 = 1    -- Valve has been opened 
                  reg.rTimeToOffV2 = reg.rRunTimeInject
                    print ("V2 if-1 ",io.valve2)
          end

     -- count down if relay on
             if  reg.rTimeToOffV2 > 0 and
                     io.valve2 == 1 and
                       reg.t3second == 0 then
                                   flagV2 = 1   -- Valve has been opened 
                   reg.rTimeToOffV2 = reg.rTimeToOffV2-.5

         print ("V2 time to off " ,reg.rTimeToOffV2)
     end

    -- turns relay off if timer 0
         if  reg.rTimeToOffV2 <= 0 and
                                flagV2 == 1 then
                               io.valve2 = 0
                           reg.rTimeToOffV2 = 0
                                  if io.valve2 == 0 then flagV2 = 0 -- timer finished valve closed
                                  end
                                  if g == 1 then i = 1  -- valve 2 finished lock
                                  end
                    print ("V2 if-3 ",io.valve2)
         end


-- start valve 3
   if g == 1 and reg.rRunTimeV3 > 0 and
      h == 1 and
      i == 1 and
      j == 0 then -- only start if this valve hasn't already run
    io.valve3 = 1
     else if reg.rRunTimeV3 <= 0 then
     h = 1
     end
    end

-- ******control for Valve 3

    -- Moves the run time into the timer if relay tuned on and timer O
      if io.valve3 == 1 and
         reg.rTimeToOffV3 == 0 and
                            flagV3 == 0 then
                            flagV3 = 1  -- Valve has been opened 
                  reg.rTimeToOffV3 = reg.rRunTimeV3
    print ("V3 if-1 ",io.valve3)
          end

     -- count down if relay on
             if  reg.rTimeToOffV3 > 0 and
                     io.valve3 == 1 and
                       reg.t3second == 0 then
                                   flagV3 = 1   -- Valve has been opened 
                   reg.rTimeToOffV3 = reg.rTimeToOffV3-.5
         print ("V3 time to off " ,reg.rTimeToOffV3)
     end

    -- turns relay off if timer 0
         if  reg.rTimeToOffV3 <= 0 and
                                flagV3 == 1 then
                               io.valve3 = 0
                           reg.rTimeToOffV3 = 0
                                  if io.valve3 == 0 then flagV3 = 0 -- timer finished valve closed
                                  end
                                  if g == 1 then j = 1  -- valve 3 finished lock
                                  end
                 print ("V3 if-3 ",io.valve3)
         end

  -- reset the seqence
   if g == 1 and
      h == 1 and 
      i == 1 and
      j == 1 then
            g = 0
            h = 0
            i = 0
            j = 0
            k = 0
   end

   -- ******************Fert control ********************************************


    if reg.rFertActive == 1 and
             rRunFert  == 1 then
              flagFertRunning = 1
     else if reg.rFertActive == 0 then   -- reset the seqence
             flagFertRunning = 1
              flagAgitateFin = 1
               flagInjectFin = 1
     end
   end

  -- Start Agitate
  if flagFertRunning == 1 and reg.rRunTimeAgitate > 0 and 
     flagAgitateFin == 0 then -- only start if this valve hasn't already run
     reg.fertAgitate = 1
     else if reg.rRunTimeAgitate <= 0 then
     flagAgitateFin = 1
     end
  end

-- ******control for Agitate

    -- Moves the run time into the timer if relay tuned on and timer O
            if reg.fertAgitate == 1 and
                reg.rTimeToOffAgitate == 0 and
                               f_Agitate == 0 then
                               f_Agitate = 1    -- Valve has been opened 
                  reg.rTimeToOffAgitate = reg.rRunTimeAgitate
                print ("V1 if-1 ",reg.fertAgitate)
            end

    -- count down if relay on
            if reg.rTimeToOffAgitate > 0 and
                reg.fertAgitate == 1 then
                         f_Agitate = 1  -- Valve has been opened 
                if reg.t3second == 0 then
                     reg.rTimeToOffAgitate = reg.rTimeToOffAgitate-.5
                end
              print ("V1 time to off " ,reg.rTimeToOffAgitate) 
              end



    -- turns relay off if timer 0
         if  reg.rTimeToOffAgitate <= 0 and
                                f_Agitate == 1 then
                       reg.fertAgitate = 0
                     reg.rRunFert = 0
                reg.rTimeToOffAgitate = 0
                    if reg.fertAgitate == 0 then f_Agitate = 0 -- timer finished valve closed               
                    end                
                    if flagFertRunning == 1 then flagAgitateFin = 1   --valve 1 finished lock                
                    print ("V1 if-3 ",reg.fertAgitate)
                   end
          end

  -- ***************start Inject**********************
   if flagFertRunning == 1 and reg.rRunTimeInject > 0 and 
      flagAgitateFin == 1 and
      flagInjectFin == 0 then -- only start if this valve hasn't already run
      io.fertPump = 1
      else if reg.rRunTimeInject <= 0 then
      flagInjectFin = 1
      end
   end


-- *****control for inject *****************

    -- Moves the run time into the timer if relay tuned on and timer O
      if io.fertPump == 1 and
         reg.rTimeToOffInject == 0 and
                            f_Inject == 0 then
                            f_Inject = 1    -- Valve has been opened 
                  reg.rTimeToOffInject = reg.rRunTimeInject
                    print ("V2 if-1 ",io.fertPump)
          end

     -- count down if relay on
             if  reg.rTimeToOffInject > 0 and
                     io.fertPump == 1 and
                    reg.t3second == 0 then
                                   f_Inject = 1 -- Valve has been opened 
                       reg.rTimeToOffInject = reg.rTimeToOffInject-.5

         print ("V2 time to off " ,reg.rTimeToOffInject)
     end

    -- turns relay off if timer 0
         if  reg.rTimeToOffInject <= 0 and
                         f_Inject == 1 then
                               io.fertPump = 0
                           reg.rTimeToOffInject = 0
                                  if io.fertPump == 0 then f_Inject = 0 -- timer finishef_Inject valve closed
                                  end
                                  if flagFertRunning == 1 then flagInjectFin = 1  -- valve 2 finished lock
                                  end
                    print ("V2 if-3 ",io.fertPump)
         end




  -- reset  fert seqence
   if flagFertRunning == 1 and
      flagAgitateFin == 1 and 
      flagInjectFin == 1 then
            flagFertRunning = 0
            flagAgitateFin = 0
            flagInjectFin = 0
   end

 -- timer for count down
  print(reg.t3second)
          if reg.t3second == 0 then
              reg.t3second = 3
         end
  sleep(1000)
 end

0 个答案:

没有答案