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

[lua][question] how to use one computer to trigger another one to runa program.

Started by jamesscape2, 26 April 2012 - 10:23 AM
jamesscape2 #1
Posted 26 April 2012 - 12:23 PM
Hi, this is my first post on this plus am a noob at lua.

EDIT: change the code in the first spoiler to the correct code that now correctly and properly works

this is my code for the computer to send the signal out to the other computer after the user has logged in. Feel free on this one to all so optimism some of the code for me it would be useful to me

Spoilerfunction UserCorrect()
print(" Username correct ")
sleep(sleepTime2)
write(" Enter password: ")
inputPass = read("*")
if inputPass == pass then
print(" Password Correct ")
sleep(sleepTime2)
rs.setBundledOutput("back", rs.getBundledOutput("back") + colors.white) print(" Signal Sent On ")
sleep(sleepTime3)
rs.setBundledOutput("back", rs.getBundledOutput("back") - colors.white) print(" Signal Sent Off ")
print(" Code finishes here for now ")
elseif inputPass ~= pass then
print(" Password incorrect.. ")
sleep(sleepTime3)
term.clear()
term.setCursorPos(1,1)
print(" Computer Shutting down.. ")
sleep(sleepTime2)
os.shutdown()
end
end

function UserIncorrect()
if inputUser ~= user then
print(" Username incorrect.. ")
sleep(sleepTime3)
term.clear()
term.setCursorPos(1,1)
print(" Computer Shutting down.. ")
sleep(sleepTime2)
os.shutdown()
end
end

function UserDebug()
if inputUser == debug then
print(" Debugging.. Shutting Down. ")
sleep(sleepTime3)
term.clear()
term.setCursorPos(1,1)
print(" Computer Shutting down.. ")
sleep(sleepTime2)
os.shutdown()
end
end


sleepTime1 = 5
sleepTime2 = 2
sleepTime3 = 3
user = "jamesscape2"
pass = "basketball"
debug = "debug"

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

print(" —————————– ")
print(" Welcome to the Control Centre ")
print(" —————————– ")

sleep(sleepTime1)

write(" Enter username: ")
inputUser = read("*")
if inputUser == user then
UserCorrect()
elseif
inputUser == debug then
UserDebug()
else
UserIncorrect()
end

and this is the code i have so far to start the program on the other computer.
at the moment i am just mucking around with it trying to see if it works, the program is in now way finished

Spoilerwhile rs.getBundledInput("back") = true then
term.clear
term.setCursorPos(1,1)
print("Working")
end

thanks i have been try at this for a hour or so now so it would be great if i could get a straight answer how to fix it

THANKS
Edited on 26 April 2012 - 01:18 PM
jamesscape2 #2
Posted 26 April 2012 - 03:19 PM
Fixing or atleast telling my what to use to enable the other computer to detect the signal coming from the other computer to start its program would help me heaps
libraryaddict #3
Posted 26 April 2012 - 03:32 PM
I dont like bundled cables and use a API to handle them. Which means i dont know how it works.
But for the second piece of code. You need "==" not "="
"==" compares one to another. "=" is setting one to another.

Controlling Computer

rednet.open("top") -- Opens a modem on top of the computer
User = {} -- Makes a table
ID = 0 --Set this to the computerID of the one you are controlling

--For the User and Password. You can add multiple users and a password per user.
--For just one user. Delete User[2]. To add a user use User[3] User[4] etc.
--You need to do the same thing for Password. But with a password.
--You proberly dont want to use capitalized letters. Its caps specific. As in you need to type the caps in correctly too.
User[1] = "Jack"
User[2] = "Sam"
Password = {}
Password[1] = "Jill"
Password[2] = "Dam"
function Clear()
  term.clear()
  term.setCursorPos(1,1)
end
function OpenDoor(UserName)
  Clear()
  print("Welcome "..UserName)
  rednet.send(ID, "Open")
  sleep(5)
  rednet.send(ID, "Close")
end

while true do -- While true is true do this
  Clear() -- Calls on function Clear() which clears the screen
  write("User: ")
  local Ignore,UserTry = pcall(read, "*") -- pcall catches errors and throws it as a status
  write("Password: ")
  local Ignore,PassTry = pcall(read, "*")
  for n=1,#User do -- Starting from 1 to the max Users there is. Look for this user.
	if User[n] == UserTry and PassTry == Password[n] then -- If the user exists and the pass is correct. Do it!
	  OpenDoor(User[n])
	  break -- Exit the loop, We dont want any duplicate user errors, And this loop no longer needs to run
	end
  end
end

And the slave computer

rednet.open("top")
ID = 0 --Set this to the master Computer
term.setCursorPos(1,1)
term.clear()
print("Dobby says you may NOT touch this computer!")
while true do
  event,param1,param2 = os.pullEventRaw() -- os.pullEvent() is a modified version of os.pullEventRaw() in that its safer to use. But its not Ctrl+T proof
  if event == "rednet_message" and param1 == ID then
	if param2 == "Open" then
	  rs.setOutput("back", true)
	elseif param2 == "Close" then
	  rs.setOutput("back", false)
	end
  end
end

I didnt put bundled in :
This is also Ctrl+T proof.
Just setup the users/ID's and rename as startup.
Then restart the computers.
Viola!

If you dont want it Ctrl+T proof then rename os.PullEventRaw() to os.pullEvent() and pcall(read, "*") to read()

So anyways.
You should be able to use/look at that
Kolpa #4
Posted 26 April 2012 - 03:33 PM
coudnt u just ask for any redstone input ? like

send:
redstone.setOutput("back", true)
sleep(sleepTime3)
redstone.setOutput("back", false)

receive:
while true do 
if rs.getInput("back") == true then
break
end
end
print("working")

also ur using while wrong :)/>/> thats the main problem
i fixed it 4 u
libraryaddict #5
Posted 26 April 2012 - 03:39 PM
I could of sworn he asked to control a computer from another computer.

Was wondering if I should give him code to transfer entire files from computer to computer.

Oh well. My mistake xD