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

Run Turtleprogram from a Computer?

Started by Klausar, 29 September 2012 - 04:16 PM
Klausar #1
Posted 29 September 2012 - 06:16 PM
I want to know how to run a Turtleprogram from my Computer. For Example if the program is "Stone" and mines at a stonegenerator, what should be the code for the Computer, and for the turtle? Oh and for example my treecutter asks me howmany trees it should cut down, I would also have to see that and answer it from my Computer.
Anonomit #2
Posted 29 September 2012 - 06:23 PM
I'm not sure what you're asking, but if you want to run a program on a computer that can only be found on a turtle, try this:

local program = "put the program name here"
shell.run( "copy", "rom/programs/turtle/" .. program, "/" )
shell.run( program )
Klausar #3
Posted 29 September 2012 - 06:32 PM
I want to run a program on my turtle but start it from my computer.
Anonomit #4
Posted 29 September 2012 - 06:39 PM
I want to run a program on my turtle but start it from my computer.

Is the computer right next to the turtle? Do they have modems? Do they have a redstone connection? At least one of these things is required
Klausar #5
Posted 29 September 2012 - 06:53 PM
They have modems, I'm not sure how to connect them though.
Anonomit #6
Posted 29 September 2012 - 07:03 PM
Ok. For the computer, this should run inside a program:

local modemSide = "side of modem" --replace with side the modem is on
local turtleID = id of turtle  --replace with turtle id. the id of the turtle can be found by typing 'id' into the turtle

rednet.open( modemSide ) --turns on the modem
rednet.send( turtleID, "begin" ) --sends the message "begin" to the turtle

And for the turtle, run this:


local modemSide = "right"
local programToRun = "name of program to run" --replace with name of program that you want to start remotely

while true do
id, msg, dist = rednet.receive() --waits for message
if msg == "begin" then --if the message says "begin" then start the program
shell.run( programToRun ) --starts the program
end
end
Klausar #7
Posted 29 September 2012 - 07:11 PM
Doesn't work :)/>/> My Computer code looks like this:
local modemSide = "right"
local turtleID = 4

rednet.open( modemSide ) --turns on the modem
rednet.send( turtleID, "begin" ) --sends the message "begin" to the turtle. The id of the turtle can be found by typing id into the turtle

While my startup for the turtle looks like this:
local modemSide = "right"
local programToRun = "stone"

while true do
id, msg, dist = rednet.receive()
if msg == "begin" then
shell.run( stone )
end
end

Anonomit #8
Posted 29 September 2012 - 07:14 PM
Hmmm, I haven't tested it myself. What error did it give? or did it just do nothing? Is the program's name stone, with all lowercase letters?
Klausar #9
Posted 29 September 2012 - 07:15 PM
Tried with capital letters too and yes the program is called stone.
Anonomit #10
Posted 29 September 2012 - 08:03 PM
I've changed the rednet.receive() into an os.pullEvent()
Shouldn't make a difference, but I've traditionally used that, so I'm more comfortable with it.
The program "stone" should be in the root folder of the turtle, or the rom. Is it possible that you put the wrong modemSide? It depends on which way you face. The modem should light up red if you configured it properly. If it isn't change the modemSide or put the modem on the side "back"

Computer:

local modemSide = "side of modem" --replace with side the modem is on
local turtleID = id of turtle  --replace with turtle id. the id of the turtle can be found by typing 'id' into the turtle

rednet.open( modemSide ) --turns on the modem
rednet.send( turtleID, "begin" ) --sends the message "begin" to the turtle

Turtle:

local modemSide = "right"
local programToRun = "name of program to run" --replace with name of program that you want to start remotely

while true do
event, id, msg, dist =os.pullEvent( "rednet_message" ) --waits for message
if msg == "begin" then --if the message says "begin" then start the program
shell.run( programToRun ) --starts the program
end
end
Klausar #11
Posted 30 September 2012 - 07:50 AM
Still nothing happens, that's weird.

Edit: The modem on the computer lights up, the one on the turtle doesn't, I tried all sides.
Heroj04 #12
Posted 30 September 2012 - 09:00 AM
You havent opened th modem on the tirtle add a

rednet.open("right")
to the start of your turtle program
Klausar #13
Posted 30 September 2012 - 09:05 AM
You havent opened th modem on the tirtle add a

