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

Countdown Timer - Clock Format

Started by dougdashwitz, 26 July 2012 - 11:48 PM
dougdashwitz #1
Posted 27 July 2012 - 01:48 AM
I've searched all over and im finding plenty of options for countdown timers that use "sleep" to perform the timer portion. Is it possible to create a program, that counts down in minute:second format?

i have yet to see it done in my travels. I'm trying to figure it out myself, but coding is rather … well … horrible. Any ideas, examples, or similar programs that i might be able to customize would be greatly appreciated.

if i wasnt clear enough in what i was looking for just tell me, i have a nasty habit of leaving out important information.
brett122798 #2
Posted 27 July 2012 - 02:00 AM
Sounds pretty easy to me, I'll whip up some code and edit this post when I got something that may work for you. :)/>/>
brett122798 #3
Posted 27 July 2012 - 02:28 AM
Decided to post again so you can see I came back. Here's what i came up with:


print("How much time?")
print("")
write("Minutes: ")
minutes = read()
print("")
write("Seconds: ")
seconds = read()
clocktime = minutes+seconds
repeat
if seconds == 0 then
minutes = minutes-1
seconds = 60 -- 60 because it's about to get counted down again
end
if minutes == -1 then
break
end
seconds = seconds-1
if seconds < 10 then
zero = 0
else
zero = ""
end
sleep(1)
print("Time Remaining: "..minutes..":"..zero..seconds)
until minutes == -1
sleep(0.2)
print("TIME IS UP!")
sleep(5)

Hope that's what you were looking for.
dougdashwitz #4
Posted 27 July 2012 - 02:43 AM
that Definately helps… this is what i got based off what you wrote and some of my own tinkering. i wanted the timer to display on a monitor as it was counting, and it works great… except for a decimal point at the end that i cant quite figure out the source of, i know its probably something simple…


term.clear()
term.setCursorPos(1,1)
print("How many Minutes?") --only need minutes
write("-> ")
minute = tonumber(read()) --input directly as number(avoid string)
mon = peripheral.wrap("top", true)
mon.clear()
local j = 0   --declare seconds variable
local i = minute   ---declare minutes variable
repeat
	if i > 0 then	--if there are minutes
		if j > 0 then  --and seconds
			j = j - 1	--subtract a second
		else
			j = 59	  --otherwise no minutes left and seconds are now 59 and counting
			i = i - 1
		end
	else
		if j > 0 then  --finish countdown of only seconds (no minutes remaining)
			j = j - 1
		end  
	end	  
sleep(1)
mon.setCursorPos(1,1)
mon.write(i)							   -- print minutes
mon.setCursorPos(2,1)			
mon.write(":")							-- print ":" seperator
mon.setCursorPos(3,1)
mon.write(j)							   -- print seconds (strangely shows up as "##.0")
until i == 0
rs.setOutput(left,true)               -- trigger redstone output
sleep(1)
rs.setOutput(left,false)              -- deactivate trigger
brett122798 #5
Posted 27 July 2012 - 02:53 AM
lol

I'm having a hard time understanding your code, maybe cause you're displaying on a monitor, it's a bit different? Anyway, I'm glad you got what you needed(for the most part..).
Illmatar #6
Posted 27 July 2012 - 03:00 AM
that Definately helps… this is what i got based off what you wrote and some of my own tinkering. i wanted the timer to display on a monitor as it was counting, and it works great… except for a decimal point at the end that i cant quite figure out the source of, i know its probably something simple…

I have a comment to add on to your code. from just looking at it I think it may be dropping the last 59 seconds if you experience this in game (I have not tested it) The reason I am thinking this is it is going until the minutes is 0 but not checking if the seconds is 0 also.

until i == 0
to

until i == 0 and j == 0
might fix it if you are experiencing this

I could be wrong still new to this myself.

Also if you are talking about an extra 0 at the end it may be from when the number was longer and the text not overwriting on top you could try adding something like


mon.clearLine()

After using the setCursorPos
dougdashwitz #7
Posted 27 July 2012 - 06:38 PM
Thanks clueless, adding the seconds variable fixed the stopping at 59 issue as you suspected. Still getting a strange number issue. the time reads funny, for 1 minute and 30 seconds, it reads 1:30.0

its adding a tenths place to the seconds column

