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

Displaying a countdown timer

Started by drakansteal, 01 April 2016 - 03:16 AM
drakansteal #1
Posted 01 April 2016 - 05:16 AM
First off I'll start by saying it's beyond funny how bad I am at coding, secondly, I've searched for a solution for an hour or so and can't find one that quite fits what I need so here's my question.

I have set up a redstone system that will power on some engines for a length of time dictated by the numeric value I've input. But, I want said numeric value to be displayed on a 2 x 2 monitor (left of the computer) ticking down, second after second. I'm not bothered about second/minute format (unless it won't be too much bother to add that in). Here's my code so far (don't judge exactly how sloppy it is):

while true do
term.clear()
term.setCursorPos(1,1)
print("Turn the engines on? Y/N")
input = read()
  if input == "Y" then
    print("How long for? (Seconds)")
    sleep(1)
    redstone.setOutput("back",true)
    local s = tonumber(read())
      local mon = peripheral.wrap("left")
      mon.clear()
    sleep(s)
    redstone.setOutput("back",false)
  elseif input == "debug" then
    os.exit()
  end
end
os.reboot()

The two monitor things I have written in there are prep to display the number.
Edited by
moTechPlz #2
Posted 01 April 2016 - 11:51 AM
Hi, you can do something simple like :

local start = 1000 -- how many seconds to count down
local monitor = peripheral.find( "monitor" )
monitor.setTextScale( 4.5 )
for count = start, 0, -1 do
	monitor.setCursorPos( 1, 2 )
	monitor.write( string.format( "%4d", count ) )
	sleep( 1 )
end
monitor.clear( )
Edited on 01 April 2016 - 09:59 AM
Unknowntissue #3
Posted 01 April 2016 - 08:26 PM
I'm no pro but I think I can help you too. I'm still a beginner at code just like you so i can explain how its done in the point of view of a newbie.

Personally I like it when people tell me what I did wrong in my code so I can correct my way of looking at it.

First thing I would change is move the following to the start of the program and out of the loop:

local mon = peripheral.wrap("left")
Technically this does work but it is good practice to keep variables that will never change out of the repeating loop. The reason for this is if you wanted to move the monitor to the top for some reason you don't have to go fishing for it deep in the code.( It also makes your code run smoother when you get into more intense stuff)

I don't know how much you know but I don't want to post a wall of text if I don't have to so I'll just post how I think the code should look. If you want me to explain more I would be happy to just let me know.

-- Startup
mon = peripheral.find("monitor") --# Will find Monitor no matter what side it is on
mon.setTextScale(4) --# toy with this number until you are happy
engine = "back" --# Just in case you ever need to change the side the engines are on you only need to change this
runProgram  = true --# while this is true the program will keep looping
-- The Program
while runProgram do
mon.clear()
print("Turn on engines? Y/N")
input = read()
if input == "Y" or input == "y" then --# works for both upper and lower y
  print("For how long?(seconds)")
  sec = tonumber(read()) --# how many seconds do you want to run
  redstone.setOutput(engine,true)
  for i = sec, 0 , -1 do --# Start at "sec", stop at 0, and count by -1
   mon.clear()
   mon.cursorPos(1,1)
   mon.write(tostring(i))
   os.sleep(1)
  end
  redstone.setOutput(engine,false)
elseif input == "N"  or input == "n" then
  runProgram = false
end
end
Edited on 01 April 2016 - 07:32 PM
drakansteal #4
Posted 01 April 2016 - 09:47 PM
Hi, you can do something simple like :

local start = 1000 -- how many seconds to count down
local monitor = peripheral.find( "monitor" )
monitor.setTextScale( 4.5 )
for count = start, 0, -1 do
	monitor.setCursorPos( 1, 2 )
	monitor.write( string.format( "%4d", count ) )
	sleep( 1 )
end
monitor.clear( )
Thankyou, not quite what I was looking for but I appreciate you taking the time to help me!
I'm no pro but I think I can help you too. I'm still a beginner at code just like you so i can explain how its done in the point of view of a newbie.

Personally I like it when people tell me what I did wrong in my code so I can correct my way of looking at it.

First thing I would change is move the following to the start of the program and out of the loop:

local mon = peripheral.wrap("left")
Technically this does work but it is good practice to keep variables that will never change out of the repeating loop. The reason for this is if you wanted to move the monitor to the top for some reason you don't have to go fishing for it deep in the code.( It also makes your code run smoother when you get into more intense stuff)

I don't know how much you know but I don't want to post a wall of text if I don't have to so I'll just post how I think the code should look. If you want me to explain more I would be happy to just let me know.

-- Startup
mon = peripheral.find("monitor") --# Will find Monitor no matter what side it is on
mon.setTextScale(4) --# toy with this number until you are happy
engine = "back" --# Just in case you ever need to change the side the engines are on you only need to change this
runProgram  = true --# while this is true the program will keep looping
-- The Program
while runProgram do
mon.clear()
print("Turn on engines? Y/N")
input = read()
if input == "Y" or input == "y" then --# works for both upper and lower y
  print("For how long?(seconds)")
  sec = tonumber(read()) --# how many seconds do you want to run
  redstone.setOutput(engine,true)
  for i = sec, 0 , -1 do --# Start at "sec", stop at 0, and count by -1
   mon.clear()
   mon.cursorPos(1,1)
   mon.write(tostring(i))
   os.sleep(1)
  end
  redstone.setOutput(engine,false)
elseif input == "N"  or input == "n" then
  runProgram = false
end
end

This worked great and was pretty much exactly what I was looking for, there were minimal errors in your code but easy to fix so it didn't cause major hassle :)/> Just fyi, you needed to make mon a local variable and mon.cursorPos should be .setCursorPos but the program now works fine so this is great. Thanks again - I finally understand the looping system.