Advertisement
DustinLuhmann

TestTime

Mar 4th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.15 KB | None | 0 0
  1. timeOfDay = "Day"
  2. os.loadAPI("ocs/apis/sensor")
  3. mySensor = sensor.wrap("top")
  4.  
  5. currentMinutes = -1
  6. currentSeconds = -1
  7.  
  8. function printTime(minutes, seconds)
  9.     currentSeconds = seconds
  10.     currentMinutes = minutes
  11.     min = tostring(minutes)
  12.     sec = tostring(seconds)
  13.     if string.len(sec) == 1 then
  14.         sec = "0"..sec
  15.     end
  16.     if timeOfDay == "Day" then
  17.         term.clear()
  18.         term.setCursorPos(1,1)
  19.         print(min..":"..sec.." till Night")
  20.     else
  21.         term.clear()
  22.         term.setCursorPos(1,1)
  23.         print(min..":"..sec.." till Day")
  24.     end
  25. end
  26.  
  27. function getTime()
  28.     returnedString = textutils.serialize(mySensor.getTargets())
  29.     angleIdx = string.find(returnedString, "Angle") + 8
  30.     timeRatio = tonumber(string.sub(string.sub(returnedString,angleIdx), 1, -5))
  31.     if timeRatio >= .5 then
  32.         timeRatio = timeRatio - .5
  33.         timeOfDay = "Night"
  34.     else
  35.         timeOfDay = "Day"
  36.     end
  37.     time = 10 - 2 * (10 * timeRatio)
  38.     minutes = math.floor(time)
  39.     secondsDecimal = time - minutes
  40.     seconds = math.floor(secondsDecimal * 60)
  41.     if (seconds ~= currentSeconds or minutes ~= currentMinutes) then
  42.         printTime(minutes, seconds)
  43.     end
  44. end
  45.  
  46. function run()
  47.     while true do
  48.         getTime()
  49.     end
  50. end
  51.  
  52. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement