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

Keycard(Floppy file) Reader Help!

Started by rawritsdan, 18 August 2012 - 04:37 PM
rawritsdan #1
Posted 18 August 2012 - 06:37 PM
Hey guys, just another thing im working on,I have been at it for a little while now but after fixing one error im encountering another.
Take a look at my code and see if there is any way I can use this to activate a redstone output using a file on my floppy. The file and data is there and the "Maintenance" function all work its just it keeps crashing after a few seconds, All ideas welcome.
Thanks, Dan :(/>/>


term.clear()
term.setCursorPos(1,1)
print "Card Reader Is Online"
function door()
redstone.setOutput("back",true)
sleep(5)
redstone.setOutput("back",false)
shell.run("startup")
end
function main()
term.clear()
term.setCursorPos(1,1)
print "Maintenance Mode"
print ""
print "1) Lock Reader"
print "2) Force Open"
input = read()
if input == "1" then
  while true do
   sleep(0)
   disk.eject("top")
   e, k = os.pullEvent("key")
  if k == 50 then
   break
  end
   end
   print "Card Reader Lock De-activated"
   read()
   shell.run("startup")
end
if input == "2" then
redstone.setOutput("back",true)
print "To Re-activate Type Password"
pass = read()
if pass == "fixme" then
redstone.setOutput("back",false)
shell.run("startup")
end
end
end
while true do
sleep(0)
if fs.exists("disk/cl") then
file = io.open("disk/cl", "r")
cle = file:read()
print "File Opened"
if cle == "2" then
disk.eject("top")
door()
else if cle == "3" then
disk.eject("top")
door()
else
sleep(0)
disk.eject("top")
shell.run("startup")
end
end
else
sleep(0)
shell.run("startup")
end
end
BigSHinyToys #2
Posted 18 August 2012 - 09:10 PM
1 ) if you have a question Ask a Pro by putting it in the Ask a Pro section
2 ) don't shell.run("startup") this is bad programing there are much better ways to make a loop
3 ) I think your error is cause by stack over flow from all those shell.run() 's
4 ) post the error code

and … that about covers it

Now that said I will help you Give me time to look over / test your code.
[EDIT]
your code was not as bad a i thought the main function is never called thought ?
I have made some modifications and commented out the shell.runs it runs in the main loop now this is better It you absolutly have to use os.reboot() instead of shell.run()
I have made the code in the loop only run when a disk is inserted with the line local event,arg1,arg2 = os.pullEvent("disk")
Here is your code


function door()
    redstone.setOutput("back",true)
    sleep(5)
    redstone.setOutput("back",false)
    -- shell.run("startup")
end
function main()
    term.clear()
    term.setCursorPos(1,1)
    print "Maintenance Mode"
    print ""
    print "1) Lock Reader"
    print "2) Force Open"
    input = read()
    if input == "1" then
	    while true do
		    sleep(0)
		    disk.eject("top")
		    e, k = os.pullEvent("key")
		    if k == 50 then
			    break
		    end
	    end
    print "Card Reader Lock De-activated"
    read()
    shell.run("startup")
    end
    if input == "2" then
	    redstone.setOutput("back",true)
	    print "To Re-activate Type Password"
	    pass = read()
	    if pass == "fixme" then
		    redstone.setOutput("back",false)
		    shell.run("startup")
	    end
    end
end
while true do
    term.clear()
    term.setCursorPos(1,1)
    print "Card Reader Is Online"
    local event,arg1,arg2 = os.pullEvent("disk")
    if fs.exists("disk/cl") then
	    file = io.open("disk/cl", "r")
	    cle = file:read()
	    print "File Opened"
	    if cle == "2" then
		    disk.eject("top")
		    door()
	    elseif cle == "3" then
		    disk.eject("top")
		    door()
	    else
		    sleep(0)
		    disk.eject("top")
		    -- shell.run("startup")
	    end
    else
    -- sleep(0)
    -- shell.run("startup")
    end
