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

Countdown Timer

Started by Aggermor, 24 June 2013 - 12:27 AM
Aggermor #1
Posted 24 June 2013 - 02:27 AM
Hello everyone, I am new at coding Lua and I am so jellous whenever I see something really well done with ComputerCraft. I am pretty bad at this. Anyway, I tried to make a countdown timer program on my own. This is maybe my 10th attempt at making a program and I have only made about 3 successfully. I was wondering if someone could help me get this working so that I could learn and fix the mistakes.

Here is a link to my code:
http://pastebin.com/DkSV7Wge

Also, is it possible to center the text on the monitor? If so, I will need some help on that too, and thanks very much!

EDIT: The error is, "test:20: attempt to compare string with number expected, got string"
Edited on 24 June 2013 - 11:14 AM
Lyqyd #2
Posted 24 June 2013 - 01:14 PM
Split into new topic.
JohnnyX #3
Posted 24 June 2013 - 03:09 PM
The error is, "test:20: attempt to compare string with number expected, got string"

Yeah, the error happens because at line 18 you turn the variable second int oa string with the read() command (I think the proper command is io.read() ) and at line 20 you want to compare a string with a number.
You can center the monitor by using " mon.setCursorPos(x, y) " (without hte quotes).
Here is my timer. It's not functional because i used a function i think. But the gist is in there. here it is:

--Swan Station Timer
term.clear()
term.setCursorPos(1,1)
m = peripheral.wrap("front")
minutes = 1
seconds = 0

function Xtimer(seconds, minutes, monitor) --a timer function
monitor.clear() 
monitor.setCursorPos(3, 3)
  seconds = seconds - 1
  if (minutes == 0 and seconds == -1 ) then
  seconds = 0
  end
  if(seconds < 0) then
    seconds = 59
    minutes = minutes - 1
   elseif(seconds == 0) then
     seconds = tostring(00)
     end
     if(seconds < 10) then
     seconds = "0"..tostring(seconds)
     end
  monitor.write(minutes..":"..seconds)
seconds = tonumber(timer)
os.sleep(1)  

end

timer = os.startTimer(60)

print("Initializing timer...")
os.sleep(1)

while(true) do

  if (redstone.getInput("left") and minutes < 4) then
  print("Reseting...")
  end 
  if (minutes < 0 and seconds == 0) then
  write("System Failture")
  end

  Xtimer(seconds, minutes, m) -- calling the timer function  
end
Grim Reaper #4
Posted 24 June 2013 - 03:54 PM
Like JohnnyX said, when you call 'read' to get input from the user, you;ll be getting a string back regardless of what you typed in. Therefore, you could simply just call 'tonumber' on the returned value of 'read' to cast it to a number.

You could do it like this:

local second = tonumber (read())

As for centering the monitor, JohnnyX is right again. You'll need to account for how wide your monitor is and how long the string your printing is in order to center it properly.
You could simply divide the monitor width in half and subtract the width of the string you're printing cut in half in order to calculate the correct x position for the string to be centered.
As for the y coordinate, any y position that fits on the monitor should be fine.


local monitor = peripheral.wrap ("top")

local monitorWidth, monitorHeight = monitor.getSize()
local _string  = "Test string."

local centerStringX = (monitorWidth / 2) - (_string:len() / 2)
local centerStringY = 1

monitor.setCursorPos (centerStringX, centerStringY)
monior.write (_string)