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

Mining Turtle with Monitors

Started by The_Cat, 01 December 2014 - 08:34 PM
The_Cat #1
Posted 01 December 2014 - 09:34 PM
Hi,

I made this simple mining program that tunnels and it record information such as the percentage it is done.

I want to send this information to a computer (Which i have done) and then display it to a monitor (which isnt work). When i display the info on the monitor the percentage done is not updating.

Code for the turtle:
rednet.open("right")

function gravelSides()
while turtle.detect() == true do
  turtle.dig()
  sleep(1)
end
end

function placeTorch() --Places a torch every 5 blocks (Need to put torches in slot 2)
if x == 5 then
  turtle.up()
  turtle.turnLeft()
  turtle.forward()
  turtle.dig()
  turtle.select(2)
  turtle.place()
  turtle.select(1)
  turtle.back()
  turtle.turnRight()
  turtle.down()
  x = 0
end
end

function dig() --Digs a 3 x 2
turtle.digUp()
turtle.turnLeft()
turtle.dig()
gravelSides()
turtle.up()
turtle.dig()
gravelSides()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
gravelSides()
turtle.down()
turtle.dig()
gravelSides()
turtle.turnLeft()
turtle.dig()
gravelSides()
placeTorch()
turtle.forward()
x = x + 1
end

function resetTerm() --Resets the GUI
term.clear()
term.setCursorPos(1,1)
end

function checkFuel() --Checks if you have enough fuel
resetTerm()
print("Checking Fuel...")
sleep(2)
if turtle.getFuelLevel() > minFuelAmount then
  resetTerm()
  print("You have Enough fuel.")
  print("Current fuel: "..turtle.getFuelLevel())
  print("Starting...")
  sleep(3)
else
  while (true) do
   turtle.refuel(1)
   resetTerm()
   print("Waiting for fuel...")
   print("Current fuel: "..turtle.getFuelLevel())
   print("Amount of fuel needed: "..minFuelAmount-turtle.getFuelLevel())
   if turtle.getFuelLevel() > minFuelAmount then
	print("Have enough fuel.")
	print("Starting...")
	sleep(4)
	break
   end
  end
end
end


function completed()
done = w / tunnelLength * 100
end

x = 0
w = 0
write("Enter the length of the tunnel: ")
tunnelLength = read()
minFuelAmount = 4 * tunnelLength + tunnelLength
checkFuel()
resetTerm()
for i = 1,tunnelLength do
resetTerm()
print("Digging...")
completed()
rednet.send(4, "Tunnel is: "..done.."% completed.")
print("Tunnel is: "..done.."% completed.")
w = w + 1
dig()
resetTerm()
print("Digging...")
completed()
rednet.send(4, "Tunnel is: "..done.."% completed.") --4 is the ID of the computer
print("Tunnel is: "..done.."% completed.")
end

for o = 0,tunnelLength do
turtle.back()
end

On the monitor i have a code that is on a never ending loop:
while (true) do
rednet.open("back")
id, message = rednet.receive()
local monitor = peripheral.wrap("top")
term.clear()
term.setCursorPos(1, 1)
print("Turtle: "..id.."\n"..message)
monitor.write(message)
end

So basicly i would like to know how to print text on a monitor that is updating insync with the computer.

Thanks.
KingofGamesYami #2
Posted 01 December 2014 - 11:45 PM
Try this:


local function display( id, message ) --#make a function
  term.clear() --#clear
  term.setCursorPos( 1, 1 ) --#set cursor
  print( "Turtle: " .. id .. "\n" .. message ) --#print message
end

local origin = term.current() --#save default term
local monitor = peripheral.wrap( "top" ) --#wrap monitor
rednet.open( "back" ) --#open rednet
while true do
  local id, message = rednet.receive() --#get message
  display( id, message ) --#display on term
  term.redirect( monitor ) --#redirect to monitor
  display( id, message ) --#display on monitor
  term.redirect( origin ) --#redirect back to term
end
The_Cat #3
Posted 02 December 2014 - 01:47 AM
Try this:


local function display( id, message ) --#make a function
  term.clear() --#clear
  term.setCursorPos( 1, 1 ) --#set cursor
  print( "Turtle: " .. id .. "\n" .. message ) --#print message
end

local origin = term.current() --#save default term
local monitor = peripheral.wrap( "top" ) --#wrap monitor
rednet.open( "back" ) --#open rednet
while true do
  local id, message = rednet.receive() --#get message
  display( id, message ) --#display on term
  term.redirect( monitor ) --#redirect to monitor
  display( id, message ) --#display on monitor
  term.redirect( origin ) --#redirect back to term
end

Worked like a charm, Thanks.