Jump to content




Gps Not Working

turtle

  • You cannot reply to this topic
7 replies to this topic

#1 cmckain14

  • Members
  • 22 posts

Posted 25 September 2013 - 11:52 PM

I have building a mining program but the gps is reporting nil even though the debug reports a value.
a = 0
print("Are there GPS satellites in the sky?")
input = read()
if input == "No" then
  error("There must be a GPS system in place to use this program!")
end
if input == "Yes" then
  local x, y, z = gps.locate(2,true)
  y = by --Backup for y
  print(y) --No value 
  if y == nil then
    error("Nope, still lost :(/>/>")
    end
  for i = 1,tonumber(y) do --It says that y is not a number 
    turtle.digDown()
    turtle.down()
    repeat
      if turtle.detect() then
        turtle.dig()
        end
      turtle.turnLeft()
      a = a + 1
     until a > 4
    end
  while by ~= 1 do
    turtle.up()
    by = by - 1
    end
  turtle.select(2)
  turtle.placeDown()
end
Also, could I have some feedback on the code, please?

#2 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 26 September 2013 - 02:01 AM

Before this bit of code runs, what is by set to?
If you never set by, then by is nil. Then you do "y = by" so y is also nil. Then you error because y is nil.

View Postcmckain14, on 25 September 2013 - 11:52 PM, said:

y = by --Backup for y
print(y) --No value 
if y == nil then
  error("Nope, still lost :(/>/>/>/>")
end


#3 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 26 September 2013 - 02:40 AM

One small tidbit of feedback. You have two if statements to check one instance of user input. It is probably better to use 1 if block to check the one input.
input = read()
if input == "no" then
  error("There must be a GPS system in place to use this program!")
elseif input == "yes" then -- or just simply else
  --insert rest of code here
end

In this bit of code its not really a big deal but in the larger scheme of things its a good habit to get into.

#4 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 26 September 2013 - 03:17 AM

View PostLuanub, on 26 September 2013 - 02:40 AM, said:

One small tidbit of feedback. You have two if statements to check one instance of user input. It is probably better to use 1 if block to check the one input.
input = read()
if input == "no" then
  error("There must be a GPS system in place to use this program!")
elseif input == "yes" then -- or just simply else
  --insert rest of code here
end

In this bit of code its not really a big deal but in the larger scheme of things its a good habit to get into.
More importantly though, what happens if they type "banana"?

#5 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 26 September 2013 - 04:36 AM

View Postimmibis, on 26 September 2013 - 03:17 AM, said:

View PostLuanub, on 26 September 2013 - 02:40 AM, said:

One small tidbit of feedback. You have two if statements to check one instance of user input. It is probably better to use 1 if block to check the one input.
input = read()
if input == "no" then
  error("There must be a GPS system in place to use this program!")
elseif input == "yes" then -- or just simply else
  --insert rest of code here
end

In this bit of code its not really a big deal but in the larger scheme of things its a good habit to get into.
More importantly though, what happens if they type "banana"?

Or No/Yes instead of no/yes. Definitely could use some extra error checking/prevention.

It could look something like...
local input = string.lower(read()) --make the var local and convert to lower case
if input == "no" then
  --error
elseif input == "yes" then
--run the code
else
   error("Invalid answer")
end

There are also other ways to accomplish this but hopefully that shows the general idea.

#6 cmckain14

  • Members
  • 22 posts

Posted 26 September 2013 - 08:54 AM

View Postimmibis, on 26 September 2013 - 02:01 AM, said:

Before this bit of code runs, what is by set to?
If you never set by, then by is nil. Then you do "y = by" so y is also nil. Then you error because y is nil.

View Postcmckain14, on 25 September 2013 - 11:52 PM, said:

y = by --Backup for y
print(y) --No value 
if y == nil then
  error("Nope, still lost :(/>/>/>/>/>")
end
But why is y nil when the GPS should define it?

#7 MR_nesquick

  • Members
  • 106 posts
  • LocationNorway

Posted 26 September 2013 - 09:40 AM

by = y == y cord
y = by == nil

your backup is deleting the y cord.

smal derp

#8 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 27 September 2013 - 01:36 AM

View Postcmckain14, on 26 September 2013 - 08:54 AM, said:

View Postimmibis, on 26 September 2013 - 02:01 AM, said:

Before this bit of code runs, what is by set to?
If you never set by, then by is nil. Then you do "y = by" so y is also nil. Then you error because y is nil.

View Postcmckain14, on 25 September 2013 - 11:52 PM, said:

y = by --Backup for y
print(y) --No value 
if y == nil then
  error("Nope, still lost :(/>/>/>/>/>/>/>/>")
end
But why is y nil when the GPS should define it?
Because you made it nil after that.
"by" is nil.
"y = by" sets "y" to whatever "by" is.
Therefore, "y = by" sets "y" to nil (in this program).





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users