Advertisement
quenti77

Ascenseur

Jan 16th, 2013
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.72 KB | None | 0 0
  1. -- Init device
  2. local function initDevice()
  3.     rednet.open("back")
  4.     return peripheral.wrap("front")
  5. end
  6.  
  7. -- Init floor
  8. local function initFloor()
  9.     tab = {}
  10.     tab[0] = "B1"
  11.     tab[1] = "GF"
  12.    
  13.     return tab
  14. end
  15.  
  16. local function numFloor(f, tab)
  17.     result = -1
  18.    
  19.     for i = 0, #tab do
  20.         if tab[i] == f then
  21.             result = i
  22.         end
  23.     end
  24.    
  25.     return result
  26. end
  27.  
  28. -- Etage
  29. local function getFloor()
  30.    
  31.     -- If test
  32.     if rs.testBundledInput("right", 1) then
  33.         return 1 -- White
  34.     end
  35.    
  36.     if rs.testBundledInput("right", 2) then
  37.         return 0 -- Orange
  38.     end
  39.  
  40.     return -1
  41. end
  42.  
  43. -- Nouvelle etage
  44. local function getNextFloor(tableau)
  45.         return tableau[0]
  46. end
  47.  
  48. -- Ajout
  49. local function addFloor(tableau, etage)
  50.         local i = 0
  51.        
  52.         while tableau[i] ~= nil do
  53.                 i = i + 1
  54.         end
  55.        
  56.         tableau[i] = etage
  57.        
  58.         return tableau
  59. end
  60.  
  61. -- Suppression
  62. local function delFloor(tableau)
  63.         local i = 1
  64.         local j = 0
  65.         local tab = {}
  66.        
  67.         while tableau[i] ~= nil do
  68.                 j = i - 1
  69.                 tab[j] = tableau[i]
  70.                 i = i + 1
  71.         end
  72.         print("Deleting ...")
  73.         return tab
  74. end
  75.  
  76. local function openDoor(fl)
  77.     print("open door")
  78.  
  79.  for i = 1, 4 do
  80.   if fl == 0 then
  81.    rs.setBundledOutput("right", 8)
  82.   elseif fl == 1 then
  83.    rs.setBundledOutput("right", 32)
  84.   end
  85.   sleep(0.5)
  86.   rs.setBundledOutput("right", 0)
  87.   sleep(0.5)
  88.  end
  89. end
  90.  
  91. local function closeDoor(fl)
  92.     print("close door")
  93.  
  94.  for i = 1, 4 do
  95.   if fl == 0 then
  96.    rs.setBundledOutput("right", 4)
  97.   elseif fl == 1 then
  98.    rs.setBundledOutput("right", 16)
  99.   end
  100.  
  101.   sleep(0.5)
  102.   rs.setBundledOutput("right", 0)
  103.   sleep(0.5)
  104.  end
  105. end
  106.  
  107. -- Main function fields
  108. local actualFloor = "CF"
  109. local floorSelect = "CF"
  110. local floors = {}
  111. local etageTest = -1
  112. local etageActuel = 5
  113. local direction = 0
  114. local stateLift = 1
  115. local tickWait = 0
  116. local tickMax = 5
  117.  
  118. m = initDevice()
  119. f = initFloor()
  120.  
  121. while true do
  122.  
  123.     local s, msg, distance = rednet.receive(0.5)
  124.     local copy = {}
  125.     local bc = {}
  126.    
  127.     bc["floor"] = "CF"
  128.     bc["f"] = "CF"
  129.     bc["s"] = " "
  130.    
  131.     if msg ~= nil and s ~= nil then
  132.         copy = textutils.unserialize(msg)
  133.     end
  134.    
  135.     if copy["floor"] ~= nil then
  136.         floors = addFloor(floors, numFloor(copy["floor"], f))
  137.         print("Add floor : "..copy["floor"].." with id : "..numFloor(copy["floor"], f))
  138.         rednet.send(s, "ok_call")
  139.     end
  140.    
  141.     -- Add floor and prepare broadcast information
  142.     etageTest = getFloor()
  143.     if etageTest ~= -1 then
  144.         etageActuel = etageTest
  145.     end
  146.    
  147.     -- 1 = reset
  148.     -- 2 = open door
  149.     -- 3 = wait floor
  150.     -- 4 = close door
  151.     -- 5 = lift UP/DOWN
  152.     if stateLift == 1 then
  153.         stateLift = 2
  154.     elseif stateLift == 2 then
  155.         openDoor(etageActuel)
  156.         stateLift = 3
  157.     elseif stateLift == 3 then
  158.         if tickWait > tickMax then
  159.             direction = getNextFloor(floors)
  160.            
  161.             if direction ~= nil and direction ~= etageActuel then
  162.                 tickWait = 0
  163.                 stateLift = 4
  164.             elseif direction ~= nil and direction == etageActuel then
  165.                 floors = delFloor(floors)
  166.                 direction = etageActuel
  167.             else
  168.                 tickWait = 0
  169.             end
  170.         else
  171.             tickWait = tickWait + 1
  172.         end
  173.     elseif stateLift == 4 then
  174.         closeDoor(etageActuel)
  175.         stateLift = 5
  176.     elseif stateLift == 5 then
  177.         -- If < Then up Else if > Then down Else arrived floor
  178.         if etageActuel < direction then
  179.             bc["s"] = "U"
  180.             rs.setBundledOutput("left", 1)
  181.             sleep(0.1)
  182.             rs.setBundledOutput("left", 0)
  183.             sleep(0.1)
  184.         elseif etageActuel > direction then
  185.             bc["s"] = "D"
  186.             rs.setBundledOutput("left", 2)
  187.             sleep(0.1)
  188.             rs.setBundledOutput("left", 0)
  189.             sleep(0.1)
  190.         else
  191.             floors = delFloor(floors)
  192.             stateLift = 1
  193.         end
  194.     end
  195.    
  196.     bc["f"] = f[etageActuel]
  197.     bc["floor"] = f[etageActuel]
  198.     rednet.broadcast(textutils.serialize(bc))
  199.     sleep(0.5)
  200. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement