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

Turtle Control Help

Started by Zukamimozu, 25 June 2012 - 12:04 AM
Zukamimozu #1
Posted 25 June 2012 - 02:04 AM
Okay so I was working on this program that when you input a command into a computer it would send it to the turtle and the turtle would do so, but I have come across an error that I don't know how to fix.

Computer Code (No Errors)


rednet.open("right")
term.clear()
term.setCursorPos(16, 1)
print "Turtle Control"
while true do
  term.setCursorPos(1, 3)
  write "F, L, R, B, M, U, D?"
  local input = read()

  if input == string.lower("F") then
	rednet.send(58,"F")
	print "Turtle Forward"
  end
  if input == string.lower("B") then
	rednet.send(58,"B")
	print "Turtle Backward"
  end
  if input == string.lower("L") then
	rednet.send(58,"L")
	print "Turtle Left"
  end
  if input == string.lower("R") then
	rednet.send(58,"R")
	print "Turtle Right"
  end
  if input == string.lower("U") then
	rednet.send(58,"U")
	print "Turtle Up"
  end
  if input == string.lower("D") then
	rednet.send(58,"D")
	print "Turtle Down"
  end
  if input == string.lower("M") then
	rednet.send(58,"M")
	print "Turtle Mined"
  end
  if input == string.lower("quit") then
	break
  end
end


