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

Controlling Turtle with local key-pressed events

Started by michaelcampbell229, 12 July 2017 - 05:57 AM
michaelcampbell229 #1
Posted 12 July 2017 - 07:57 AM
So I have to two scripts. One runs on a Advanced Wireless Pocket Computer and the other on a Advanced Wireless Mining Turtle.
This script runs on the turtle controls movement such as forwards, up, down, turn left, turn right, backwards, break block, and place blocks.
ComputerCode

rednet.open("back")
local disf = 0
local disb = 0
local disr = 0
local disl = 0
local disv = 0
local x = 1

while true do
if x == 1 then
  term.clear()
  term.setCursorPos(1,1)

   print("Height "..disv)
   -- print("Right "..disr)
   -- print("Left "..disl)
   print("Forwards "..disf)
   print("Back "..disb)
   local event, character = os.pullEvent("char")
  if character == "p" and "o" then
   disf = 0
   disb = 0
   disr = 0
   disl = 0
   disv = 0
  end
	if disr == -4 then
	 disr = 0
	end
	if disl == -4 then
	 disl = 0
	end
  if character == "k" then
   x = 2
   rednet.broadcast("controlledroute")
  end
  if character == "w" then
   rednet.broadcast("forward")
	if disb >= 1 then
	 disb = disb-1
	else
	 disb = 0
	 disf = disf+1
	end
  end
  if character == "a" then
   rednet.broadcast("left")
	if disr >= 1 then
	 disr = disr-1
	else
	 disl = disl+1
	 disr = 0
	end
  end
  if character == "s" then
   rednet.broadcast("back")
	if disf >= 1 then
	 disf = disf-1
	else
	 disb = disb+1
	 disf = 0
	end
  end
  if character == "d" then
   rednet.broadcast("right")
	if disl >= 1 then
	 disr = disr-1
	else
	 disl = disl+1
	 disr = 0
	end
  end
  if character == "g" then
   rednet.broadcast("down")
	if disv >= 0 then
	 disv = disv-1
   end
  end
  if character == "h" then
   rednet.broadcast("up")
	if disv >= 0 then
	 disv = disv+1
	end
  end
  if character == "e" then
   rednet.broadcast("placedown")
  end

  if character == "q" then
   rednet.broadcast("placeup")
  end

  if character == "f" then
   rednet.broadcast("break")
  end
end
if x == 2 then
  term.clear()
  term.setCursorPos(1,1)
  print("Controlled")
  local event, character = os.pullEvent("char")

   if character == "k" then
	x = 1
   end
  
   if character == "m" then
	print("YES")
   end
end
end
and this code runs on the turtle. When the turtle receives a message, corresponding to the message sent dependent on a key pressed, it then moves the turtle. I haven't written the code that will actually make the turtle move (based on which options is selected) yet that that shouldn't be too hard.
TurtleCode

rednet.open("right")
term.clear()

while true do
local sender, message, protocol = rednet.receive()
if message == "left" then
turtle.turnLeft()
end
if message == "right" then
turtle.turnRight()
end
if message == "forward" then
turtle.forward()
end
if message == "back" then
turtle.back()
end
if message == "placedown" then
turtle.placeDown()
end
if message == "placeup" then
turtle.placeUp()
end
if message == "up" then
turtle.up()
end
if message == "down" then
turtle.down()
end
if message == "break" then
turtle.dig()
end
----------------------------------
if message == "controlledroute" then
end
----------------------------------
end

So, the turtle will move based on which key is pressed when the screen is showing Height, Forwards, and Back (as expected and planned) as/when variable x == 1. When the "k" key is pressed the screen clears and shows the "Controlled" screen (which that should show when the "k" key is pressed) (and if pressed again will go back to the previous screen/message) but when another key is pressed such as the programmed m key nothing is happening, should pressing the m key not print "YES" ? (and if it is changed to another key, such as l, the same result) Originally i thought that it was because there are two local events but then when on the Controlled screen pressing the k key wouldn't do anything correct?

The idea is when its in the first mode (Height ect) the user can manually control the turtle, when they press k (probably should make it k and something else, but that is easy) the robot goes into "Programmed/Controlled" mode, the user then selects an option such as: option a: dig 16x16x1 meaning that rather than the user having to manually control the robot to dig out area they put it in programmed mode and off it goes based on controls stored/programed on the turtle; but I have hit this wall and I cant progress until this bit is fixed.

Something like this would replace the part that currently says "YES": (this includes the option a, option b, and option c bit) (this bit of code is not tested just an idea as I haven't been able to get past this bit so ignore any code errors in this part)
CodeTheory - read after

if x == 2 then
  term.clear()
  term.setCursorPos(1,1)
  print("Controlled")
  term.setCursorPos(1,4)
  print("Option a: dig 16x16")
  term.setCursorPos(1,6)
  print("Option b: dig 16x64")
  term.setCursorPos(1,8)
  print("Option c: dig 32x64")

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

   if character == "k" then
	x = 1 --return to previous screen
   end
  
  
   if character == "a" then
	term.setTextcolor(color.green)
	term.setCursorPos(1,4)
	print("Option a: dig 16x16")					--makes option/key pressed green
	term.setTextcolor(color.white)
	
	 if character == "y" then
	  rednet.broadcast("small")			   --user presses y to conform
	 end
   end
  
   if character == "b" then
	term.setTextcolor(color.green)
	term.setCursorPos(1,6)
	print("Option b: dig 16x64")					 --makes option/key pressed green
	term.setTextcolor(color.white)
	
	 if character == "y" then
	  rednet.broadcast("medium")	 --user presses y to conform
	 end
   end
  
   if character == "c" then
	term.setTextcolor(color.green)
	term.setCursorPos(1,8)
	print("Option c: dig 32x64")	 --makes option/key pressed green
	term.setTextcolor(color.white)
	
	 if character == "y" then
	  rednet.broadcast("medium")	 --user presses y to conform
	 end
   end
end
KingofGamesYami #2
Posted 12 July 2017 - 02:33 PM
Pressing 'm' does print "YES". However, let's take a look at the sequence of events

-screen is cleared
-cursor set
-"controlled" written
-"m" is pulled as an event
-"YES" is printed
-screen is cleared
-cursor set
-"controlled written"
-waits for another event.
michaelcampbell229 #3
Posted 13 July 2017 - 05:02 AM
Pressing 'm' does print "YES". However, let's take a look at the sequence of events

-screen is cleared
-cursor set
-"controlled" written
-"m" is pulled as an event
-"YES" is printed
-screen is cleared
-cursor set
-"controlled written"
-waits for another event.
Thanks for your response, so it is displaying yes but it is then instantly cleared and "controlled" is then displayed again. Ok thanks, I'll try modify the code so it doesn't look like that, any suggestions that would still provide the functionality that I am trying to achieve?
Lupus590 #4
Posted 13 July 2017 - 12:45 PM
Clear the screen after pulling the event but before printing yes (or doing any other event processing which uses the screen)