ravneravn

day night timer

Nov 14th, 2013
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --time = "night"
  2. mon = peripheral.wrap("back")
  3.  
  4. function setColor(text, background)
  5. term.setTextColor(text)
  6. term.setBackgroundColor(background)
  7. end
  8.  
  9. function drawImage(image)
  10. myImage = paintutils.loadImage(image)
  11. paintutils.drawImage(myImage, 1, 1)
  12. end
  13.  
  14. function writeFile()
  15. handle = fs.open("dayOrNight", "w")
  16. handle.write(time)
  17. handle.close()
  18. end
  19.  
  20. function loadFile()
  21. handle = fs.open("dayOrNight", "r")
  22. time = handle.readLine()
  23. handle.close()
  24. end
  25.  
  26. function mWrite(message, cX, cY, background, text)
  27. mon.setBackgroundColor(background)
  28. if text == nil then mon.setTextColor(colors.black)
  29. else mon.setTextColor(text)
  30. end
  31. mon.setCursorPos(cX, cY)
  32. mon.write(message)
  33. mon.setBackgroundColor(colors.black)
  34. end
  35.  
  36. function testClick()
  37. event, param1, x, y = os.pullEvent("monitor_touch")
  38. print("x: "..x)
  39. print("y; "..y)
  40. end
  41.  
  42. function clock()
  43. clockTime = {}
  44. for i = 1, 24 do
  45. clockTime[i] = os.setAlarm(i)
  46. end
  47. end
  48.  
  49. function writeTime()
  50. floorTime = math.floor(os.time())
  51. if floorTime > 9 then currTime = floorTime..":00"
  52. else currTime = "0"..floorTime..":00"
  53. end
  54. if time == "night" then
  55. mWrite(currTime, 13, 1, colors.black, colors.white)
  56. elseif time == "day" then
  57. mWrite(currTime, 13, 1, colors.lightBlue, colors.white)
  58. end
  59. end
  60.  
  61.  
  62.  
  63.  
  64. function day()
  65. term.clear()
  66. time = "day"
  67. drawImage("day")
  68. writeTime()
  69. redstone.setOutput("left", false)
  70. signal = "off"
  71. end
  72.  
  73. function night()
  74. term.clear()
  75. time = "night"
  76. drawImage("night")
  77. writeTime()
  78. redstone.setOutput("left", true)
  79. signal = "on"
  80. end
  81.  
  82. function updateClock()
  83. currTime = os.time()
  84. term.clear()
  85. drawImage(time)
  86. writeTime()
  87. end
  88.  
  89. function determineTime()
  90. notInUse = os.time()
  91. if math.floor(os.time()) > 19 and math.floor(os.time()) < 6 or math.floor(os.time()) == 20 then night()
  92. elseif math.floor(os.time()) < 20 and math.floor(os.time()) > 5 or math.floor(os.time()) == 6 then day()
  93. end
  94. end
  95.  
  96.  
  97.  
  98. ---------------------------------
  99. print("determining time")
  100. determineTime()
  101. print(time)
  102. term.redirect(mon)
  103. determineTime()
  104.  
  105.  
  106.  
  107. ----------------------------------
  108.  
  109. while true do
  110. clock()
  111. sunset = os.setAlarm(20)
  112. sunrise = os.setAlarm(6)
  113.  
  114. for i = 1, 24 do
  115. event, arg, xpos, ypos = os.pullEvent()
  116. if event == "alarm" then
  117. determineTime()
  118. elseif event == "monitor_touch" and signal == "on" then
  119. -- day()
  120. redstone.setOutput("left", false)
  121. signal = "off"
  122. elseif event == "monitor_touch" and signal == "off" then
  123. -- night()
  124. redstone.setOutput("left", true)
  125. signal = "on"
  126. end
  127. end
  128. end
RAW Paste Data