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

Trouble Loading Monitor on Startup

Started by heimdall116, 20 April 2014 - 06:29 AM
heimdall116 #1
Posted 20 April 2014 - 08:29 AM
Hi there,

I've got an elusive bug in my code, and could use some advice from a pro. :)/> This program connects to an ME storage unit and displays its capacity on a monitor.

Problem:
My monitor doesn't work on the first run of the code after initially placing the computer, but does on all subsequent runs. The code doesn't return any errors. Curiously, if I add print(text) inside the while loop, the correct display is repeatedly printed in the terminal until I break - so the ME is wrapped correctly and the code is iterating properly, but the monitor just isn't communicating??

Code

-- Simple LUA script to watch the ME Storage unit
-- Heimdall116, 4/6/14

--Load adjacent peripherals
local ME = peripheral.wrap("left")
local monitor = peripheral.wrap("top")

--Prep the monitor
monitor.clear()
local mX, mY = monitor.getSize()
monitor.setTextScale(3)

--Start periodic updates
while true do
  free = ME.getFreeBytes()
  total = ME.getTotalBytes()
  local text = (total-free..' / '..total)
 
  --Display it on the monitor
  monitor.clear()
  local x = math.floor(mX/2 - 4)
  local y = mY/2-1
  monitor.setTextColor(colors.white)
  monitor.setCursorPos(x,y)
  monitor.write("ME Usage:")
 
  if free/total < 0.25 or total == 0 then
    cCode = colors.red
  elseif free/total < 0.5 then
    cCode = colors.yellow
  else
    cCode = colors.green
  end
 
  local x = math.floor(mX/2 - string.len(text)/2)
  monitor.setCursorPos(x,y+1)
  monitor.setTextColor(cCode)
  monitor.write(text)
 
  monitor.setTextColor(colors.yellow)
  monitor.setCursorPos(1,mY)
  monitor.write(' - H116 ')
  monitor.setTextColor(colors.blue)
  monitor.write(' (Go Blue!)')
 
  os.sleep(3)
end

Thanks very much in advance!
OReezy #2
Posted 21 April 2014 - 01:18 AM
This is the problem here:
local mX, mY = monitor.getSize()
monitor.setTextScale(3)
The size is based on the scale so when you increase the scale to 3, the values you saved for size before changing the text scale are incorrect from then on.This means its likely printing off screen. When you restart the program without rebooting, the text scale is still 3 so it gets the correct size the second time around. Switching those two lines of code should fix your problem.
Edited on 20 April 2014 - 11:19 PM