Jump to content




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


  • You cannot reply to this topic
2 replies to this topic

#1 onContentStop

  • Members
  • 6 posts

Posted 16 June 2014 - 01: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


#2 CCJJSax

  • Members
  • 262 posts

Posted 16 June 2014 - 06: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 by CCJJSax, 16 June 2014 - 06:06 AM.


#3 onContentStop

  • Members
  • 6 posts

Posted 18 June 2014 - 02:13 AM

OK, that is a much simpler fix than I though would be needed. Thanks for your help CCJJSax!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users