Jump to content




Simple countdown problem


7 replies to this topic

#1 test

  • Members
  • 3 posts

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:
Posted Image

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

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

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 by KingofGamesYami, 03 November 2014 - 08:33 PM.


#3 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

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 by Dragon53535, 03 November 2014 - 09:48 PM.


#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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


#5 test

  • Members
  • 3 posts

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 by dcw_damon, 04 November 2014 - 06:20 AM.


#6 TheJebForge

  • Members
  • 128 posts

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 by Thejebforge, 04 November 2014 - 06:02 PM.


#7 test

  • Members
  • 3 posts

Posted 04 November 2014 - 08:33 PM

Thank you so much Thejenforge. Its working :D


Edited by dcw_damon, 04 November 2014 - 10:56 PM.


#8 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 04 November 2014 - 10:34 PM

View Postdcw_damon, on 03 November 2014 - 10:14 PM, said:

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...i/ScopeTutorial





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users