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

Time Lock (Countdown Timer)

Started by Computer Geek, 28 August 2014 - 01:50 AM
Computer Geek #1
Posted 28 August 2014 - 03:50 AM
Ok hello there

I have been attempting to program a time lock for my vault in minecraft. I can barely program a password protected door, actually let me restate that: I CAN'T! I want this program to first write <Insert Keycards> on 3 monitors connect to each other. Then the computer receives a redstone pulse from my AND gate which is hooked up to 2 mag-card readers (Immibis Core mod). Then after this pulse the clock starts to countdown from 3:00 to 0:00(all displayed on a monitor centered on the screen) then outputs a redstone pulse from the back of the computer to open the vault. I want there to be a ":" separating the minutes from the seconds which complicates things a lot. And of course that to be on a monitor I wanted to also include a restart of the time lock triggered by a redstone pulse and then restart the program. The error is about an end supposed the while statement before i start any of the else if statements, but the problem is that I want this loop to include all the else if statements. It is a bit repetitive. Here is what I came up with:



local monitor = peripheral.wrap("bottom")
monitor.setTextScale(1.5)
monitor.setCursorPos(2,2)
monitor.write("<Insert Keycards>")

os.pullEvent("redstone")

monitor.clear()
monitor.setTextScale(1.5)
monitor.setCursorPos(1,2)
monitor.write("3:00")

m = 2 – minutes
s = 59 – seconds

while m ~= 0 and s ~= 0 do

elseif s == 0 then
sleep(1) – 1 second pause
m = m-1
s = 59
monitor.clear()
monitor.setTextScale(1.5)
monitor.setCursorPos(1,2)
monitor.write(m,":",s)

elseif s < 10 then
sleep(1)
s = s - 1
monitor.clear()
monitor.setTextScale(1.5)
monitor.setCursorPos(1,2)
monitor.write(m,":0",s)

elseif s < 60 then
sleep(1)
s = s - 1
monitor.clear()
monitor.setTextScale(1.5)
monitor.setCursorPos(1,2)
end


monitor.clear()
monitor.setTextScale(1.5)
monitor.setCursorPos(1,2)
monitor.write("0:00")
sleep(3)
rs.setOutput("back, true")
sleep(20)
rs,setOutput("back, false")
os.reboot() – end of the program



P.S. - I tried to gather as much information on how to make a countdown timer and this is what I come up with
Dragon53535 #2
Posted 28 August 2014 - 04:43 AM
Alright so basically you're creating a counter right? well you can do

for a = 59, 0, -1 do
  print(a)
  sleep(1)
end
This will count from 59 until it hits zero by subtracting one every time it loops. The syntax of the for loop is
for [starting number],[ending number],[increment] do
Now there are other things a for loop can do, however that's the basis of this one and you can even use input from somewhere else by doing

local s = read()
local n = tonumber(s)
for a = n, 0, -1 do
  print(a)
  sleep(1)
end
Which will read what someone inputs, and then start on that number. If you need minutes and seconds, you can easily stack the for loops like so.

for m = 2, 0, -1 do
  for s = 59, 0, -1 do
	print(m..s)
	sleep(1)
  end
end
Which will after every 60 seconds, lower down the minutes, now i understand that you need it to show it with two digits such as 01 or 23 which is actually easy and was shown on an ask a pro earlier(it got moved to general for program request) asking for a count UP timer.

for minutes = 2, 0, -1 do
  for seconds = 59, 0, -1 do
	print((minutes < 10 and "0"..minutes or minutes)..":"..(seconds < 10 and "0"..seconds or seconds))
	sleep(1)
  end
end
This would print out 02:59 for the first time and then it would go 02:09 when it went down that far until it hit 02:00 and then it would go 01:59 and so on and so forth.
Credit to Cranium for this.
The beauty of this is that when minutes is 0 and seconds is 0 then it stops and leaves both loops.

Also i don't know if you need to keep setting the text size on monitors after clearing them, i believe they keep that setting.
Edited on 28 August 2014 - 02:50 AM
Computer Geek #3
Posted 01 September 2014 - 05:48 PM
Ok so using your help I came up with this program for the countdown time lock



local monitor = peripheral.wrap ("bottom")
monitor.setTextScale(2)
monitor.setCursorPOS(2,2)
monitor.write("<Insert Keycards>")

os.pullEvent("redstone")

monitor.setTextScale(3.5)
monitor.setCursorPos(3,1)
monitor.write("3:00")


for minutes = 2, 0, -1 do
for seconds = 59, 0, -1 do
monitor.clear
monitor.write((minutes < 10 and "0"..minutes or minutes)..":"..(seconds < 10 and "0"..seconds or seconds))
end

end


monitor.clear
monitor.write("00:00")
sleep(3)
rs.setOutput("left, true")
sleep(10)
rs.setOutput("left, false")
os.reboot


There is an error that says it expects an "=" on line 16
I have no idea what is wrong. Please help!!!
Dragon53535 #4
Posted 01 September 2014 - 11:31 PM

monitor.clear
That's your error, same with

os.reboot
They are functions and as such need () such as

monitor.clear()
os.reboot()
if you don't have the () then it thinks it's a variable that you're trying to make and that's why it needs an =

ALSO

for minutes = 2, 0, -1 do
  for seconds = 59, 0, -1 do
	monitor.clear
	monitor.write((minutes < 10 and "0"..minutes or minutes)..":"..(seconds < 10 and "0"..seconds or seconds))
  end
end
is just going to loop through quickly and not for 3 minutes. you need to add a sleep(1) after the monitor.write so

for minutes = 2, 0, -1 do
  for seconds = 59, 0, -1 do
    monitor.clear()
    monitor.write((minutes < 10 and "0"..minutes or minutes)..":"..(seconds < 10 and "0"..seconds or seconds))
    sleep(1)
  end
end
This is what this section should be.
Edited on 01 September 2014 - 09:34 PM
Computer Geek #5
Posted 14 September 2014 - 03:06 AM
Thank you so much for that loop! It works perfectly for my situation! Ok so my final code for the program is this:


										 --Startup--
local monitor = peripheral.wrap("bottom")
monitor.clear()
monitor.setTextScale(1.5)
monitor.write("<Insert Keycards>")
os.pullEvent("redstone")   -- waits for the AND gate to activate after both cards are swiped
sleep(3)
monitor.setTextScale(4)
monitor.setCursorPos(2,1)
monitor.write("3:00")
sleep(1)

for minutes = 2, 0, -1 do
		for seconds = 59, 0, -1 do
				 monitor.clear()
				 monitor.setCusorPos(2,1)
				 monitor.write((minutes < 10 and "0"..minutes or  minutes)..":"..(seconds < 10 and "0"..seconds or seconds))
				 sleep(1)
		end
end

					  -- after about 3:00 have passed:
monitor.clear()
monitor.setCursorPos(2,1)
monitor.write("00:00")
rs.setOutput("left" , true)			  -- opens both the doors
sleep(10)
rs.setOutput("left", false)			  -- closes both the doors
os.reboot()

Works flawlessly, thanks so much!

I have included some pictures but i don't know how to upload them. :(/>
Dragon53535 #6
Posted 14 September 2014 - 03:16 PM
Try using Imgur and just linking
TheOddByte #7
Posted 14 September 2014 - 03:45 PM
I have included some pictures but i don't know how to upload them. :(/>
As Dragon said, use Imgur, then copy the BBcode link after you've uploaded a picture and paste that link.
By the way, saw that you started opening a code tag but never closed it, you can close it with