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

[lua][question]Need help adding some code

Started by kjetilhansen, 20 December 2012 - 01:08 PM
kjetilhansen #1
Posted 20 December 2012 - 02:08 PM
Hello!

I have copyed a code i found on youtube, and I have added some of my own. No I would like to add a feature that flashes.

What my code does:
Asks for password, right password open door, wrong password 2 times starts an alarm, and asks for an administrator password.

I would like to add one more feature that I have spent hours trying to do…

After the alarm goes off it asks for administrator password, if you enter this wrong I would like rs.setOutput left to flash whit 1 seconds between flashes. This will turn on and off untill the right administratorpassword is entered correct..

Can sombody help me with this?

This is my code so far: (I may misspell somthing since I dont copy paste)


os.pullEvent = os.pullEventRaw
tectutils.slowPrint("Radioactive hazard area, atuhorized personnel only")
sleep(1)
print ("Please log in; ")
sleep(1)
write("x")
password = io.read()

if password == "passord" then
print ("Authorizing, please stand by")
textutils.slowprint("..............................")
sleep(1)
print ("Welcome to Black Mesa nukelear facility")
rs.setOutput ("right" , true )
sleep(4)
rs.setOutput ("right" , false)
else
print ("Authorizing, please stand by")
textutil.slowPrint("..................................")
sleep(1)
print ("Wrong password, 1 try left. ")
sleep(1)
print ("Please input correct password")
write("x")
password = io.read()
if password = "passord" then
rs.setOutput ("right" , true )
sleep(4)
rs.setOutput ("right" , false)
else
print ("Authorizing, please stand by")
textutils.slowPrint("................................")
sleep(1)
print ("Wrong password")
sleep(1)
textutils.slowPrint("Hazard area breach - Activating alarm")

repeat
rs.setOutput ("top" , true )
sleep(1)
print ("Failsafe mode activated")
textutils.slowPrint("Enter deactivation password; ")
sleep(1)
failsafe = io.read()
if failsafe == "passord1" then
rs.setOutput ("top" , false )
break

else
print ("Wrong deactivation password")
end
until i==3
end
end

Again, I wrote this code off my minecraft computer so there may be som spelling issues, but the code works on my ingame computer :)/>

Thanks

-Kjetil (Norway)
ChunLing #2
Posted 20 December 2012 - 06:28 PM
You could use the parallel API to run a flashing function in parallel with the password entry function. Or you could write a pullEvent loop using timers and storing the characters till you got a password entered.

I think that, for a secure system, you should go with the second, though it's more involved.
kjetilhansen #3
Posted 20 December 2012 - 10:43 PM
Hello,
We play on a closed server so this is just more for fun than a security system, but I don't have much coding experience..
I tried with turning the left output on and off with 1second interval in the same loop as the alarm but it only goes thrugh that once so just one flash, i found another guide on youtube that was

s = 0
 repeat
 rs,setOutput ("left" , true )
 sleep(1)
 rs,setOutput ("left" , false )
 sleep(1)
until s==1

This didn't work eighter, cause then after writing the password wrong the light started flashing and that would go on untill s = 1 somthing that don't happend so i wasn't getting any further..
I also tried with a new repeat and break command between the first repeat, and the else print ("wrong password") this just gave me errors..

-Kjetil
remiX #4
Posted 21 December 2012 - 12:37 AM
This should work, haven't tested it though.
If you need me to explain anything, just ask.
I might have changed the sides, not sure so just fix those :)/>

Spoiler

os.pullEvent = os.pullEventRaw
-- Variables
userPassword = "password"
adminPassword = "admin"
sP = textutils.slowPrint
running = true
adminsafe = false
tries = 0

function clear() term.setCursorPos(1,1) term.clear() end

function flash()
    while true do
        rs.setOutput("left", true)
        sleep(1)
        rs.setOutput("left", false)
        sleep(1)
    end
end

while running do
    clear()
    sP("Radioactive hazard area, atuhorized personnel only!")
    sleep(1)
    print("Please log in: ")
    write(" > ")
    password1 = io.read()
    print("\nAuthorizing, please stand by")
    sP("..................................")
    sleep(1)
    if password1 == userPassword then
        print ("Welcome to Black Mesa nukelear facility!")
        rs.setOutput("right" , true )
        sleep(4)
        rs.setOutput("right" , false)
    else
        tries = tries + 1
        if tries == 2 then
            parallel.waitForAny(function() -- This allows for two functions to work at the same time
            while not adminsafe do
                clear()
                rs.setOutput ("top" , true )
                sleep(1)
                print ("Failsafe mode activated")
                sP("Enter deactivation password: ")
                write(" > ")
                sleep(1)
                password2 = io.read()
                if password2 == adminPassword then
                    rs.setOutput ("top" , false )
                    break
                else
                    print ("\nWrong deactivation password")
                    sleep(1.5)
                end
            end
            end,
            flash)
        end
        print("\nIncorrect password!\nYou have one more try left.")
        sleep(1.5)
    end
end
kjetilhansen #5
Posted 21 December 2012 - 11:46 PM
This thing is great! I will test it out some more, but the basics work :)/>