This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
onContentStop's profile picture

Time display code does not work but there is no error message

Started by onContentStop, 16 June 2014 - 12:40 AM
onContentStop #1
Posted 16 June 2014 - 02:40 AM
This time code I am trying to make is supposed to display the time of day (numerical and in words) along with the day number.
It should show up on a monitor like so:
It is MORNING
	7:00 AM
Day 1
However, upon running the code I see this:
Spoiler
Here is the code in its entirety:
local mon = peripheral.wrap("right") --Define monitor
if not mon then
  print("No monitor found! :(/>/>") --Monitor failsafe
end
while true do --Finds time and day every 5 seconds
  local t = os.time()
  local d = os.day()
  sleep(5)
end
local day = "" --Creates day, a string
if t > 0 and t < 11 then --midnight to 11 AM
  day = "MORNING"
elseif t > 11 and t < 13 then --11 AM to 1 PM
  day = "NOON"
elseif t > 13 and t < 18 then --1 PM to 6 PM
  day = "AFTERNOON"
else --any other time
  day = "NIGHT"
end
while true do
  print(t) --puts raw time in terminal for troubleshooting
  mon.clear()
  mon.setTextScale(4)
  mon.setCursorPos(1, 2)
  mon.write("It is " .. day)
  mon.setCursorPos(4, 3)
  mon.write(textutils.formatTime(t, false))
  mon.setCursorPos(1, 4)
  mon.write("Day " .. d)
  sleep(1)
end
CCJJSax #2
Posted 16 June 2014 - 07:05 AM
Your problem is that it never exits the first while loop.

so basically it was resetting the variable t and d every 5 seconds but never used them.


local mon = peripheral.wrap("left")
if not mon then
  error("no monitor found")
end
while true do
  local t = os.time()
  local d = os.day()
  local day
  if t > 0 and t < 11 then
	day = "morning"
  elseif t > 11 and t < 13 then
	day = "noon"
  elseif t > 13 and t < 18 then
	day = "afternoon"
  else
	day = "night"
  end
  -- this is where your writing should go
  mon.clear()
  mon.setCursorPos(1, 1)
  mon.write("It is "..day)
  mon.setCursorPos(1, 2)
  mon.write(textutils.formatTime(t, false))
  mon.setCursorPos(1, 3)
  mon.write("Day ".. d)
  sleep(5)
end
Edited on 16 June 2014 - 05:06 AM
onContentStop #3
Posted 18 June 2014 - 03:13 AM
OK, that is a much simpler fix than I though would be needed. Thanks for your help CCJJSax!