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

Repeat Loop Problem

Started by CitricThunder, 08 August 2013 - 12:34 PM
CitricThunder #1
Posted 08 August 2013 - 02:34 PM
I'm making a way to automate the checking of fuel in a peat engine, and so far it's good. Except for one part.

Here is my startup file

os.loadAPI("functions")
functions.terminal()
functions.gui()
rs.setOutput("bottom", true)
f = fs.open("engines", "r")
engine = f.readLine()
if engine == "on" then
rs.setOutput("back", true)
elseif engine == "off" then
rs.setOutput("back", false)
end
f.close()
functions.cursor(15, 9)
write("1: Turn on engines")
functions.cursor(15, 10)
write("2: Turn off engines")
functions.cursor(1, 1)
write("Engines " ..engine)
while true do
rednet.open("left")
senderId, message, distance = rednet.receive()
repeat
  sleep(.05)
  if senderId == 10 then
   i = 1
   functions.cursor(1, 19)
   write("Engine 1 has " ..message.. " fuel")
   senderId, message, distance = rednet.receive()
   if senderId == 12 then
	i = 2
	functions.cursor(1, 18)
	write("Engine 2 has " ..message.. " fuel")
	senderId, message, distance = rednet.receive()
	if senderId == 13 then
	 i = 3
	 functions.cursor(1, 17)
	 write("Engine 3 has " ..message.. " fuel")
	end
   end
  end
until i == 3
break
end
g = 0
local event, button, X, Y = os.pullEvent("mouse_click")
XY = X..","..Y
repeat
sleep(0.8)
g = g + 1
if XY == "15,9" then
  rs.setOutput("back", true)
  functions.cursor(17, 11)
  write("Engines turned on")
  f = fs.open("engines", "w")
  f.write("on")
  f.close()
  functions.cursor(1, 1)
  write("Engines on ")
  functions.cursor(17, 11)
  sleep(2)
  g = g + 2
  term.clearLine()
elseif XY == "15,10" then
  rs.setOutput("back", false)
  functions.cursor(17, 11)
  write("Engines turned off")
  f = fs.open("engines", "w")
  f.write("off")
  f.close()
  functions.cursor(1, 1)
  write("Engines off ")
  functions.cursor(17, 11)
  sleep(2)
  g = g + 2
  term.clearLine()
end
until g == 250
rs.setOutput("bottom", false)
sleep(1.8)
os.reboot()

Functions file

function terminal()
  term.clear()
  term.setCursorPos(1,1)
end
function cursor(x, y)
  term.setCursorPos(x, y)
end
function gui()
  cursor(38, 1)
  write("Computer ID: " ..os.computerID())
  cursor(36, 19)
  write("MachineTech V1.0")
end
function time()
  time = os.time()
  time = textutils.formatTime(time, false)
  write("The time is "..time)
end

Turtle startup

rednet.open("right")
if rs.getInput("back") == true then
while true do
  turtle.select(1)
  turtle.suck()
  rednet.send(9, turtle.getItemCount(1) )
  turtle.select(1)
  turtle.drop(64)
  sleep(250)
end
elseif rs.getInput("back") == false then
sleep(2)
os.reboot()
end

The computer receives all the engine statuses perfect, but when I go to turn them on/off, where it says "functions.cursor(17, 11)
write("Engines turned on")", it waits the 0.8 seconds at the beginning of the repeat loop and clears the text and rewrites it. Which turns out to "Engines turned on clear() sleep(0.8) Engines turned on ..clear.. sleep(0.8)" I can't figure out what to change to fix this.

Heres a GIF of what happens on my screen-


EDIT–Sorry for the spacing, the forums messed with it
jay5476 #2
Posted 08 August 2013 - 04:53 PM
I advise in your repeat loop where it goes

repeat
--code
until g == 250
Do

repeat
--code
Until g > 250
To avoid it going over 250 and continue
CCJJSax #3
Posted 08 August 2013 - 08:14 PM
I advise in your repeat loop where it goes

repeat
--code
until g == 250
Do

repeat
--code
Until g > 250
To avoid it going over 250 and continue
I'd actually do Until g >= 250. So it'll catch it right at 250 too like he originally wanted.
CitricThunder #4
Posted 10 August 2013 - 03:06 PM
Bump?
CitricThunder #5
Posted 11 August 2013 - 01:17 AM
Solved, moved

local event, button, X, Y = os.pullEvent("mouse_click")
XY = X..","..Y
inside the repeat loop