Bjornalf

Frame Elevator motor controller

Mar 17th, 2013
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.53 KB | None | 0 0
  1. local curfloor
  2. local sel
  3.  
  4. local function loadOnStartup()
  5.   local h = fs.open('.data', 'r') -- hidden file
  6.  
  7.   assert(h, "ERROR: Could not open data file in read mode.")
  8.  
  9.   local t1 = h.readLine()
  10.   local t2 = h.readLine()
  11.   h.close()
  12.  
  13.   if t1 then
  14.     curfloor = t1
  15.   end
  16.  
  17.   if t2 then
  18.     sel = t2
  19.   end
  20. end
  21.  
  22. local function persistData()
  23.   local h = fs.open('.data', 'w')
  24.  
  25.   assert(h, "ERROR: Could not open data file in write mode.")
  26.   print(curfloor)
  27.   h.write(curfloor..'\n')
  28.   print(sel)
  29.   h.write(sel)
  30.   h.close()
  31. end
  32.  
  33. local function setCurfloor(s)
  34.   curfloor = s
  35.   persistData()
  36. end
  37.  
  38. local function setSel(s)
  39.   sel = s
  40.   persistData()
  41. end
  42.  
  43. local function down(n)
  44.   for i=1, n do
  45.     rs.setBundledOutput("back", colors.white)
  46.     sleep(2)
  47.     rs.setBundledOutput("back", colors.magenta)
  48.     sleep(.5)
  49.     rs.setBundledOutput("back", 0)
  50.     sleep(1)
  51.   end
  52. end
  53.  
  54. local function up(u)
  55.   for i=1, u do
  56.     rs.setBundledOutput("back", colors.orange)
  57.     sleep(1)
  58.     rs.setBundledOutput("back", colors.yellow)
  59.     sleep(.5)
  60.     rs.setBundledOutput("back", 0)
  61.     sleep(2)
  62.   end
  63. end
  64.  
  65. local function ground()
  66.   print(sel)
  67.   print(curfloor)
  68.  
  69.   if curfloor == sel then
  70.     print("already here")
  71.   else
  72.     if curfloor == "second" then
  73.       down(5)
  74.       setCurfloor("ground")
  75.     else
  76.       down(21)
  77.       setCurfloor("ground")
  78.     end
  79.   end
  80. end
  81.  
  82. function second()
  83.   print(sel)
  84.   print(curfloor)
  85.  
  86.   if curfloor == sel then
  87.     print("already here")
  88.   else
  89.     if curfloor == "ground" then
  90.       up(5)
  91.       setCurfloor("second")
  92.     else
  93.       down(16)
  94.       setCurfloor("second")
  95.     end
  96.   end
  97. end
  98.  
  99. function top()
  100.   print(sel)
  101.   print(curfloor)
  102.  
  103.   if curfloor == sel then
  104.     print("already here")
  105.   else
  106.     if curfloor == "ground" then
  107.       up(21)
  108.       setCurfloor("mountain")
  109.     else
  110.       up(16)
  111.       setCurfloor("mountain")
  112.     end
  113.   end
  114. end
  115.  
  116. rednet.open("right") -- you want to be using channels if you are going to use 'modem_message' not rednet api
  117.  
  118.  
  119. if fs.exists('.data') then
  120.   loadOnStartup()
  121. else
  122.   curfloor = 'ground'
  123.   sel = 'ground'
  124.   persistData()
  125. end
  126.  
  127. while true do
  128.   local event, side, sendChannel, replyChannel, msg, distSent = os.pullEvent()
  129.  
  130.   if event == "modem_message" then
  131.     setSel(msg)
  132.     print(event)
  133.     print(sel)
  134.     if sel == "ground" then
  135.       ground()
  136.     elseif sel == "second" then
  137.       second()
  138.     else
  139.       top()
  140.     end
  141.   else
  142.     print("not correct signal")
  143.   end
  144. end
  145.  
  146. shell.run("elevator")
Advertisement
Add Comment
Please, Sign In to add comment