Advertisement
HydrantHunter

General Purpose Countdown Timer with monitor support

Apr 19th, 2016
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.42 KB | None | 0 0
  1. print("")
  2. term.write("Enter hours: ")
  3. local nHours = tonumber(read())
  4. nHours = nHours or 0
  5. print("")
  6. term.write("Enter minutes: ")
  7. local nMinutes = tonumber(read())
  8. nMinutes = nMinutes or 0
  9. print("")
  10. term.write("Enter seconds: ")
  11. local nSeconds = tonumber(read())
  12. nSeconds = nSeconds or 0
  13. term.clear()
  14.  
  15. local totalTime = (nHours * 36000) + (nMinutes * 600) + (nSeconds * 10)
  16. if totalTime == 0 then print("") error("No time to count down\n", 0) end
  17. local hours, minutes, seconds, sHours, sMinutes, sSeconds, sTenths, countdownTimer, timer, _
  18. local mon = { peripheral.find("monitor") }
  19.  
  20. if mon then
  21.   local x
  22.   for i = 1, #mon do
  23.     mon[i].setTextColor(colors.white)
  24.     mon[i].setBackgroundColor(colors.black)
  25.     mon[i].clear()
  26.     mon[i].setTextScale(1)
  27.     x = mon[i].getSize()
  28.     if x == 7 then      --# width = 1
  29.       mon[i].setTextScale(0.5)
  30.     elseif x == 18 then --# width = 2
  31.       mon[i].setTextScale(nHours < 10 and 2 or 1)
  32.     elseif x == 29 then --# width = 3
  33.       mon[i].setTextScale(3)
  34.     elseif x == 39 then --# width = 4
  35.       mon[i].setTextScale(4)
  36.     elseif x >= 50 then --# width = 5+
  37.       mon[i].setTextScale(5)
  38.     end
  39.   end
  40. end
  41.  
  42. for i = totalTime, 0, -1 do
  43.   hours = totalTime > 35999 and math.floor(totalTime / 36000) or 0
  44.   sHours = tostring(hours)
  45.   minutes = totalTime > 599 and math.floor((totalTime - (hours * 36000)) / 600) or 0
  46.   sMinutes = minutes > 9 and tostring(minutes) or "0" .. tostring(minutes)
  47.   seconds = totalTime > 9 and math.floor((totalTime - ((hours * 36000) + (minutes * 600))) / 10) or 0
  48.   sSeconds = seconds > 9 and tostring(seconds) or "0" .. tostring(seconds)
  49.   sTenths = tostring(math.floor(totalTime - ((hours * 36000) + (minutes * 600) + (seconds * 10))))
  50.   term.setCursorPos(1, 1)
  51.   if totalTime == 0 and term.isColor() then term.setTextColor(colors.red) end
  52.   term.write(sHours .. ":" .. sMinutes .. ":" .. sSeconds .. ":" .. sTenths .. " ")
  53.   if mon then
  54.     for i = 1, #mon do
  55.       mon[i].setCursorPos(1, 1)
  56.       if totalTime == 0 and mon[i].isColor() then mon[i].setTextColor(colors.red) end
  57.       mon[i].write(sHours .. ":" .. sMinutes .. ":" .. sSeconds .. ":" .. sTenths .. " ")
  58.     end
  59.   end
  60.   if totalTime > 0 then
  61.     countdownTimer = os.startTimer(0.1)
  62.     repeat
  63.       _, timer = os.pullEvent("timer")
  64.       if timer == countdownTimer then
  65.         totalTime = totalTime - 1
  66.       end
  67.     until timer == countdownTimer
  68.   end
  69. end
  70. print("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement