title: Shorten Lua code irrigation timer
body: <p>Hi I am new to coding and set about doing some code for a irrigation controller running on the Control By Web Product which uses Lua.</p>
  
  <p>My spec was for it to Operate as follows </p>
  
  <ul>
  <li><ul>
  <li><p>Set a register for each valve run time. Then trigger the sequence so they water one after another. </p></li>
  <li><p>Ability to pause the sequence (timers) and have the valve turn off
  while paused and restart the timers and turn valve on when unpaused.</p></li>
  <li><p>Ability to manually start valves and any time </p></li>
  <li><p>Have a number of sequences happening at the same time in the same
  script.</p></li>
  </ul></li>
  </ul>
  
  <p>I have done the below script but it is to big more that 8KB allowed. I'm sure there is ways to make it smaller but is beyond me at this stage.</p>
  
  <p>If anyone is willing to have a go. I am willing to pay to have this code fixed up. 
  Cheers 
  Jon</p>
  
  <hr>
  
  <pre><code>-- 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 &gt; 0 and 
       h == 0 then -- only start if this valve hasn't already run
       io.valve1 = 1
       else if reg.rRunTimeV1 &lt;= 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 &gt; 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 &lt;= reg.rFertStartTime then
                       reg.rRunFert = 1
                    end
  
              end
  
      -- turns relay off if timer 0
           if  reg.rTimeToOffV1 &lt;= 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 &gt; 0 and 
        h == 1 and
        i == 0 then -- only start if this valve hasn't already run
        io.valve2 = 1
        else if reg.rRunTimeInject &lt;= 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 &gt; 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 &lt;= 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 &gt; 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 &lt;= 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 &gt; 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 &lt;= 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 &gt; 0 and 
       flagAgitateFin == 0 then -- only start if this valve hasn't already run
       reg.fertAgitate = 1
       else if reg.rRunTimeAgitate &lt;= 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 &gt; 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 &lt;= 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 &gt; 0 and 
        flagAgitateFin == 1 and
        flagInjectFin == 0 then -- only start if this valve hasn't already run
        io.fertPump = 1
        else if reg.rRunTimeInject &lt;= 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 &gt; 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 &lt;= 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
  </code></pre>