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

Forcefield program not working (rednet)

Started by graywolf69, 05 February 2015 - 02:32 PM
graywolf69 #1
Posted 05 February 2015 - 03:32 PM
I wrote a program yesterday and it isn't working. Basically it sends an off or on command to a computer that turns the forcefield on or off. Problem is I can turn the forcefield on but not off. I've gone through it 3-4 times and couldn't figure out what was wrong, I even copied and pasted the off command and just changed some of the parts and it didn't work. Help is greatly appreciated!

Code:

Sending program: http://pastebin.com/F0j9TugB

Receiving program: http://pastebin.com/ApL20f1t
HPWebcamAble #2
Posted 05 February 2015 - 03:50 PM
At first glance, I think this is the problem


if message == forceon then --#Line 12 from the receiving program

Should be


if message == "forceon" then

without the quotes, its a variable, but with the quotes, its a string.
graywolf69 #3
Posted 05 February 2015 - 04:22 PM
Nope, force on works, it's force off that doesn't. Also I wrote everything this way and the other stuff works.
Edited on 05 February 2015 - 03:23 PM
KingofGamesYami #4
Posted 05 February 2015 - 04:53 PM
Here, I rewrote your program(s) to reflect what it is actually doing:

Spoiler

rednet.open("top")

while true do
    term.clear()
    term.setCursorPos(1,1)
    print("Graywolf Industries forcefield control terminal:")
    print("Waiting for wireless commands, manual entry not permitted.")
    id,message = rednet.receive()

    if id == 9 or id == 10 then

        if message == nil then
                term.clear()
                term.setCursorPos(1,1)
                print("Wireless input recieved, forcefield coming online...")
                rs.setOutput("left", true)
                sleep(1)
        elseif message == nil then
                term.clear()
                term.setCursorPos(1,1)
                print("Wireless input recieved, forcefield coming offline...")
                rs.setOutput("left", false)
                sleep(1)

        end

      end

end

Spoiler

rednet.open("back")

while true do

term.clear()
term.setCursorPos(1,1)
print("Graywolf Industries portable control terminal")
print("Please enter your administration password:)")
        local i = read("*")

        if i == ("4969") then
                term.clear()
                term.setCursorPos(1,1)
                print("Password accepted. Enter a command: launch1, launch2, launch3, launch4, launchall, forceon, forceoff")
                local i2 = read()

                if i2 == ("launch1") then
                        print("Launching missile silo 1 at preconfigured target! Silo will be reloaded in 14 seconds...")
                        rednet.send(4, nil)
                        sleep(1)

                elseif i2 == ("launch2") then
                        print("Launching missile silo 2 at preconfigured target! Silo will be reloaded in 14 seconds...")
                        rednet.send(5, nil)
                        sleep(1)

                elseif i2 == ("launch3") then

                        print("Launching missile silo 3 at preconfigured target! Silo will be reloaded in 14 seconds...")
                        rednet.send(6, nil)
                        sleep(1)

                elseif i2 == ("launch4") then
                        print("Launching missile silo 4 at preconfigured target! Silo will be reloaded in 14 seconds...")
                        rednet.send(7, nil)
                        sleep(1)

                elseif i2 == ("launchall") then
                        print("Launching all missiles at preconfigured targets! Silos will be reloaded in 14 seconds...")
                        rednet.send(4, nil)
                        rednet.send(5, nil)
                        rednet.send(6, nil)
                        rednet.send(7, nil)
                        sleep(1)       


                elseif i2 == ("forceon") then
                        print("Turning on forcefield, may take a while...")
                        rednet.send(8, nil)
                        sleep(1)


                elseif i2 == ("forceoff") then
                        print("Turning off forcefield...")
                        rednet.send(8, nil)
                        sleep(1)


                else
                print("Incorrect command! Please try again.")
                end

        end




end

Since you either send it nil or nil, message will always equal nil in the first statement. Basically, when you think you are telling it to turn off, you are telling it to turn on.

Edit2: If you don't believe me, run this:


if forceon == forceoff then
   print( "They are equal!" )
else
   print( "They are not equal!" )
end
Edited on 05 February 2015 - 04:00 PM
HPWebcamAble #5
Posted 05 February 2015 - 04:56 PM
Here, I rewrote your program to reflect what it is actually doing:

Spoiler

rednet.open("top")

while true do
	term.clear()
	term.setCursorPos(1,1)
	print("Graywolf Industries forcefield control terminal:")
	print("Waiting for wireless commands, manual entry not permitted.")
	id,message = rednet.receive()

	if id == 9 or id == 10 then

		if message == nil then
				term.clear()
				term.setCursorPos(1,1)
				print("Wireless input recieved, forcefield coming online...")
				rs.setOutput("left", true)
				sleep(1)
		elseif message == nil then
				term.clear()
				term.setCursorPos(1,1)
				print("Wireless input recieved, forcefield coming offline...")
				rs.setOutput("left", false)
				sleep(1)

		end

	  end

end

Haha, was just about to post the fix.

To clarify, you need to use "" (quotation marks) around the messages. If you don't, it just uses the variable, which is nil since you never initialized them.
graywolf69 #6
Posted 05 February 2015 - 11:57 PM
Thank you so much, it worked!