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

help with some redstone stuff in computer craft

Started by mattias1172, 28 December 2012 - 03:01 PM
mattias1172 #1
Posted 28 December 2012 - 04:01 PM
I know the very basics of how to use computer craft however, i am stuck on a project i am trying to do. Once ik how to do this, i will be able to do varients of stuff like this on my own. What i am trying to do is use a computer connected to a transmitter on the back of the computer to turn on and off a forcefield via a password. I got most of the coding done but i cannot figure out how to make it so the redstone stays ON while the computer reboots and resets back to the main page asking for a password. that way i can type in my password to turn off the redstone. when i type in my password to turn the redstone on, it turns on fine but now the program is unlocked and the main terminal is back up which i dont want and if i reboot the redstone turns off automatically. i need a way for the redstone to turn on and stay on and be able to type in a password for the redstone to turn off and vice versa.

here is a copy of what i have written in the computer:

term.clear()
term.setCursorPos(1, 1)
print("Forcefield")
input = read("*")
if input == "startup" then
redstone.setOutput("back", true)
print("Forcefield Online")

elseif input == (this password is so i can get back to the main terminal to edit files" then
print("Terminal Unlocked")

elseif input == "shutdown" then
redstone.setOutput("back", false)
print("Forcefield Offline")

else
print("Password Incorrect!")
os.reboot()

end



basically i think i need a way to loop this code or something

Thanks
Geforce Fan #2
Posted 28 December 2012 - 04:07 PM
Hmmm…
It's impossible for it to be on while it reboots, however if the computer never shuts down then…
you could make this your code and it'd work.

while true do
term.clear()
term.setCursorPos(1, 1)
print("Forcefield")
input = read("*")
if input == "startup" then
redstone.setOutput("back", true)
print("Forcefield Online")

elseif input == (this password is so i can get back to the main terminal to edit files" then
print("Terminal Unlocked")

elseif input == "shutdown" then
redstone.setOutput("back", false)
print("Forcefield Offline")

else
print("Password Incorrect!")
os.reboot()

end
end
This is probably the fastest response ever. :3
mattias1172 #3
Posted 28 December 2012 - 04:16 PM
Hmmm…
It's impossible for it to be on while it reboots, however if the computer never shuts down then…
you could make this your code and it'd work.

while true do
term.clear()
term.setCursorPos(1, 1)
print("Forcefield")
input = read("*")
if input == "startup" then
redstone.setOutput("back", true)
print("Forcefield Online")

elseif input == (this password is so i can get back to the main terminal to edit files" then
print("Terminal Unlocked")

elseif input == "shutdown" then
redstone.setOutput("back", false)
print("Forcefield Offline")

else
print("Password Incorrect!")
os.reboot()

end
end
This is probably the fastest response ever. :3


when i add the second end i get this error: bios:206: [string "startup"]:21: '<eof>' expected

not sure exactly what that means yet…


(yes i did try the "end" both inline with the other "end" and inline with the redstone.setoutput line


i also tried the "when true do" line at the top and i got another error similar to the one above except it states it expected a "=" in line 1 —-> bios:206: [string "startup"]:1: '=' expected

that above error is including the extra "end"
Grim Reaper #4
Posted 28 December 2012 - 04:43 PM
I've annotated the code a bit to give you an idea of what is going on.



while true do
    term.clear()
    term.setCursorPos(1, 1)
    print("Forcefield")
    input = read("*")
    if input == "startup" then
        redstone.setOutput("back", true)
        print("Forcefield Online")

    -- The following line is an un-ended string, so the interpreter doesn't detect a 'then'.
    -- This is because you're using an opening parenthesis rather than an opening quote to distinguish
    -- a string. The rest of the line is interpreted as an unended string.

    -- OLD_LINE: elseif input == (this password is so i can get back to the main terminal to edit files" then
    elseif input == "this password is so i can get back to the main terminal to edit files" then
        print("Terminal Unlocked")

    elseif input == "shutdown" then
        redstone.setOutput("back", false)
        print("Forcefield Offline")

    else
        print("Password Incorrect!")
        os.reboot()
    end
end

I've commented out the line that was problematic and fixed it.

Also, when you tried using

when true do
and you received an error: 'when' is not a valid keyword and thus is interpreted as an identifier and thus expects some kind of comparison or assignment (hence the '= expected').
mattias1172 #5
Posted 28 December 2012 - 04:57 PM
I've annotated the code a bit to give you an idea of what is going on.



while true do
	term.clear()
	term.setCursorPos(1, 1)
	print("Forcefield")
	input = read("*")
	if input == "startup" then
		redstone.setOutput("back", true)
		print("Forcefield Online")

	-- The following line is an un-ended string, so the interpreter doesn't detect a 'then'.
	-- This is because you're using an opening parenthesis rather than an opening quote to distinguish
	-- a string. The rest of the line is interpreted as an unended string.

	-- OLD_LINE: elseif input == (this password is so i can get back to the main terminal to edit files" then
	elseif input == "this password is so i can get back to the main terminal to edit files" then
		print("Terminal Unlocked")

	elseif input == "shutdown" then
		redstone.setOutput("back", false)
		print("Forcefield Offline")

	else
		print("Password Incorrect!")
		os.reboot()
	end
end

I've commented out the line that was problematic and fixed it.

Also, when you tried using

when true do
and you received an error: 'when' is not a valid keyword and thus is interpreted as an identifier and thus expects some kind of comparison or assignment (hence the '= expected').


my apologies, the correction u made, i had already had it that way in my computer, so it already looks like that. i wrote it wrong on my post and i didnt see it. The question is still un answered. :(/>
Grim Reaper #6
Posted 28 December 2012 - 05:05 PM
You can't exactly keep the redstone on when the computer reboots because the computer is the thing generating the redstone signal and therefore cannot keep a continual redstone pulse going when they shutoff.

However, you can save the state of the computer so when the computer turns back on the redstone signal will resume.
Or, you could have a second computer that receives the redstone signal and toggles the signal. This way, when the computer turns off the signal won't turn off until another redstone signal is sent to the transmitting computer. You might also need to save the state of the toggle when the computer shuts off.

I can show you how to do this if you think it will solve your problem.
mattias1172 #7
Posted 28 December 2012 - 05:10 PM
if u think that will work sure, however, i do not want two computers controlling one forcefield. It doesnt need to reboot everytime. What i really mean is that when i type in "startup" the forcefield turns on. But i want another code there or something that makes the Program restart and ask for another password again to turn off the forcefield. Then when its off it will turn it on again and a continiues circle of this.
Grim Reaper #8
Posted 28 December 2012 - 05:16 PM
Here you are:


local forcefieldState  = false -- False is offline.
local forcefieldToggle = "protect"  -- The command to be entered in order to toggle the forcefield.
local terminalPassword = "password" -- The command to be entered in order to return to the terminal.

while true do
        term.clear()
        term.setCursorPos(1, 1)
        print("Forcefield: " .. (forcefieldState and "online" or "offline"))
        input = read("*")
        if input == "startup" then
                redstone.setOutput("back", true)
                print("Forcefield Online")

        elseif input == terminalPassword then
                print("Terminal Unlocked")
        -- Toggle the forcefield if the correct command is entered.
        elseif input == forcefieldToggle then
            forcefieldState = not forcefieldState
            rs.setOutput("back", forcefieldState)
        else
                -- This will simply prompt the user that the password entered is wrong, then restart the loop after two seconds.
                print("Password Incorrect!")
                sleep(2)
        end
end

If you need me to explain anything, I'd be happy to.
mattias1172 #9
Posted 28 December 2012 - 05:26 PM
thanks, i understand most of it but do i add everyhting exactly as type in that quote? and two more things, first, is there a way for me to just copy this code and paste it into the computer? and second, ik how to use floppy disks for the most part (the basics) but if i wanna say copy this code onto one so i can quickly put it onto another computer, how would i do this? i just started using computercraft today (been using tekkit for a few months tho) so i still need to learn the basics
Grim Reaper #10
Posted 28 December 2012 - 05:32 PM
You can copy this code word for word if you want and then tweak as you wish.
As for pasting into the game: there are auto-typers, but the best way to do this is with pastebin.

To use pastebin, make sure that the http api is enabled and then type the following: pastebin get <code>.
In this case, you'd type:

pastebin get wVEFdYMa
Uploaded to: http://pastebin.com/wVEFdYMa

If you wanted to copy this file to a disk, you could use the following command:

cp fileName disk/fileName
* Replace "fileName" with whatever the program is called on your computer.
mattias1172 #11
Posted 28 December 2012 - 06:18 PM
for the most part it works! thanks. just a few bugs tho. First, i can type anything to tuen the forcefield off when its on, second, the status doesnt switch to online when i type startup. however the forcefield does turn on
Grim Reaper #12
Posted 28 December 2012 - 07:03 PM
Try this version:
http://pastebin.com/hSm56iDX

Code:
Spoiler


local forcefieldToggle = "protect"  -- The command to be entered in order to toggle the forcefield.
local terminalPassword = "password" -- The command to be entered in order to return to the terminal.
local forcefieldSavePath = "/.forcefieldState" -- The location on the computer where the state of the forcefield is stored.

-- Saves the state of the forcefield.
function saveForcefieldState(state)
    local fileHandle = fs.open(forcefieldSavePath, 'w')
    fileHandle.write(state)
    fileHandle.close()
end

-- Loads the state of the forcefield from the forcefield saved path.
function loadForcefieldState()
    local fileHandle = fs.open(forcefieldSavePath, 'r')
    local string_forcefieldState = fileHandle.readAll()
    fileHandle.close()

    if string_forcefieldState == "true" then
        return true
    end

    return false
end

-- This initializes the program if the file that stores the state doesn't exist.
-- It starts by assuming that the forcefield is off. I would suggest changing this to whatever
-- the state is when you install this program.
if not fs.exists(forcefieldSavePath) then
    saveForcefieldState(false)
end
local forcefieldState  = loadForcefieldState() -- False is offline.

-- Main loop.
while true do
        term.clear()
        term.setCursorPos(1, 1)
        print("Forcefield: " .. (forcefieldState and "online" or "offline"))
        input = read("*")

        if input == "startup" then
            redstone.setOutput("back", true)
            print("Forcefield Online")

        elseif input == terminalPassword then
            saveForcefieldState(forcefieldState)
            print("Terminal Unlocked")

            -- This return statement returns control back to the shell function that executed it.
            return
        -- Toggle the forcefield if the correct command is entered.
        elseif input == forcefieldToggle then
            forcefieldState = not forcefieldState
            rs.setOutput("back", forcefieldState)
        else
            print("Password Incorrect!")
            saveForcefieldState(forcefieldState)
            os.reboot()
        end
end

I've implemented a couple features that cause the program to save the current state of the forcefield to a file that shouldn't be visible from the shell unless you know its actual path or don't attempt to look at the files on the computer using the 'list' or 'dir' commands.
mattias1172 #13
Posted 28 December 2012 - 07:33 PM
startup:28: attempt to call nil

is what i get
Grim Reaper #14
Posted 28 December 2012 - 07:44 PM
startup:28: attempt to call nil

is what i get

Sorry, I spelled something incorrectly.
Here is the fix (also in the above post):



local forcefieldToggle = "protect"  -- The command to be entered in order to toggle the forcefield.
local terminalPassword = "password" -- The command to be entered in order to return to the terminal.
local forcefieldSavePath = "/.forcefieldState" -- The location on the computer where the state of the forcefield is stored.

-- Saves the state of the forcefield.
function saveForcefieldState(state)
	local fileHandle = fs.open(forcefieldSavePath, 'w')
	fileHandle.write(state)
	fileHandle.close()
end

-- Loads the state of the forcefield from the forcefield saved path.
function loadForcefieldState()
	local fileHandle = fs.open(forcefieldSavePath, 'r')
	local string_forcefieldState = fileHandle.readAll()
	fileHandle.close()

	if string_forcefieldState == "true" then
		return true
	end

	return false
end

-- This initializes the program if the file that stores the state doesn't exist.
-- It starts by assuming that the forcefield is off. I would suggest changing this to whatever
-- the state is when you install this program.
if not fs.exists(forcefieldSavePath) then
	saveForcefieldState(false)
end
local forcefieldState  = loadForcefieldState() -- False is offline.

-- Main loop.
while true do
		term.clear()
		term.setCursorPos(1, 1)
		print("Forcefield: " .. (forcefieldState and "online" or "offline"))
		input = read("*")

		if input == "startup" then
			redstone.setOutput("back", true)
			print("Forcefield Online")

		elseif input == terminalPassword then
			saveForcefieldState(forcefieldState)
			print("Terminal Unlocked")

			-- This return statement returns control back to the shell function that executed it.
			return
		-- Toggle the forcefield if the correct command is entered.
		elseif input == forcefieldToggle then
			forcefieldState = not forcefieldState
			rs.setOutput("back", forcefieldState)
		else
			print("Password Incorrect!")
			saveForcefieldState(forcefieldState)
			os.reboot()
		end
end

http://pastebin.com/nGYFdeuk
remiX #15
Posted 28 December 2012 - 10:42 PM
I'd suggest to use
saveForcefieldState(forcefieldState)
after each offline and online, because logging off may mess it up and not save.
ChunLing #16
Posted 29 December 2012 - 03:38 AM
I may be missing something here, but can't you invert the redstone output using a torch? Then it's a simple matter of making it so that entering the password sends a signal to turn off the torch.
AngelMalus #17
Posted 29 December 2012 - 03:41 AM
While the computer is off there is no power from it, for that you could use an RSLatch or a MemoryCell or a Piston with 1 thick to TOGGLE a redstone circuit on and off.
And you can still control the system with your computer.

Over here I have a simple memory cell controlled by a computer.
You can see in the 2 photos that the piston is holding different states, while the computer is off and is sending no power.



The problem with that setup is that the computer needs 2 outputs to control. It can also be done with 1 output, but its just a little more complex.

Depending on the mods you have, you could use the redpower gates. Specifically the Toggle one, that way, you can only use 1 side from the computer to send output signal and toggle on and off.



Sadly with this 2 your computer won't be able to know the state of the output once it turns back on.
But…


On this version the computer can still change the output, and check the current state once it turns back on.

Again it all depends on the mods you have, here is with bundled cables and color wires. This setup will also let you know the status of the output when the computer turns on, and it only uses one side of the computer:


Hope it helps.
ChunLing #18
Posted 29 December 2012 - 05:23 PM
Um…wouldn't a single redstone torch inverter work for letting the computer send a signal to turn it off, leaving it on the rest of the time?