Advertisement
HPWebcamAble

Stupid little timer thing

Aug 29th, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.64 KB | None | 0 0
  1. --# Starting time for each timer
  2. --# You could add more, but you'd have to add a bit of code to 'printTime' to dipslay them
  3. local timers = {
  4.   {"10","00"},
  5.   {"5","00"}
  6. }
  7.  
  8. --# Returns tTime in a xx:xx format
  9. local function formatTime(tTime)
  10.   return tTime[1]..":"..tTimer[2]
  11. end
  12.  
  13. --# Decrements each timer
  14. local function updateTime()
  15.   local allDone = true
  16.   for i = 1, #timers do
  17.     if timers[i][2] == "00" then
  18.       if tonumber(timers[i][1]) > 0 then
  19.         timers[i][2] = "59"
  20.         timers[i][1] = tostring(tonumber(timers[i][1]) - 1)
  21.         allDone = false
  22.       end
  23.     elseif tonumber(timers[i][2]) < 10 then
  24.       timers[i][2] = "0"..(tonumber(timers[i][2]) - 1)
  25.       allDone = false
  26.     else
  27.       timers[i][2] = tostring(tonumber(timers[i][1]) - 1)
  28.       allDone = false
  29.     end
  30.   end
  31.   return not allDone --# Returns 'true' if at least one timer counted down, false if all timers have finished
  32. end
  33.  
  34. --# Prints time to monitor
  35. local function printTime()
  36.   monitor.setCursorPos(1,6)
  37.   monitor.clearLine()
  38.   monitor.write("         T MINUS "..formatTime(timer[1]).."MINUTES")
  39.   monitor.setCursorPos(1,10)
  40.   monitor.clearLine()
  41.   monitor.write("         T MINUS "..formatTime(timer[2]).."MINUTES")
  42. end
  43.  
  44.  
  45. --# Insert this in place of '-- TIMER HERE <<<<>>>>'
  46. --# The above functions just need to be somewhere above this, in the same program
  47. local timerID = os.startTimer(1)
  48. while true do
  49.   local event = { os.pullEvent() }
  50.   if event[1] == "timer" and event[2] == timerID then
  51.     if not updateTime() then break end --# Stops when all functions have reached 0:00
  52.     printTime()
  53.     timerID = os.startTimer()
  54.   end
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement