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

Broadcast a variable and controlling a wireless robot

Started by michaelcampbell229, 16 May 2016 - 12:14 AM
michaelcampbell229 #1
Posted 16 May 2016 - 02:14 AM
I have a created a program that allows the user to control a wireless robot using aswd (left, back, forwards, and right) g for up and d for down when holding a pocket computer - eventually it will also place and break blocks. That worked fine, the robot moved based on the message received, but then I thought I would make a segment of code that would make the robot run in a pattern such as creating a platform 10x10. I tried making this code but I got stuck and I dont know how. Currently the robot code runs fine (no errors) but the pocket computer shows the name of the that was run last, its meant to clear and then print Height, Direction, Forwards and Back. Any ideas on how to fix it?

The code in blue segment (near the bottem) I think should toggle between allowsqaure yes and no, the turtle did receive the signal and printed "start making square" but doesn't seem to toggle between, any ideas? (but since changing/adding it hasnt received or send anything)

this is my code currently (pocket computer) code, everything new is labled (if the new code was removed it would just move and print proper text (Height, Direction, Forwards and Back)

rednet.open("back")

local disf = 0
local dire = 0
local disv = 0

while true do

 local sender, message, protcol = rednet.receive() --- new


if message == "squareon" then					  ----- new from here
allowsquareYN = 1
end
if message == "squareoff" then
allowsquareYN = 0
end																  ----- to here

term.clear()
term.setCursorPos(1,1)
print("Height "..disv)
print("Direction "..dire)
print("Forwards "..disf)
print("Back "..disf)

print("Allow Square = "..allowsquareYN)				  ---- new

local event, character = os.pullEvent("char")

if character == "w" then
  rednet.broadcast("forward")
   end

if character == "a" then
  rednet.broadcast("left")
   end
  
if character == "s" then
  rednet.broadcast("back")
  end

if character == "d" then
  rednet.broadcast("right")
  end

if character == "g" then
  rednet.broadcast("godown")
  end

if character == "h" then
  rednet.broadcast("goup")
  end

if character == "u" then
  rednet.broadcast("allowsquaremsg")
  end
if character == "j" then
  rednet.broadcast("tenxten")
	end
end

And the robots code, again everything new is labled ect.


rednet.open("left")

while true do

local sender, message, protcol = rednet.receive()

if message == "left" then
	turtle.turnLeft()
	print("moving left")
	end

if message == "right" then
	turtle.turnRight()
	print("moving right")
	end
if message == "back" then
	turtle.back()
	print("going backwards")
	end
if message == "forward" then
	turtle.forward()
	print("moving forwards")
	end

if message == "goup" then
	turtle.up()
	print("moving up")
	end

if message == "godown" then
	turtle.down()
	print("moving down")
	end

if message == "allowsquaremsg" then									   ---- blue segment
print("starting make square")
   if allowsqare == 1 then
	 allowsquare = 0
	 print("false therefore 0")
	 rednet.broadcast("squareoff") -- this line is also new
[color=#000080]  end

	if allowsquare == 0 then
	  allowsquare = 1
	  print("true or 1")
	  rednet.broadcast("squareon")  -- this line is also new
[color=#000080]	end
  end
																								    ---- end of blue segment
---------------Preset Movements--------  
while allowsquare == 1 do

local tenxtenf = 0
local tenxtens = 0
local tenxtennum = 0
local tenxtendir = 1

if message == "tenxten" then
print("moving in a 10x10")
  while tenxten == 0 do
	turtle.forward(9)
	tenxtenf = 1
	end

  else
   turtle.turnRight()
   turtle.forward()
   turtle.turnRight()
   tenxtenf = 0
   tenxtennum = 1
  
  end
end
end
I tried changing the font colour but because its code it didnt like it
valithor #2
Posted 16 May 2016 - 02:20 AM
From a quick skim (might have missed something):


if message == "allowsquaremsg" then
  print("starting make square")
  if allowsqare == 1 then --# variable is misspelt
	allowsquare = 0
	print("false therefore 0")
	rednet.broadcast("squareoff")
  end

  if allowsquare == 0 then
	allowsquare = 1
	print("true or 1")
	rednet.broadcast("squareon")
  end
end

Other than that one typo, the other thing I notice is that the allowsquare variable is never defined in the turtle program. If it is never defined it will never equal 0 or 1 in your checks. Assign it to either 0 or 1 at the top of your program and try it again.

edit:

This might also be a unintended side effect, but once allowsquare equals 1 the program will be stuck in the while loop at the bottom, since there is no where in that loop that changes the variable that the loop is based on the loop will never end. This also means that no new messages will ever be received since the check is outside of the while loop the program is stuck in.
Edited on 16 May 2016 - 12:25 AM
michaelcampbell229 #3
Posted 16 May 2016 - 02:42 AM
From a quick skim (might have missed something):

Other than that one typo, the other thing I notice is that the allowsquare variable is never defined in the turtle program. If it is never defined it will never equal 0 or 1 in your checks. Assign it to either 0 or 1 at the top of your program and try it again.

edit:

This might also be a unintended side effect, but once allowsquare equals 1 the program will be stuck in the while loop at the bottom, since there is no where in that loop that changes the variable that the loop is based on the loop will never end. This also means that no new messages will ever be received since the check is outside of the while loop the program is stuck in.

Thanks for the reply, didnt notice that typo, thanks. About the variable allowsquare not being defined I added a line at the top of the program that makes allowsquare = 0, and once the turtle has finished making the platform it will make allowsquare equal to 0 therefore exiting the loop.

After these two small changes the code still doesn't work on the pocket computer.