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

countdown timer (printed)

Started by Herr_Tipsy, 06 November 2015 - 01:15 PM
Herr_Tipsy #1
Posted 06 November 2015 - 02:15 PM
hello i'm making a doorlock which opens for 3 minutes and then closes which and during this 3 minutes i would like to print the remaining time counting down, it mostly works but instead of replacing the previous number it prints it on a line below it. how can i get it to count down on one line? and possibly port it to a screen next to the computer?

code:

os.pullEvent = os.pullEventRaw
print("Enter password")
password = io.read()
if password == "nuked" then
rs.setOutput("bottom", true)
rs.setOutput("back", true)
print("Accepted")
for i = 0, 180, 1 do
  print(i-1," seconds remaining")
  sleep(1)
  term.clear()
rs.setOutput("bottom", false)
rs.setOutput("back", false)
end
else
print("try again")
end
Bomb Bloke #2
Posted 06 November 2015 - 10:35 PM
You can use term.setCursorPos() (from the term API) to reposition the cursor. Eg:

for i = 0, 180, 1 do
	term.clear()
	term.setCursorPos(1, 1)
	term.write(i-1," seconds remaining")
	sleep(1)
end

The easiest way to use an external monitor is to wrap and then redirect to it. Eg:

local mon = peripheral.wrap("right")  -- Or whatever side the monitor is on.

local oldTerm = term.redirect(mon)

term.write("This will be written on the external monitor.")

term.redirect(oldTerm)

term.write("This will be written on the computer's own display.")