Advertisement
incinirate

timer

May 20th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.05 KB | None | 0 0
  1. local mon = peripheral.find("monitor")
  2. mon.setTextScale(2)
  3.  
  4. local mW, mH = mon.getSize()
  5.  
  6. local args = {...}
  7. local mins = 0
  8. local seconds = 0
  9.  
  10. if #args == 0 then
  11.   printError("Syntax: timer [minutes] <seconds>")
  12.   return
  13. elseif #args == 1 then
  14.   seconds = tonumber(args[1])
  15. else
  16.   mins = tonumber(args[1])
  17.   seconds = tonumber(args[2])
  18. end
  19.  
  20. if seconds > 59 then
  21.   mins = mins + math.floor(seconds / 60)
  22.   seconds = seconds % 60
  23. end
  24.  
  25. seconds = seconds + 1
  26.  
  27. local startEpoch = os.epoch("utc") / 1000
  28. local lastFire = -1
  29. while mins > 0 or seconds > 0 do
  30.   local time = math.floor(os.epoch("utc") / 1000 - startEpoch)
  31.   if time > lastFire then
  32.     lastFire = time
  33.  
  34.     seconds = seconds - 1
  35.     if seconds == -1 then
  36.       seconds = 59
  37.       mins = mins - 1
  38.     end
  39.  
  40.    
  41.     mon.clear()
  42.  
  43.     local str = mins .. ":" .. string.format("%1.2d", seconds)
  44.     mon.setCursorPos((mW - #str) / 2, mH / 2)
  45.     mon.write(str)
  46.   end
  47.  
  48.   sleep(0.25)
  49. end
  50.  
  51. mon.clear()
  52. local str = "Done!"
  53. mon.setCursorPos((mW - #str) / 2, mH / 2)
  54. mon.write(str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement