Jump to content




Good Clock Program


12 replies to this topic

#1 KDJSAIUH

  • Members
  • 35 posts

Posted 07 September 2013 - 10:55 AM

Startup Code
shell.run("monitor","right","clock")

Clock Code
term.clear()
term.setCursorPos(1,1)
z=true
while true do
shell.run"time"
sleep(0.75)
term.setCursorPos(1,1)
term.clear()
end

this is a pretty good clock and it auto-updates its self when you log off and back on

#2 1lann

  • Members
  • 516 posts
  • LocationSeattle

Posted 07 September 2013 - 12:15 PM

Although (No offence) this program is practically completely useless, you could improve it by removing some of the clutter.
while true do term.clear() term.setCursorPos(1,1) shell.run("/rom/programs/time") sleep(0.75) end


#3 BigTwisty

  • Members
  • 106 posts

Posted 09 September 2013 - 08:30 AM

Also why is z even declared? I can't see where you use it.

#4 jay5476

  • Members
  • 289 posts

Posted 09 September 2013 - 09:49 AM

just a pointer, you could do
while true do
term.redirect(peripheral.wrap(side))
shell.run("clear")
shell.run("time")
sleep(1)
end
Edit: Why do you declare z and call term.clear and term.setCursorPos at the start why not move them to the top of your while loop

#5 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 09 September 2013 - 10:32 AM

Don't put a redirect in a while loop without balancing it with a restore.

#6 BigTwisty

  • Members
  • 106 posts

Posted 09 September 2013 - 12:31 PM

It WOULD be pretty funny to see someone trying to debug that one... "I've run term.restore() 500 times and it's STILL stuck on the monitor!"

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 09 September 2013 - 01:23 PM

View PostBigTwisty, on 09 September 2013 - 12:31 PM, said:

It WOULD be pretty funny to see someone trying to debug that one... "I've run term.restore() 500 times and it's STILL stuck on the monitor!"
There was a thread in AaP about that I little while ago that I helped with, multiple redirects but only one restore. One of my old programs also had that edge case where it would occasionally get stuck on a monitor too.

#8 sgt_twitchy

  • Members
  • 3 posts
  • LocationNew York

Posted 16 September 2013 - 10:06 PM

I wrote a pretty simple clock program free for anyone to use, please give comments on where i can reduce code.

local time = os.time()
local moniter = peripheral.wrap('side')
local function getTime()
   moniter.clear()
   moniter.setCursorPos(1, 1)
   moniter.write(textutils.formatTime(time, bTwentyFourHour))
   sleep(0.75)
end
while true do
   getTime()
end


#9 jay5476

  • Members
  • 289 posts

Posted 17 September 2013 - 01:15 AM

errr getTime is just writing the same thing over and over move time = os.time() into your function

#10 sgt_twitchy

  • Members
  • 3 posts
  • LocationNew York

Posted 17 September 2013 - 02:57 AM

so something like this?
local moniter = peripheral.wrap('side')
local function getTime()
   time = os.time()
   moniter.clear()
   moniter.setCursorPos(1, 1)
   moniter.write(textutils.formatTime(time, bTwentyFourHour))
   sleep(0.75)
end
while true do
   getTime()
end


#11 Shefla

  • Members
  • 30 posts

Posted 18 September 2013 - 11:00 AM

Hello guys, here is my attempt to code another clock program.
Please tell me what you think about since I'm kind of new to lua therefore any advice/criticism is welcome.

function showTime (display)
  display = display or term
  while true do
	local timer = os.startTimer(1)
	local event, id = os.pullEvent("timer")
	if id == timer then
	  display.clear()
	  display.setCursorPos(1, 1)
	  display.write(textutils.formatTime(os.time(), true))
	end
  end
end


#12 6677

  • Members
  • 197 posts
  • LocationCambridgeshire, England

Posted 24 December 2013 - 12:42 PM

I know this is an old thread but this was my attempt at a clock for a single monitor, no terminal redirects either.</p>
side = ...
print(side)
mon = peripheral.wrap(side)
mon.clear()
while true do
  os.startTimer(1)
  local event = os.pullEvent("timer")
  mon.clear()
  mon.setCursorPos(1,3)
  mon.write(textutils.formatTime(os.time()),true)
end
I just saved that as clock, called it with &quot;clock top&quot; and it runs on the top monitor. Presumably would work with network attached monitor too


EDIT: Anyone else had issues with the comments suddenly deciding to be posted as plain text HTML?

Edited by 6677, 24 December 2013 - 03:33 PM.


#13 MrR4P70R

  • New Members
  • 1 posts

Posted 18 March 2014 - 11:58 PM

Like 6677, I know this is an old thread but I've made my own clock for my server spawn and I'm happy to share it with you :)

m = peripheral.wrap("side")
m.clear()
m.setTextScale(2)
m.setTextColor(colors.red)
function updateClock(m)
  while true do
	mcT = os.time()
	m.setCursorPos(1,5)
	m.clearLine()
	m.setCursorPos(6,5)
	m.write(textutils.formatTime(mcT))
	m.setCursorPos(1,2)
	m.clearLine()
	m.setCursorPos(3,2)
	m.write("Jour "..os.day())
	sleep(0.1)
  end
end
updateClock(m)

Just in case, you need to replace "side" at lign 1 by the side where your monitor is AND you need to have Advanced stuffs (Computer, Monitor)

PS: I use a 3x2 monitor but you can have a smaller one or a bigger one, just check for "text size" at lign 3, "text color" at lign 4 and "text placment" at lign 10 and 14 :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users