Difference between revisions of "Making a Password Protected Door"

From ComputerCraft Wiki
Jump to: navigation, search
(Improve code)
Line 1: Line 1:
  
This tutorial covers on how to make a computer output redstone current when the right password is typed in. The current is then used to trigger an iron door.
+
This tutorial covers on how to make a Computer output redstone current when the right password is typed in. The current is used to trigger an iron door on the left side of the Computer, you can change the side, of course.
  
  
 
__TOC__
 
__TOC__
= Easy tutorial with screenshots =
+
= How to make it =
 
+
Here's the code for it, I'll explain what everything does in the comments. (the --'s).
A password protected door is actually pretty easy, if you break it into steps.
+
In your Computer, type 'edit startup'. Then write this code into there. (You don't need the comments (the --'s) in the final code)
 
+
<code>
 
+
local side = "left" -- Change left to whatever side your door / redstone is on, E.G: left, right, front, back, bottom, top. Be sure to leave the "s around it, though
First, you need to craft a computer, and connect the back to an iron door with redstone. Like this:
+
local password = "bacon" -- Change bacon to what you want your password to be. Be sure to leave the "s around it, though
[[File:Tutorial1.png]]
+
local opentime = 5 -- Change 5 to how long (in seconds) you want the redstone current to be on. Don't put "s around it, though
[[File:cTutorial2.png]]
+
term.clear() -- Clears the screen
 
+
term.setCursorPos(1,1) -- Fixes the cursor position
Once you're done with the basics, open the computer and edit the "startup" file. (type in edit startup)
+
write("Password: ") -- Prints 'Password: ' to the screen
This will make it so the program will be executed when the computer boots.
+
local input = read("*") -- Makes the variable 'input' have the contents of what the user types in, the "*" part sensors out the password
 
+
if input == (password) then -- Checks if the user inputted the correct password
Once you access the startup file, enter in these two lines.
+
term.clear() -- Already explained up top
[[File:CTutorial3.png]]
+
term.setCursorPos(1,1)
 
+
print("Password correct!") -- Prints 'Password correct!' to the screen
The first line defines a function, so that we can repeat the program if a password is entered,
+
rs.setOutput(side,true) -- Output a redstone current to the side you specified
and the second line is assigning whatever the user types to the variable, "t",
+
sleep(opentime) -- Wait the amount of seconds you specifed, then..
So for example, if I typed "qwerty", it would be t = "qwerty". It also overrides anything else the user types in,
+
rs.setOutput(side,false) -- Stop outputting a redstone current
so if I typed in "programs", it would just be t = "programs", and not act as the programs command in the regular
+
os.reboot() -- Reboot the computer, reopening the lock
OS. This can very useful.
+
else -- Checks if the user didn't input the correct password
 
+
term.clear()
 
+
term.setCursorPos()
After you're done with the previous lines, enter in the following line:
+
print("Password incorrect!") -- Prints 'Password incorrect!' to the screen
[[File:CTutorial4.png]]
+
sleep(2) -- Waits 2 seconds
 
+
os.reboot() -- Reboot the computer, reopening the lock
This makes it so the program checks if the variable that we declared earlier, "t", equals "password". (You can change password to what you want the actual password to be.)
+
end
 
+
</code>
 
+
But now you'll need to put in what to do, depending on if the user typed in the right password.
+
This next set of lines will do that:
+
 
+
[[File:CTutorial5.png]]
+
 
+
Now, this maay seem complicated, but it's really simple. The four lines after the if line, determine what the computer will do if the password is correct. So if it's correct, It'll set the redstone output from it's back to true (on), and open the door. (this is the "redstone.setOutput("back", true)" command.) It'll then wait two seconds, and turn the redstone back off again. It'll then repeat the function, so you don't need to reboot the computer.
+
 
+
However, the last two lines dictate what the computer will do if the password is wrong, which is really just not opening the door, and repeating the function.
+
 
+
 
+
And now the finishing touches:
+
 
+
[[File:CTutorial6.png]]
+
 
+
This adds the "Ends" to the if which is inside the function, and the function. So we've declared our function.
+
Now to start it, we have the computer call the function after it declares it.
+
 
+
Well, you should have your own password protected door!
+
Feel free to change the script around, maybe add a few success/failure messages, etc. etc.
+
  
 
(NOTE: If the program fails somehow, Pressing and holding Ctrl + T will terminate it, and allow you to edit it like normally)
 
(NOTE: If the program fails somehow, Pressing and holding Ctrl + T will terminate it, and allow you to edit it like normally)
  
 
= Alternative code =
 
 
Use Ctrl + T to terminate the program. You could set it up as a [[Startup|startup script]] to make it run automatically at boot time.
 
<code>
 
local function pass()
 
    term.clear()
 
    term.setCursorPos(1, 1)
 
    textutils.slowWrite("Enter password: ")
 
    t = io.read()
 
   
 
    -- Replace 'password' by the one you want
 
    if t == "password" then
 
        textutils.slowWrite("Access granted!")
 
        redstone.setOutput("back", true)
 
        sleep(2)
 
        redstone.setOutput("back", false)
 
    else
 
        textutils.slowWrite("Access denied!")
 
        sleep(2)
 
    end
 
end
 
 
while true do
 
    pass()
 
end
 
</code>
 
 
= No Termination =
 
= No Termination =
  
 
If you don't want people holding CTRL+T and quitting your lock, use this code at the top of your program:
 
If you don't want people holding CTRL+T and quitting your lock, use this code at the top of your program:
<pre>function os.pullEvent() return os.pullEventRaw() end</pre>
+
<pre>os.pullEvent = os.pullEventRaw</pre>
  
  
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]

Revision as of 00:48, 15 April 2012

This tutorial covers on how to make a Computer output redstone current when the right password is typed in. The current is used to trigger an iron door on the left side of the Computer, you can change the side, of course.


How to make it

Here's the code for it, I'll explain what everything does in the comments. (the --'s). In your Computer, type 'edit startup'. Then write this code into there. (You don't need the comments (the --'s) in the final code) local side = "left" -- Change left to whatever side your door / redstone is on, E.G: left, right, front, back, bottom, top. Be sure to leave the "s around it, though local password = "bacon" -- Change bacon to what you want your password to be. Be sure to leave the "s around it, though local opentime = 5 -- Change 5 to how long (in seconds) you want the redstone current to be on. Don't put "s around it, though term.clear() -- Clears the screen term.setCursorPos(1,1) -- Fixes the cursor position write("Password: ") -- Prints 'Password: ' to the screen local input = read("*") -- Makes the variable 'input' have the contents of what the user types in, the "*" part sensors out the password if input == (password) then -- Checks if the user inputted the correct password

term.clear() -- Already explained up top
term.setCursorPos(1,1)
print("Password correct!") -- Prints 'Password correct!' to the screen
rs.setOutput(side,true) -- Output a redstone current to the side you specified
sleep(opentime) -- Wait the amount of seconds you specifed, then..
rs.setOutput(side,false) -- Stop outputting a redstone current
os.reboot() -- Reboot the computer, reopening the lock

else -- Checks if the user didn't input the correct password

term.clear()
term.setCursorPos()
print("Password incorrect!") -- Prints 'Password incorrect!' to the screen
sleep(2) -- Waits 2 seconds
os.reboot() -- Reboot the computer, reopening the lock

end

(NOTE: If the program fails somehow, Pressing and holding Ctrl + T will terminate it, and allow you to edit it like normally)

No Termination

If you don't want people holding CTRL+T and quitting your lock, use this code at the top of your program:

os.pullEvent = os.pullEventRaw