ChaddJackson12

Elevator Control

Jan 7th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. --[[ Notes ]]--
  2. --[[
  3.     -- It is a good thing to organize your code.
  4.     -- I have placed comments explaining the code.
  5.     -- You should put your functions and variables before the code that calls them, so they aren't  "nil"
  6.     -- The floor number is not known when the computer is restarted, so you should send it to the floor it's not on to set it, or it can go either way.
  7. --]]
  8. --[[ Local Variables ]]--
  9. Listen = true -- Start the loop to listen for key presses
  10. if fs.exists("floor.txt") then -- Is the floor location saved?
  11.     f = fs.open("floor.txt", "r") -- Open "floor.txt" in read mode
  12.     GetN = f.readLine() -- Get the number from the file
  13.     f.close() -- close the file
  14.     n = tonumber(GetN) -- Change "n" to n... Making it a number instead of a string (string in " 's and number not in " 's)
  15. else
  16.     n = nil
  17. end
  18.  
  19. --[[ Functions ]]--
  20. -- Elevator --
  21. function elevateDown() -- You define the function
  22.   redstone.setOutput("top", true) -- You power redstone on the top of the computer
  23.   sleep(0.5) -- Sleep for 0.5 seconds
  24.   redstone.setOutput("top", false) -- You take out the power for the top of the computer
  25. end -- End the function
  26.  
  27. function elevateUp()
  28.   redstone.setOutput("bottom", true) -- You power redstone on the top of the computer
  29.   sleep(0.5)
  30.   redstone.setOutput("bottom", false) -- You take out the power from the bottom of the computer
  31. end
  32.  
  33. while Listen do -- While you want to have someone press keys, do the following code:
  34.     repeat -- Start a repeat loop
  35.         event, key = os.pullEvent('key') -- Listen for a key event
  36.         os.sleep(0.1)
  37.     until key -- Exit loop when key has been pressed
  38.     if key == 200 then -- Check if up key was pressed
  39.         if n == 1 then -- Check if elevator is already at the top floor
  40.             print("Elevator is already at the top floor.") -- Tell the player the elevator is already at the top floor
  41.         else
  42.             n = 1 -- Set the floor value
  43.             Listen = false -- Exit the loop to listen for keys
  44.         end
  45.     elseif key == 208 then -- Check if down key was pressed
  46.         if n == 2 then
  47.             print("Elevator is already at the bottom floor.")
  48.         else
  49.             n = 2
  50.             Listen = false
  51.         end
  52.     else -- If the key pressed was invalid...
  53.         print("Please press Up or Down key.") -- Tell the player, and then reset the loop!
  54.     end
  55. end
  56.  
  57. while not Listen do -- Starts elevator actions when not listening for key presses.
  58.     if n == 1 then -- Should the elevator go up?
  59.         for i=1,11 do
  60.             elevateUp() -- Run the function "elevateUp()"
  61.             sleep(0.5)
  62.         end
  63.         f = fs.open("floor.txt", "w") -- Open "floor.txt" in "write" mode
  64.         f.writeLine(n) -- Write the value of "n" to the file
  65.         f.close() -- Close the file
  66.         os.reboot()
  67.     elseif n == 2 then -- Should the elevator go down?
  68.         for i = 1, 11 do
  69.             elevateDown() -- Run the function "elevateDown()"
  70.             sleep(0.5)
  71.         end
  72.         f = fs.open("floor.txt", "w")
  73.         f.writeLine(n)
  74.         f.close()
  75.         os.reboot()
  76.     end
  77. end
Advertisement
Add Comment
Please, Sign In to add comment