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

[Request] Making a simple "Count Up Timer"?

Started by Denisowator, 21 August 2014 - 03:58 PM
Denisowator #1
Posted 21 August 2014 - 05:58 PM
For those who don't know, a count up timer is the oposite of a countdown timer which is just a certain number going down until it hits 0.

So basically, I want it to look like this "00:00:00" and I want it to be hours:minutes:seconds. I want the numbers to go up to 99:99:99, or even for ever if that's possible.

Programing isn't really my thing and I don't want to spend months learning a single programming language, if the only thing I'm gonna be using it for is minecraft.

Thanks in advance. :)/>
Cranium #2
Posted 21 August 2014 - 06:48 PM

for hours = 0,99 do
  for minutes = 0,59 do
	for seconds = 0,59 do
	  term.setCursorPos(1,1)
	  term.write((hours < 10 and "0"..hours or hours)..":"..(minutes < 10 and "0"..minutes or minutes)..":"..(seconds < 10 and "0"..seconds or seconds))
	  sleep(1)
	end
  end
end
To be honest, I didn't think I would work on this as much as I had. But I had fun with it. Since minutes and seconds can only be at maximum 59 before the counter resets to 0, the only 99 you'd have is the hours, which you can simply just swap out the 24 in the code for a 99. changed
Although, to be more honest, I didn't test this code, and the ternary operators might be off, but it should work. I just don't have an emulator available right now.

Also, I'm moving this to the General section, since it's more of a request.
Edited on 21 August 2014 - 06:59 PM
Dragon53535 #3
Posted 21 August 2014 - 07:14 PM

local hour = 00
local min = 00
repeat
  for sec = 0, 59 do
	term.clear()
	term.setCursorPos(1,1)
	print(hour..":"..min..":"..sec)
	sleep(1)
  end
  min = min + 1
  if min == 60 then
	hour = hour + 1
	min = 0
  end
until hour == 100

This will print the time every second keeping it the only thing on the screen. It starts at 0:0:0 and It keeps going until it reaches 99:59:59.

Edit: Cranium, you've got it to where the hours starts at one.

for hours = 1,24 do
should be

for hours = 0,24 do

Oh and you don't have the screen clearing, just it setting to the first line :P/>
Edited on 21 August 2014 - 05:17 PM
Denisowator #4
Posted 21 August 2014 - 07:18 PM
Unfortunatly, it's not working.

I'm getting an error message that says: attempt to index global 'term' (a nil value)
Lyqyd #5
Posted 21 August 2014 - 07:53 PM
Did you try running the code in ComputerCraft, or in a different Lua interpreter?
Denisowator #6
Posted 21 August 2014 - 07:59 PM
I tried to run it in OpenComputers.

And before you say anything, I did post this exact same question on the OpenComputers forum but I eventually got tried of waiting and thought to post it here aswell since Computer Craft also uses Lua.

P.S. I am reloading the OpenComputers forum page constantly all the time, and still no replies.
Cranium #7
Posted 21 August 2014 - 08:57 PM
Edit: Cranium, you've got it to where the hours starts at one.

for hours = 1,24 do
should be

for hours = 0,24 do

Oh and you don't have the screen clearing, just it setting to the first line :P/>
Actually it doesn't need to clear anything. Since it overwrites the characters.

I tried to run it in OpenComputers.

And before you say anything, I did post this exact same question on the OpenComputers forum but I eventually got tried of waiting and thought to post it here aswell since Computer Craft also uses Lua.

P.S. I am reloading the OpenComputers forum page constantly all the time, and still no replies.
I'm not sure why you think running it on OpenComputers would work with ComputerCraft APIs. The error you're getting means that there is no term api in OpenComputers. If you have questions for them, you might just be better off waiting for their response.
Denisowator #8
Posted 21 August 2014 - 09:16 PM
Thank you guys for all your responses. :)/>

I really appreciate your help and I must admit, I feel pretty stupid. I should have gessed that the coding wouldn't work with another mod's API… especially when I remind myself what API actually stands for. :)/>

Thank you guys again and have a great day! :)/>
Edited on 21 August 2014 - 07:16 PM
KingofGamesYami #9
Posted 21 August 2014 - 11:33 PM
Here's a link to the docs of the OC Term API: http://ocdoc.cil.li/api:term
Cranium #10
Posted 21 August 2014 - 11:51 PM
So based on the docs, you could actually use the code I posted before, but instead of term.setCursorPos(1,1), you'd use term.setCursor(1,1). Other than that, it seems the rest of the functions used would work the same way.
Lyqyd #11
Posted 22 August 2014 - 01:08 AM
I think you have to specifically require() the API though. I don't really use OC, so I can't be of further help.