end
[/EDIT]
rawritsdan #3
Posted 20 August 2012 - 05:25 PM
1 ) if you have a question Ask a Pro by putting it in the Ask a Pro section
2 ) don't shell.run("startup") this is bad programing there are much better ways to make a loop
3 ) I think your error is cause by stack over flow from all those shell.run() 's
4 ) post the error code

and … that about covers it

Now that said I will help you Give me time to look over / test your code.
[EDIT]
your code was not as bad a i thought the main function is never called thought ?
I have made some modifications and commented out the shell.runs it runs in the main loop now this is better It you absolutly have to use os.reboot() instead of shell.run()
I have made the code in the loop only run when a disk is inserted with the line local event,arg1,arg2 = os.pullEvent("disk")
Here is your code


function door()
	redstone.setOutput("back",true)
	sleep(5)
	redstone.setOutput("back",false)
	-- shell.run("startup")
end
function main()
	term.clear()
	term.setCursorPos(1,1)
	print "Maintenance Mode"
	print ""
	print "1) Lock Reader"
	print "2) Force Open"
	input = read()
	if input == "1" then
		while true do
			sleep(0)
			disk.eject("top")
			e, k = os.pullEvent("key")
			if k == 50 then
				break
			end
		end
	print "Card Reader Lock De-activated"
	read()
	shell.run("startup")
	end
	if input == "2" then
		redstone.setOutput("back",true)
		print "To Re-activate Type Password"
		pass = read()
		if pass == "fixme" then
			redstone.setOutput("back",false)
			shell.run("startup")
		end
	end
end
while true do
	term.clear()
	term.setCursorPos(1,1)
	print "Card Reader Is Online"
	local event,arg1,arg2 = os.pullEvent("disk")
	if fs.exists("disk/cl") then
		file = io.open("disk/cl", "r")
		cle = file:read()
		print "File Opened"
		if cle == "2" then
			disk.eject("top")
			door()
		elseif cle == "3" then
			disk.eject("top")
			door()
		else
			sleep(0)
			disk.eject("top")
			-- shell.run("startup")
		end
	else
	-- sleep(0)
	-- shell.run("startup")
	end
end
[/EDIT]

Hey thanks man, im sorry I forgot to save the code that called main(). I didn't really understand loops so I tried to inch round them with this, again thanks and you have no idea how much this has improved my map.
dan :D/>/>
rawritsdan #4
Posted 20 August 2012 - 06:00 PM
Code works great! But where and how would I place a key event to call main()?
I have tried to add it in various places but it just stops the disk loop from repeating, I want the event to be after it prints "Card Reader Online" and to be triggered by m(50). Anything I could do?
thanks, dan :D/>/>
BigSHinyToys #5
Posted 20 August 2012 - 06:06 PM
I can't test this right now but this should work If not i will be back tomorrow to fix it i have to sleep now.

while true do
	    term.clear()
	    term.setCursorPos(1,1)
	    print "Card Reader Is Online"
	    local event,arg1,arg2 = os.pullEvent()
    if event == "disk" then
	    if fs.exists("disk/cl") then
			    file = io.open("disk/cl", "r")
			    cle = file:read()
			    print "File Opened"
			    if cle == "2" then
					    disk.eject("top")
					    door()
			    elseif cle == "3" then
					    disk.eject("top")
					    door()
			    else
					    sleep(0)
					    disk.eject("top")
					    -- shell.run("startup")
			    end
	    else
	    -- sleep(0)
	    -- shell.run("startup")
	    end
    elseif event == "key" then
	    if arg1 == 14 then -- backspace
	    main()
	   end
    end
end
rawritsdan #6
Posted 24 August 2012 - 03:03 PM
Thanks again BigSHinyToys! It works perfectly for me, Your understanding of LUA is so much greater than mine. :D/>/>
Dan :P/>/>