Turtle Code (Error: string "startup" :23: 'until' expected (to close 'repeat' at line 2)


rednet.open("right")
repeat
  local event,p1,p2,p3 = os.pullEvent()
  if event=="rednet_message" and p1==57 then
	elseif p2 == "F" then
	  turtle.forward()
	elseif p2 == "B" then
	  turtle.back()
	elseif p2 == "L" then
	  turtle.turnLeft()
	elseif p2 == "R" then
	  turtle.turnRight()
	elseif p2 == "U" then
	  turtle.up()
	elseif p2 == "D" then
	  turtle.down()
	elseif p2 == "M" then
	  turtle.dig()
	elseif p2 == "return" then
	  for x=1,20 do
		turtle.down()
	end
end

I would appreciate it if you would help.
MysticT #2
Posted 25 June 2012 - 02:15 AM
The repeat loop syntax is:

repeat
  -- code
until <condition>
And it repeats the code until the condition is true.

Also, doing this:

if input == string.lower("F") then
  rednet.send(58,"F")
  print "Turtle Forward"
end
Is the same as:

if input == "f" then
  rednet.send(58, "F")
  print "Turtle Forward"
end
But I think you want it to be:

if string.lower(input) == "f" then
  rednet.send(58, "F")
  print "Turtle Forward"
end

And you should use elseif, like you did in the turtle code. That way, it won't check every condition always.

Fixed turtle code:

rednet.open("right")
while true do -- use a while loop instead of repeat
  local id, msg = rednet.receive()
  if id == 57 then
    if msg == "F" then
	  turtle.forward()
    elseif msg == "B" then
	  turtle.back()
    elseif msg == "L" then
	  turtle.turnLeft()
    elseif msg == "R" then
	  turtle.turnRight()
    elseif msg == "U" then
	  turtle.up()
    elseif msg == "D" then
	  turtle.down()
    elseif msg == "M" then
	  turtle.dig()
    elseif msg == "return" then
	  for x = 1, 20 do
	    turtle.down()
	  end
    end
end
Zukamimozu #3
Posted 25 June 2012 - 02:30 AM
all right, that worked but it can only go forward…
MysticT #4
Posted 25 June 2012 - 02:35 AM
Post the new code, so we can see the error.
Zukamimozu #5
Posted 25 June 2012 - 04:08 AM

rednet.open("right")
term.clear()
term.setCursorPos(16, 1)
print "Turtle Control"
while true do
  term.setCursorPos(1, 3)
  write "F, L, R, B, M, U, D?"
  local input = read()
 
  if input == string.lower("f") then
    rednet.send(58,"F")
    print "Turtle Forward"
 
  elseif input == string.lower("b") then
    rednet.send(58,"B")
    print "Turtle Backward"
  elseif input == string.lower("l") then
    rednet.send(58,"L")
    print "Turtle Left"
 
  elseif input == string.lower("r") then
    rednet.send(58,"R")
    print "Turtle Right"
 
  elseif input == string.lower("u") then
    rednet.send(58,"U")
    print "Turtle Up"
 
  elseif input == string.lower("d") then
    rednet.send(58,"D")
    print "Turtle Down"
 
  elseif input == string.lower("m") then
    rednet.send(58,"M")
    print "Turtle Mined"
 
  elseif input == string.lower("quit") then
    break
  end
end
Bossman201 #6
Posted 25 June 2012 - 04:11 AM
Turtles can only go forward…What is your problem? Does the turtle not turn?

Turtle code:
local id, msg = rednet.receive()
msg = string.upper(msg)
Add the second line right under the first line in your code.

Computer code:
local input = read()
input = string.upper(input)
Take out all of the string.lower() in your 'if' statements.

Both codes:
else
print("No command exists.")
end
Put this right after the last 'elseif'.

This code will remove all 'wildcards' from user input. If you added the else statement without making the other changes, sometimes user input will not match the conditions and the 'else' message will be shown, even if the user input the correct letter. Using string.upper() will always make the user input capital letters, which is how you're checking them.
Zukamimozu #7
Posted 25 June 2012 - 04:35 AM
Every command I put in comes out as a non-existing command…


rednet.open("right")
term.clear()
term.setCursorPos(16, 1)
print "Turtle Control"
while true do
  term.setCursorPos(1, 3)
  write "F, L, R, B, M, U, D?"
  local input = read()
 
  if input == string.upper(f) then
    rednet.send(58,"f")
    print "Turtle Forward"
 
  elseif input == string.upper("b") then
    rednet.send(58,"b")
    print "Turtle Backward"
  elseif input == string.upper("l") then
    rednet.send(58,"l")
    print "Turtle Left"
 
  elseif input == string.upper("r") then
    rednet.send(58,"r")
    print "Turtle Right"
 
  elseif input == string.upper("u") then
    rednet.send(58,"u")
    print "Turtle Up"
 
  elseif input == string.upper("d") then
    rednet.send(58,"d")
    print "Turtle Down"
 
  elseif input == string.upper("m") then
    rednet.send(58,"m")
    print "Turtle Mined"
 
  elseif input == string.upper("quit") then
    break
  else
  print "No command Exists"
  end
end
kazagistar #8
Posted 25 June 2012 - 06:58 AM
I fixed your code. Lua is not actually VB or Python or Logo or whatever… you have to write your () for functions here, because any function can take a variable number of parameters, and there is no other way to signify what is parameters and what is the next command.

I also tossed out a turtle variable in front, to make it easier to switch up, and fixed some of the terminal formatting. Just cause I am such a nice dude. :P/>/>

local TURTLE = 20
rednet.open("right")
term.clear()
term.setCursorPos(16, 1)
print("Turtle Control")
while true do
  term.setCursorPos(1, 3)
  write("F, L, R, B, M, U, D? ")
  local input = string.upper(read())
  term.clearLine()
 
  if input == "F" then
    rednet.send(TURTLE,"f")
    print("Turtle Forward")

  elseif input == "B" then
    rednet.send(TURTLE,"b")
    print("Turtle Backward")

  elseif input == "L" then
    rednet.send(TURTLE,"l")
    print("Turtle Left")

  elseif input == "R" then
    rednet.send(TURTLE,"r")
    print("Turtle Right")

  elseif input == "U" then
    rednet.send(TURTLE,"u")
    print("Turtle Up")

  elseif input == "D" then
    rednet.send(TURTLE,"d")
    print("Turtle Down")

  elseif input == "M" then
    rednet.send(TURTLE,"m")
    print("Turtle Mined")

  elseif input == "QUIT" then
    term.clear()
    break

  else
    print("No command Exists")
  end
end
Zukamimozu #9
Posted 25 June 2012 - 07:07 PM
Thanks, that worked :P/>/>
Bossman201 #10
Posted 25 June 2012 - 07:50 PM
I think we should explain to him why code doesn't work, rather than just fixing it for him.

while true do
  term.setCursorPos(1, 3)
  write "F, L, R, B, M, U, D?"
  local input = read() --If you do not capitalize your letters, all 'if statement' conditions will be false and skipped over, resulting in the 'else' code being run.
  --use 'local input = string.upper(read())' instead
  if input == string.upper(f) then --Unrelated: Forgot quotation marks around the letter f here.
	rednet.send(58,"f")
	print "Turtle Forward"

  elseif input == string.upper("b") then --Never write a condition like this.
   --It is the same as 'elseif input == "B" then'
   --I think the code you really wanted was 'elseif string.upper(input) == "B" then'
   --But again, using 'local input = string.upper(read())' above will make all input capitalized after pressing enter.
	rednet.send(58,"b")
	print "Turtle Backward"
  --code
  --code
  --code
  --code
  else --Always good to have else statements, helps a lot with debugging.
   print "No command Exists"
  end
end
kazagistar #11
Posted 25 June 2012 - 07:58 PM
I did though. His code didn't work because he did not use (). He was also use string.upper, on constants, which is utterly useless, but not actually wrong.
Zukamimozu #12
Posted 25 June 2012 - 08:33 PM
thanks and I don't quite understand the language or what some of the command thins do such as string.lower, I was following 2 tutorials and decided to combine them to get this idea. If you guys are still willing help how would I modify it to change so instead I would tap certain keys to move it, because it does get annoying typing commands. If you are willing to do this for me can you assign it to WASD to movement QE for up down and SPACE for dig. If not can you explain to me how?
MysticT #13
Posted 26 June 2012 - 12:01 AM
I did though. His code didn't work because he did not use ().
Not really, the brackets are optional if there's only one parameter.

thanks and I don't quite understand the language or what some of the command thins do such as string.lower, I was following 2 tutorials and decided to combine them to get this idea.
You should read the lua manual and some tutorials to learn the language.
string.lower and string.upper just converts the string to lower or upper case.

If you guys are still willing help how would I modify it to change so instead I would tap certain keys to move it, because it does get annoying typing commands. If you are willing to do this for me can you assign it to WASD to movement QE for up down and SPACE for dig. If not can you explain to me how?
You need to use the "key" or "char" events, heres a little help:
For "key" event:

local evt, key = os.pullEvent("key")
if key == 57 then -- Space
  -- space pressed, dig
elseif key == 200 then -- Up
  -- up pressed, move forward
end
You can see the key codes here.
For char event:

local evt, c = os.pullEvent("char")
if c == "w" then
  -- w pressed, move forward
elseif c == "s" then
  -- s pressed, move back
end

You can also use both:

local evt, arg = os.pullEvent()
if evt == "char" then
  if arg == "w" then
    -- w pressed, move forward
  elseif arg == "s" then
    -- s pressed, move back
  end
elseif evt == "key" then
  if arg == 57 then
    -- space pressed, dig
  end
end
Zukamimozu #14
Posted 27 June 2012 - 04:51 AM
Okay I will try this, I'm thinking that key is like a number assigned to a key and char is the actual letter in the code? and if using char how would you do "space"? just put "space" down?