Guest User

Timer 2

a guest
Aug 29th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.67 KB | None | 0 0
  1. -- ######################
  2. -- ##
  3. -- ######################
  4.  
  5. local bCable = "back" -- Side of your bundled Cable
  6. local monitor = peripheral.wrap( "right" )
  7. local timers = { -- Format: {"Starting Mins","Starting Secs",Function to run when done}
  8.   {"10","00",function() rs.setBundledOutput(bCable,colors.combine(rs.getBundledInput(bCable),colors.red)) end},
  9.   {"5","00",function() rs.setBundledOutput(bCable,colors.combine(rs.getBundledInput(bCable),colors.orange)) end}
  10. }
  11.  
  12. --# Returns tTime in a xx:xx format
  13. local function formatTime(tTime)
  14.   return tTime[1]..":"..tTime[2]
  15. end
  16.  
  17. --# Decrements each timer
  18. local function updateTime()
  19.   local allDone = true
  20.   for i = 1, #timers do
  21.     if timers[i][2] == "00" then
  22.       if tonumber(timers[i][1]) > 0 then
  23.         timers[i][2] = "59"
  24.         timers[i][1] = tostring(tonumber(timers[i][1]) - 1)
  25.         allDone = false
  26.       elseif type(timers[i][3]) == "function" and not timers[i][4] then
  27.         timers[i][4] = true
  28.         timers[i][3]()
  29.       end
  30.     else
  31.       timers[i][2] = tostring(tonumber(timers[i][2]) - 1)
  32.       if tonumber(timers[i][2]) < 10 then
  33.         timers[i][2] = "0"..(tonumber(timers[i][2]))
  34.       end
  35.       allDone = false
  36.     end
  37.   end
  38.   return not allDone --# Returns 'true' if at least one timer counted down, false if all timers have finished
  39. end
  40.  
  41. --# Prints time to monitor
  42. local function printTime()
  43.   monitor.setCursorPos(1,6)
  44.   monitor.clearLine()
  45.   monitor.write("         T MINUS "..formatTime(timers[1]).." MINUTES")
  46.   monitor.setCursorPos(1,10)
  47.   monitor.clearLine()
  48.   monitor.write("         T MINUS "..formatTime(timers[2]).." MINUTES")
  49. end
  50.  
  51. -- ######################
  52. -- ##
  53. -- ######################
  54.  
  55. monitor.setCursorPos(1, 1)
  56. monitor.setBackgroundColor(colors.black)
  57. monitor.clear()
  58.  
  59. monitor.setTextColor(colors.red)
  60. monitor.setBackgroundColor(colors.black)
  61. monitor.setTextScale(1.5)
  62. monitor.setCursorPos(1, 3)
  63. monitor.write("                 DANGER")
  64. monitor.setTextColor(colors.red)
  65. monitor.setBackgroundColor(colors.black)
  66. monitor.setCursorPos(1, 4)
  67. monitor.write("      EMERGENCY DESTRUCTION SYSTEM")
  68. monitor.setTextColor(colors.yellow)
  69. monitor.setBackgroundColor(colors.black)
  70. monitor.setCursorPos(1, 5)
  71. monitor.write("    ON ACTIVATION IT WILL DETONATE IN")
  72. monitor.setTextColor(colors.yellow)
  73. monitor.setBackgroundColor(colors.black)
  74. monitor.setCursorPos(1, 6)
  75. monitor.write("         T MINUS       MINUTES")
  76. monitor.setTextColor(colors.red)
  77. monitor.setBackgroundColor(colors.black)
  78. monitor.setCursorPos(18, 6)
  79. monitor.write("10:00")
  80.  
  81. monitor.setTextColor(colors.red)
  82. monitor.setBackgroundColor(colors.black)
  83. monitor.setCursorPos(1, 8)
  84. monitor.write("               FAILSAFE WARNING")
  85. monitor.setTextColor(colors.yellow)
  86. monitor.setBackgroundColor(colors.black)
  87. monitor.setCursorPos(1, 9)
  88. monitor.write("  CUT-OFF SYSTEM WILL NOT OPERATE AFTER")
  89. monitor.setTextColor(colors.yellow)
  90. monitor.setBackgroundColor(colors.black)
  91. monitor.setCursorPos(1, 10)
  92. monitor.write("          T MINUS      MINUTES")
  93. monitor.setTextColor(colors.red)
  94. monitor.setBackgroundColor(colors.black)
  95. monitor.setCursorPos(19, 10)
  96. monitor.write("5:00")
  97.  
  98. while true do
  99.  
  100.   os.pullEvent("redstone")
  101.   if rs.getInput("left") then
  102.  
  103.     local timerID = os.startTimer(1)
  104.     while true do
  105.       local event = { os.pullEvent() }
  106.       if event[1] == "timer" and event[2] == timerID then
  107.         if not updateTime() then break end --# Stops when all functions have reached 0:00
  108.         printTime()
  109.         timerID = os.startTimer(1)
  110.       elseif event[1] == "redstone" and rs.getInput("bottom") and tonumber(timers[2][1]..timers[2][2]) > 0 then
  111.         break
  112.       end
  113.     end
  114.    
  115.   end
  116.  
  117. end
Advertisement
Add Comment
Please, Sign In to add comment