Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
redstorm64

Simple program debug, looks correct but won't run

Question

I've got a simple program that won't run due to an apparent missing "end" somewhere in the code.  I've looked over the code a ridiculous number of times and can't find anything wrong with it.  Hoping somebody is willing to review my code quick to see if I've missed something.  I'm hoping I've just overlooked something simple.

 

I've run the program successfully in parts, with everything included except for the first if statement (line 28 thru 86).  As soon as I add that section, I get an error looking for an 'end' to close the while loop at line 17 [see attached].

local component = require("component")
local modem = component.modem
local term = require("term")
local event = require("event")

local prgms = {"move to","home","unready","reportPos","exit"}

local port = 100
print("Starting Robot Commander...")
print("Opening port "..port.."...")
modem.open(port)
print("Port "..port.." open: :"..tostring(modem.isOpen(port)))
os.sleep(1)

local select = 0

while prgms[select] ~= "exit" do

  print("\n\nRobot Commander\n")
  print("1) Move robot to...")
  print("2) Send robot home")
  print("3) Exit robot ready state\n   (unable to restart remotely)")
  print("4) Report robot position")
  print("5) Exit Robot Commander\n")

  select = tonumber(io.read())

  if prgms[select] == "move to" then

    local mode
    local valid = false

    while valid == false do

      print("\n(R)elative or (absolute) movement?")
      mode = io.read()
  
      if mode == "r" or mode == "R" then
        print("Enter x-distance:")
        xTarget = io.read()
        print("Enter y-distance:")
        yTarget = io.read()
        print("Enter z-distance:")
        zTarget = io.read()
        valid = true

      elseif mode == "a" or mode == "A" then
        print("Enter x-target:")
        xTarget = io.read()
        print("Enter y-target:")
        yTarget = io.read()
        print("Enter z-target:")
        zTarget = io.read()
        valid = true
  
      else
        print("Error: Must enter 'a' or 'r' for mode...\n")
      end

    end

    valid = false
    while valid == false do

      print("\nMode\tx\ty\tz")
      print(tostring(mode).."\t"..xTarget.."\t"..yTarget.."\t"..zTarget)
      print("Send command?  ('y' or 'n')")
      option = io.read()

      if option == "y" or option == "Y" then
        modem.broadcast(100,1,xTarget,yTarget,zTarget,tostring(mode))
        print("Command sent")
        valid = true
  
      elseif option == "n" or option == "N" then
        print("Please re-select command.")
        valid = true

      else
        print("Error: Must enter 'y' or 'n'")

      end

    end

  end

  if prgms[select] == "home" then
    modem.broadcast(port,2)
  end

  if prgms[select] == "unready" then
    modem.broadcast(port,3)
  end

  if prgms[select] == "reportPos" then
    local pos = {}
    modem.broadcast(port,4)
    pos = {event.pull(10,"modem")}
    print(pos[6].." "..pos[7].." "..pos[8])
    print("press any key to continue")
    os.sleep(3)
    io.read()
  end

  if prgms[select] == "exit" then
    print("Exiting Robot Commander...")
    os.sleep(1)
  end

end

post-703-0-93551800-1415918829_thumb.png

Link to post
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Only *possible* problem in the program I see is "local mode" on Line 30. Maybe try local mode = "" or something similar. I've read through this code 6 times and can't find anything else *Possibly* wrong. I don't really understand the local mode part of the code in a sense of debugging, but that looks like the only thing I don't understand.

Try it and tell me the results, thanks.

Link to post
Share on other sites
  • 0

local component = require("component")

local modem = component.modem

local term = require("term")

local event = require("event")

local prgms = {"move to","home","unready","reportPos","exit"}

local port = 100

print("Starting Robot Commander...")

print("Opening port "..port.."...")

modem.open(port)

print("Port "..port.." open: :"..tostring(modem.isOpen(port)))

os.sleep(1)

local select = 0

while prgms[select] ~= "exit" do

print("\n\nRobot Commander\n")

print("1) Move robot to...")

print("2) Send robot home")

print("3) Exit robot ready state\n (unable to restart remotely)")

print("4) Report robot position")

print("5) Exit Robot Commander\n")

select = tonumber(io.read())

if prgms[select] == "move to" then

local mode

local valid = false

while valid == false do

print("\n(R)elative or (absolute) movement?")

mode = io.read()

if mode == "r" or mode == "R" then

print("Enter x-distance:")

xTarget = io.read()

print("Enter y-distance:")

yTarget = io.read()

print("Enter z-distance:")

zTarget = io.read()

valid = true

elseif mode == "a" or mode == "A" then

print("Enter x-target:")

xTarget = io.read()

print("Enter y-target:")

yTarget = io.read()

print("Enter z-target:")

zTarget = io.read()

valid = true

else

print("Error: Must enter 'a' or 'r' for mode...\n")

end

end

valid = false

while valid == false do

print("\nMode\tx\ty\tz")

print(tostring(mode).."\t"..xTarget.."\t"..yTarget.."\t"..zTarget)

print("Send command? ('y' or 'n')")

option = io.read()

if option == "y" or option == "Y" then

modem.broadcast(100,1,xTarget,yTarget,zTarget,tostring(mode))

print("Command sent")

valid = true

elseif option == "n" or option == "N" then

print("Please re-select command.")

valid = true

else

print("Error: Must enter 'y' or 'n'")

end

end

if prgms[select] == "home" then

modem.broadcast(port,2)

end

if prgms[select] == "unready" then

modem.broadcast(port,3)

end

if prgms[select] == "reportPos" then

local pos = {}

modem.broadcast(port,4)

pos = {event.pull(10,"modem")}

print(pos[6].." "..pos[7].." "..pos[8])

print("press any key to continue")

os.sleep(3)

io.read()

end

if prgms[select] == "exit" then

print("Exiting Robot Commander...")

os.sleep(1)

end

end

you seemed to have a wild extra "end" in the middle of your program, this should work but i cant test since i'm not at a mc capable computer
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.