also, i was wondering if anyone knows how to add the ability to interrupt the timer at any point, perhaps a pause, or reset?

like i said, i know enough to get by, but im not great at coding
dougdashwitz #8
Posted 27 July 2012 - 06:54 PM
i seem to remember something similar back in my vb.net days, is it possible that the number is now a double? and if so, can i convert it to a string or an integer to drop the decimal place?
dougdashwitz #9
Posted 27 July 2012 - 06:59 PM
sry for seemingly spamming my own thread, but i found that changing output to the monitor from a number to a string, fixes the decimal place… used this code:

mon.write(tostring(j))

however now i get an error with my redstone output line, have i formatted it wrong? i cant seem to find proper documentation on the rs section of this mod… (probably looking wrong :-P)

ignore that, forgot the " " around the "side"

i am still looking for a way to interrupt / pause / reset the timer while its running, so if anyone has any ideas….
Illmatar #10
Posted 28 July 2012 - 04:21 AM
the first thing that comes to mind to add in a pause thing is add a

parallel.waitForAny(function 1, function 2)
This makes it so if one finishes it will stop the other

With this though you will need to move the repeat part into a function and make a second function to watch for input

Something like (copying your last posted code so if it changed just put your new one in)

function Repeat()
	repeat
			if i > 0 then   --if there are minutes
					if j > 0 then  --and seconds
							j = j - 1	   --subtract a second
					else
							j = 59	--otherwise no minutes left and seconds are now 59 and counting
							i = i - 1
					end
			else
					if j > 0 then  --finish countdown of only seconds (no minutes remaining)
							j = j - 1
					end  
			end	  
	sleep(1)
	mon.setCursorPos(1,1)
	mon.write(i)													   -- print minutes
	mon.setCursorPos(2,1)				  
	mon.write(":")												  -- print ":" seperator
	mon.setCursorPos(3,1)
	mon.write(j)									 -- print seconds (strangely shows up as "##.0")
	until i == 0
	rs.setOutput(left,true)			   -- trigger redstone output
	sleep(1)
	rs.setOutput(left,false)			  -- deactivate trigger
end
Should work for the countdown part

and something like

function CheckPause()
	while true do
		userInput = read()
		if userInput == 'pause' then
			pause = true
		elseif userInput == 'reset' then
								-- Where you would want it to reset to
		end
	end
end
Should work for the checking function

Then adding in a pause variable to the until check or something along the lines of

until i == 0 and pause ~= true
Then when you want it to start again you call to the

parallel.waitForAny(Function 1, Function 2)
again to start so it can be paused later if needed

Hope this helps and sorry if I did too much of it for you xD trying to get all the practice I can.
dougdashwitz #11
Posted 28 July 2012 - 03:23 PM
WOW that helps alot! Just about perfect. now i just need to learn how to actually do that. LOL, i suck at coding, i'll manage.

one last question, about this whole setup.

does anyone know how to output to multiple monitors from one terminal?

originally this was going to be used for everything from traps to reactors, but now im thinking this would be a great addition to my "The Walls" server

i added a couple computers/monitors all over and have wireless redstone to activate them, but they all need someone to manually input the time to them. i have 6 terminals in the map and its a bit of a pain to have to fly to each and set the time then fly back to the main and hit the start button then fly to my start position…

if there is a way to have, like, slave monitors that just mirror the output of the main terminal, or even slaved terminals that i could use to output to a monitor to get the same result, that would be great.
dougdashwitz #12
Posted 31 July 2012 - 12:31 AM
no ideas? i really despise the current way of going to all 6 computers and launching the program, entering the time, and triggering with wireless redstone…

there has got to be an easier way with rednet or something else im missing….hopefully?
Cranium #13
Posted 01 August 2012 - 09:40 PM
If you have all of the other computers using a startup program waiting for a broadcast, you can add a broadcast to each program you need mirrored, and then when it receives those messages, then it would start them. ex:

rednet.open("side")
id, msg, dist = rednet.receive()
function runProgram()
  if msg == "program1" then
    --program code number one here
  elseif msg == "program2" then
    --program code number two here
  end
end
You can use this function at the top pf all of your "slave" computers, and input the code for all of your programs into each elseif statement. Or you can show of your Badassery skills, and have shell.run() commands to start the programs on them at each receipt of messages. Just keep in mind that if you do the latter, the timing may be slightly off.