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

Update monitor every 3 seconds

Started by Arendium, 01 December 2013 - 06:24 AM
Arendium #1
Posted 01 December 2013 - 07:24 AM
Hej i would like to update my monitor every 3 seconds
I have copied some code:


os.startTimer(1)
while true do
term.setCursorPos(3,8)
term.clearLine()
m.write("Hitze: ")
m.write(nR.getHeat())
sleep(2)
os.pullEvent("timer")
os.startTimer(1)
end

But it don't work i don't see anything

My complete code:


local function padLeft(str, w)
  return string.rep(" ", w - #str) .. str
end

local function findPeripheral(_type)
  for _,name in pairs(peripheral.getNames()) do
    if peripheral.getType(name) == _type then
	  return peripheral.wrap(name)
    end
  end
  error("Fehler: ".._type.." Kann nicht gefunden werden!", 0)
end

term.clear()
m = peripheral.wrap("left")
nR = findPeripheral("nuclear_reactor")
term.redirect(m)
m.setTextScale(1)

term.setCursorPos(3,1)
m.setBackgroundColor(colors.green)
print("Ein")
term.setCursorPos(3,2)
m.setBackgroundColor(colors.red)
print("Aus")
m.setBackgroundColor(colors.black)
term.setCursorPos(3,8)
m.write("Hitze: ")
m.write(nR.getHeat())
term.setCursorPos(3,9)
m.write("Von: ")
m.write(nR.getMaxHeat())
while true do
event,side,x,y = os.pullEvent()
  if event == "monitor_touch" then
    if y == 1 then
    term.setCursorPos(3,4)
    term.clearLine()
    m.setBackgroundColor(colors.green)
    print("   ")
    redstone.setOutput("back",true)
    end
    if y == 2 then
    term.setCursorPos(3,4)
    term.clearLine()
    m.setBackgroundColor(colors.red)
    print("   ")
    redstone.setOutput("back",false)
    end
    term.setBackgroundColor(colors.black)
  end
end
os.startTimer(1)
while true do
term.setCursorPos(3,8)
term.clearLine()
m.write("Hitze: ")
m.write(nR.getHeat())
sleep(2)
os.pullEvent("timer")
os.startTimer(1)
end
Bubba #2
Posted 01 December 2013 - 10:37 AM
Don't copy code you don't understand. That code doesn't make any sense. You will want to do something along these lines:

while true do
  --#Update the monitor here
  sleep(3)
end

It's as simple as that.
Arendium #3
Posted 01 December 2013 - 11:10 AM
That don't work it don't update in the reactor there is many heat but not at the monitor
my complete code:
(Update is at the end)

local function padLeft(str, w)
  return string.rep(" ", w - #str) .. str
end

local function findPeripheral(_type)
  for _,name in pairs(peripheral.getNames()) do
    if peripheral.getType(name) == _type then
	  return peripheral.wrap(name)
    end
  end
  error("Fehler: ".._type.." Kann nicht gefunden werden!", 0)
end

term.clear()
m = peripheral.wrap("left")
nR = findPeripheral("nuclear_reactor")
term.redirect(m)
m.setTextScale(1)

term.setCursorPos(3,1)
m.setBackgroundColor(colors.green)
print("Ein")
term.setCursorPos(3,2)
m.setBackgroundColor(colors.red)
print("Aus")
m.setBackgroundColor(colors.black)
term.setCursorPos(3,8)
m.write("Hitze: ")
m.write(nR.getHeat())
term.setCursorPos(3,9)
m.write("Von: ")
m.write(nR.getMaxHeat())
while true do
event,side,x,y = os.pullEvent()
  if event == "monitor_touch" then
    if y == 1 then
    term.setCursorPos(3,4)
    term.clearLine()
    m.setBackgroundColor(colors.green)
    print("   ")
    redstone.setOutput("back",true)
    end
    if y == 2 then
    term.setCursorPos(3,4)
    term.clearLine()
    m.setBackgroundColor(colors.red)
    print("   ")
    redstone.setOutput("back",false)
    end
    term.setBackgroundColor(colors.black)
  end
end
while true do
    term.setCursorPos(3,8)
    term.clearLine()
    m.write("Hitze: ")
    m.write(nR.getHeat())
    sleep(3)
end
Bomb Bloke #4
Posted 01 December 2013 - 04:23 PM
while true do -- This will loop as long as "true" is true (ie indefinitely).
  print("1")
end
while true do -- This code won't begin, as the code above it won't stop.
  print("2")
end

Assuming you want the "monitor_touch" event stuff, forget about sleep and use something like:

local myTimer = os.startTimer(1)
while true do
  event,side,x,y = os.pullEvent()
  if event == "monitor_touch" then
    -- Stuff
  elseif event == "timer" and side == myTimer then
    -- More stuff.
    myTimer = os.startTimer(1)
  end
end
Edited on 01 December 2013 - 03:26 PM