Advertisement
Guest User

test1

a guest
Jul 8th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.50 KB | None | 0 0
  1. local component = require("component")
  2. local sides = require("sides")
  3.  
  4. function setProxy(address)
  5.   return component.proxy(component.get(address))
  6. end
  7.  
  8. -- inputs
  9. local lever4 = setProxy("9e801798")
  10. local lever2 = setProxy("e9bfc284")
  11. local lever1 = setProxy("364893bf")
  12. local confirm = setProxy("2ea39d69")
  13.  
  14. -- outputs
  15. local spr1 = setProxy("678a6377")
  16. local spr2 = setProxy("78fcfad4")
  17. local spr3 = setProxy("366d2881")
  18. local spr4 = setProxy("26b09697")
  19. local spr5 = setProxy("e8c8271f")
  20. local spr6 = setProxy("b75a65b4")
  21. local spr7 = setProxy("9a7fa821")
  22.  
  23. local loopLock = true
  24. local res = 0
  25. local sprinklerTable = {spr1, spr2, spr3, spr4, spr5, spr6, spr7}
  26. --[[
  27. local sprinklerTable = {}
  28. do
  29.   local spradr = {"678a6377", "78fcfad4", "366d2881", "26b09697", "e8c8271f", "b75a65b4", "9a7fa821"}
  30.   for i in 1,8 do
  31.     sprinklerTable[i] = setProxy(spradr[i])
  32.   end
  33. end
  34. ]]--
  35.  
  36.  
  37. function checkInputs()
  38.   confirmState = confirm.getInput(sides.north)
  39.   if confirmState > 10 then
  40.     print("confirmed!")
  41.     loopLock = false
  42.   end
  43. end
  44.  
  45. function toggleOutputs(result)  
  46.   if result ~= 0 then
  47.     target = sprinklerTable[result]
  48.     if target.getOutput(sides.east) > 10 then
  49.       target.setOutput(sides.east, 0)
  50.       print("Turned sprinkler ", target, " off!")
  51.     elseif target.getOutput(sides.east) == 0 then
  52.       target.setOutput(sides.east, 15)
  53.       print("Turned sprinkler ", target, " on!")
  54.     end
  55.   end
  56.   if result == 0 then
  57.     if confirm.getInput(sides.west) > 10 then
  58.       print("Enabling all sprinklers!")
  59.       toggleAllSprinklers(1)
  60.     elseif confirm.getInput(sides.west) == 0 then
  61.       print("Disabling all sprinklers!")
  62.       toggleAllSprinklers(0)
  63.     end
  64.   end
  65. end
  66.  
  67. -- debug
  68. function toggleAllSprinklers(selector)
  69.   local state = 0
  70.   if selector == 0 then
  71.     state = 0
  72.     print("State 0")
  73.   else
  74.     state = 15
  75.     print("State 1")
  76.   end
  77.   for i,v in ipairs(sprinklerTable) do
  78. --  for i, v in pairs(sprinklerTable) do
  79.       v.setOutput(sides.bottom, state)
  80.       print("Set ", v, " to ", state)
  81.   end
  82.  
  83. end
  84.  
  85. while true do
  86.   --main loop
  87.  
  88.  
  89.   while loopLock == true do
  90.     -- check sensor loop
  91.     res = 0
  92.     input1 = lever4.getInput(sides.north)
  93.     input2 = lever2.getInput(sides.north)
  94.     input3 = lever1.getInput(sides.north)
  95.    
  96.     if input1 > 10 then res = res + 4 end
  97.     if input2 > 10 then res = res + 2 end
  98.     if input3 > 10 then res = res + 1 end
  99.  
  100.     checkInputs()
  101.     os.sleep(0.5)
  102.   end
  103.  
  104.   print("Selected sprinkler: ", res)
  105.   toggleOutputs(res)
  106.  
  107.   os.sleep(4)
  108.  
  109.   loopLock = true
  110.   term.clear()
  111.   print("Accepting input.")
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement