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

Simple countdown problem

Started by test, 03 November 2014 - 05:26 PM
test #1
Posted 03 November 2014 - 06:26 PM
Hey there!

I've some problems making a Countdown timer.


local mon = peripheral.wrap("top")
mon.clear()

local i = 1
local j = 20

repeat

  if i > 0 then
   if j > 0 then
	  j = j - 1
	else
	  j = 59
	  i = i - 1
	end
	else
   if j > 0 then
	  j = j - 1
	end
  
  local seconds = ""..j
  if j < 10 then
	seconds = "0"..j
	end

  end

  mon.setTextScale(5)
  mon.clear()
  mon.setCursorPos(2,1)
  mon.write(i..":"..j)

sleep(1)

until i == 0 and j == 0

But under the 10 seconds you get this:


But I want 00:07 ect and not 0:7

Last question if you can make a thing, so you can place a redstone signal from the back so you can reset it and start it when the redstone is powered.

Many thanks :D/>
KingofGamesYami #2
Posted 03 November 2014 - 08:32 PM
The problem is, lua treats 07 as 7. To fix this, give this your number:

local function double( n )
  return n >= 10 and n or "0" .. tostring( n )
end
After placing this in your code, you can use it like this

mon.write(double( i )..":"..double( j ))
Edited on 03 November 2014 - 07:33 PM
Dragon53535 #3
Posted 03 November 2014 - 09:44 PM
Yami's way works, however I prefer string.format :P/>

local function double( l )
  return string.format("%02d",l)
end


mon.write(double( i )..":"..double( j ))
in the string.format you see %02d which for the purposes of this says put a zero on the left if the next number isn't two digits long. And you can expand it if you want hundreds and such just change that 2 to a 3. Yami's version requires more fiddling if you want hundreds and such.
Edited on 03 November 2014 - 08:48 PM
Lyqyd #4
Posted 03 November 2014 - 10:13 PM
I don't really trust LuaJ's string.format. It's kinda broken in a few areas. If the above works, great. If not, this is probably the easiest. It takes a number or numeric string and returns a numeric string of the specified number of characters, properly padded with zeroes.


function pad(num, len)
  if tonumber(num) >= 0 then
    return string.sub(string.rep("0", len)..tostring(num), 0 - len)
  else
    return "-"..string.sub(string.rep("0", len - 1)..string.sub(tostring(num), 2), 1 - len)
  end
end
test #5
Posted 03 November 2014 - 10:14 PM
Final code:
Error: Countdown:30: attempt to call nil


local mon = peripheral.wrap("top")
mon.clear()

local i = 1
local j = 20

repeat

  if i > 0 then
   if j > 0 then
	  j = j - 1
	else
	  j = 59
	  i = i - 1
	end
	else
   if j > 0 then
	  j = j - 1
	end

  local function double( n )
	return n >= 10 and n or "0" .. tostring( n )
  end

end

  mon.setTextScale(5)
  mon.clear()
  mon.setCursorPos(2,1)
  mon.write(double( i )..":"..double( j ))

sleep(1)

until i == 0 and j == 0
Edited on 04 November 2014 - 05:20 AM
TheJebForge #6
Posted 04 November 2014 - 06:02 PM
There's the working code!

local mon = peripheral.wrap("top")
mon.clear()
--Setting variables
done = false
i = 1 --minutes
j = 20 --seconds
min = 0
sec = 0
while done == false do
	if i > 0 then
		if j > 0 then
			j = j - 1
		else
			j = 59
			i = i - 1
		end
	else
		if j > 0 then
			j = j - 1
		else
			done = true
		end
	end
	if j > 9 then
		sec = j
	else
		sec = "0"..j
	end
	min = i
	sleep(1)
	--Returning Results
	mon.setTextScale(5)
	mon.clear()
	mon.setCursorPos(2,1)
	mon.write(min..":"..sec)

end
Edited on 04 November 2014 - 05:02 PM
test #7
Posted 04 November 2014 - 08:33 PM
Thank you so much Thejenforge. Its working :D/>

[media]http://www.youtube.com/watch?v=1EUQsBces1I[/media]
Edited on 04 November 2014 - 09:56 PM
Bomb Bloke #8
Posted 04 November 2014 - 10:34 PM
Final code:
Error: Countdown:30: attempt to call nil

In the case of that code, you declared your double() function as being local to your repeat loop - meaning you couldn't access it from outside that loop.

http://lua-users.org/wiki/ScopeTutorial