rednet.open("right")
to the start of your turtle program

Thanks, but it still does nothing.
Heroj04 #14
Posted 30 September 2012 - 09:08 AM
Well apart from that i cant see anything
Anonomit #15
Posted 30 September 2012 - 03:50 PM
Are they really far apart? By default, the config specifies that rednet can travel only 64 blocks (in version 1.3.*)
That's the only thing I can think of that might be stopping the from connecting now
Klausar #16
Posted 30 September 2012 - 03:52 PM
There are only 20 blocks of air between them. Not too far at all.
Anonomit #17
Posted 30 September 2012 - 04:18 PM
Ok, Let's make it use redstone instead then. Note that if you're using the tekkit pack, computers can output a signal into red alloy wire, but must receive a signal from redstone.
Change the redstoneSide to the side the computer will output to.

Computer:

local redstoneSide = "side of modem" --replace with side the redstone is to be outputted from

rs.setOutput( redstoneSide, true ) --turns on redstone
sleep( 0.1 ) --waits
rs.setOutput( redstoneSide, false ) --turns off redstone

Turtle:

local programToRun = "name of program to run" --replace with name of program that you want to start remotely

while true do
event, param1, param2 =os.pullEvent( "redstone" ) --waits for redstone change
shell.run( programToRun ) --starts the program
sleep( 1 ) --sleeps so that it will not notice the redstone turning off. probably not necessary.
end
end
sebbeviper #18
Posted 01 October 2012 - 02:35 PM
Sorry to Hijack this thread but i got the wireless way to work by adding rednet.open("right") at the top of the turtle program. Now, the mining program i have is controllable so that i can choose the dimensions of the tunnel and whether or not i would like to place torches along the way, how should i do to be able to acces that from the computer instead of having to start the turtle from the computer and then having to walk to the turtle to then get it to start?
Anonomit #19
Posted 01 October 2012 - 03:28 PM
Sorry to Hijack this thread but i got the wireless way to work by adding rednet.open("right") at the top of the turtle program. Now, the mining program i have is controllable so that i can choose the dimensions of the tunnel and whether or not i would like to place torches along the way, how should i do to be able to acces that from the computer instead of having to start the turtle from the computer and then having to walk to the turtle to then get it to start?

you may need to use events instead of calling read() so you can wait for the user to press a number or send a rednet message.
it's more complicated, but you could also use the parallel api to call read() in one function and listen for rednet in another, at the same time
sebbeviper #20
Posted 01 October 2012 - 03:50 PM
Could you give some examples? I have like 0 experience with CC and lua exept some basic password locks :)/>/>
Anonomit #21
Posted 01 October 2012 - 04:52 PM
sure. Here's a simple way to wait for either a key press or a rednet_message:

while true do
local event, param1, param2, param3 = os.pullEvent()
if event == "rednet_message" and param2 == "message that was sent through rednet" then
--this happens if you receive the correct message
elseif event == "rednet_message" and param2 == "different rednet message" then --you can specify a lot of different events to wait for
--stuff that happens if that particular message is received
elseif event == "char" and param1 == "y" then --if the event was a character and the character was 'y'
--this happens if you press 'y' for yes
elseif event == "char" and param1 = "n" then
--this happens if 'n' is pressed
end
I recommend using the wiki to learn more. Try searching for events, os.pullEvent() and rednet.
If you want more flexibility at the cost of your program getting a lot more complicated to understand, try searching for the parallel api. Using it can run multiple functions at once, allowing part of your program to be sleeping or reading input, while other parts wait for rednet messages or create a good-looking UI.
PyroGodz #22
Posted 01 October 2012 - 04:53 PM
I actually have a small question, which isn't worth creating another thread for. I'm wondering how you get the ID of a turtle? If I add print(os.computerID) into the turtles startup, it shows random letters and numbers and those change everytime I restart.
sebbeviper #23
Posted 01 October 2012 - 05:51 PM
if you just type in "id" in the turtle it will show up
FuuuAInfiniteLoop(F.A.I.L) #24
Posted 24 April 2013 - 10:59 AM
The problem in the first code is that you called stone instead of "stone" so it lua will think taht is a variable and, it return nil, so it crash


EDIT: Necromancy!!