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

Need To Create A Timer

Started by Arimus, 09 September 2013 - 08:47 AM
Arimus #1
Posted 09 September 2013 - 10:47 AM
Hello everyone,

I have been searching for some code that will suit my needs but as yet I haven't found anything so I thought I might post the question here.

Being fairly new to computercraft I was wondering if anyone could help me out in creating a timer. Basically there will be like a parkour the player has to take and we need to time the player's run and post it on a screen.

Looking at the available APIs I thought maybe I could use the os.startTimer thingy but I am not sure. Is it possible to create an accurate time with minutes, seconds, tenths of a second and hundreths of a second?

Thanks!
RoD #2
Posted 09 September 2013 - 11:00 AM
welcome to computercraft :D/>
A timer is really easy to do… I once created one, here's the code:
Spoiler

m = 0--this is the starting minute

function clear()--the clear function will make it easy to clear the screen
term.clear()
term.setCursorPos(1,1)
end

function clockS()--the main function
for i = 1,59 do--this will count to 59 and then restart
write(m..":")--write the minutes then ":"
write(i)--write the seconds
sleep(1)--sleeps for one second
if i == 59 then--if the senconds reach 59 it will do:
m = m + 1--add one number to the minute counter
clockM()--restart the timer
end
clear()
end
end
function clockM()--this function dont need to be here but i created it in the begining of the code and then i didnt take it but dont chnage anything in it
clear()
clockS()--goes to the main function
end
clockM()
So this is really simple and it will count the minutes after 60…so fell free to make it stop at 60 minutes if you want.
you can get the time by doing this:

print(i..":"..m)
Hope it helps
Sorry for my bad english and for the code up there(i created it when i didn't knew too much about programming)
Arimus #3
Posted 09 September 2013 - 01:35 PM
Thanks very much! I will see how I can implement this code. :-)
RoD #4
Posted 09 September 2013 - 04:00 PM
Thanks very much! I will see how I can implement this code. :-)
Np :)/> btw i changed some things in the code(added the tenths of a second, and when de clock reach 60 minutes it ends :D/> here is the new one:
Spoiler

m = 0--this is the starting minute
function clear()--the clear function will make it easy to clear the screen
term.clear()
term.setCursorPos(1,1)
end
function clockS()--the main function
for i = 0,59 do--this will count to 59 and then restart
for t = 0,10 do
write(m..":")--write the minutes then ":"
write(i..":"..t)--write the seconds
sleep(0.1)--sleeps for 1/10 of a second
if i == 59 then--if the seconds reach 59 it will do:
m = m + 1--add one number to the minute counter
clockM()--restart the timer
end
if m == 60 then
print("END")
return
end
clear()
end
end
end
function clockM()--this function dont need to be here but i created it in the begining of the code and then i didnt take it but dont chnage anything in it
clear()
clockS()--goes to the main function
end
clockM()

immibis #5
Posted 09 September 2013 - 05:25 PM
Just so you know, Rodmastercraft's code will crash with a stack overflow if you keep it running for about 2 hours and 8 minutes.
Lyqyd #6
Posted 09 September 2013 - 05:46 PM
Something more like this would be better:


local time = 0

while true do
  term.clear()
  term.setCursorPos(1,1)
  local minutes = math.floor(time / 60)
  local hours = math.floor(minutes / 60)
  term.write(hours..":"..minutes..":"..time % 60)
  time = time + 1
  sleep(1)
end

Leading zeroes are left as an exercise for the reader.
RoD #7
Posted 11 September 2013 - 03:43 PM
Just so you know, Rodmastercraft's code will crash with a stack overflow if you keep it running for about 2 hours and 8 minutes.
:/ oh i didn't mean to… But as i said the code is old :P/>