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

Help with printing numbers on monitor

Started by bubblesayoyo, 02 February 2014 - 09:42 PM
bubblesayoyo #1
Posted 02 February 2014 - 10:42 PM
So I've got a program that reads my energy cell and either turns on or off redstone signals to turn engines on or off. And I want to 'print' out some info onto a monitor for easier reading right. I've got the code working great when it prints on the computer as seen in the first picture.
[attachment=1460:2014-02-02_22.42.48.png]

The numbers read correctly, however when I send it over to the monitor it comes out as decimals and I can't figure it out?!

[attachment=1461:2014-02-02_22.42.43.png]

Here is my program. Thanks in advance for any help that you can give!


mon = peripheral.wrap("left")
cell = peripheral.wrap("cofh_thermalexpansion_energycell_0")
local monw, monh = mon.getSize()
pCount = 0
mon.setTextScale(1)
function getES()
eStored = cell.getEnergyStored("top")
eMax = cell.getMaxEnergyStored("top")
eTemp = (eStored/100)
eTempOne = eMax - (eTemp * 10)
end
function writeLineOne()
mon.clear()
mon.setCursorPos(1,1)
storedLength = tostring(eStored)
mon.setCursorPos((monw-(#storedLength + 16))/2,3)
mon.setTextColor(128)
mon.write("Energy Stored:  ")
mon.setTextColor(16)
mon.write(eStored)
end
function writeLineTwo()
countLength = tostring(pCount)
mon.setCursorPos((monw-(#countLength + 17))/2,6)
mon.setTextColor(128)
mon.write("Program Cycles:  ")
mon.setTextColor(2048)
mon.write(pCount)
end

function writeLineThree()
if (eStored < eTempOne) or (eStored == 0) then
  countRed = 2
else
  countRed = 3
end
mon.setCursorPos((monw-(countRed + 11))/2,9)
mon.setTextColor(128)
mon.write("Redstone:  ")
mon.setTextColor(16384)
getES()
if (eStored < eTempOne) or (eStored == 0) then
  mon.write("ON")
else
  mon.write("OFF")
end
end
function monitorPrint()
writeLineOne()
writeLineTwo()
writeLineThree()
end
function clearColor()
term.clear()
term.setCursorPos(1,1)
term.setTextColor(1)
end
function checkEngine()
while true do
  getES()
  pCount = pCount + 1
  if (eStored < eTempOne) or (eStored == 0) then
   redstone.setOutput("back", true)
   monitorPrint()
   clearColor()
   write("Energy Stored:  ")
   term.setTextColor(16)
   print(eStored)
   term.setTextColor(1)
   write("Program Cycles: ")
   term.setTextColor(2048)
   print(pCount)
   term.setTextColor(1)
   write("Redstone:	   ")
   term.setTextColor(16384)
   print("ON")
  else
   redstone.setOutput("back", false)
   monitorPrint()
   clearColor()
   write("Energy Stored:  ")
   term.setTextColor(16)
   print(eStored)
   term.setTextColor(1)
   write("Program Cycles: ")
   term.setTextColor(2048)
   print(pCount)
   term.setTextColor(1)
   write("Redstone:	   ")
   term.setTextColor(16384)
   print("OFF")
  end
  sleep(3)
end
end
function waitForEsc()
while true do
		 local e, key = os.pullEvent("key")
		 if key ~= 45 then
				 -- It wasn't escape
				 return true
		 end
end
end
parallel.waitForAny(function() checkEngine() end,function() waitForEsc() end)
ingie #2
Posted 02 February 2014 - 11:09 PM
try wrapping tostring( number ) around your numbers when you print them - which is a good practice anyway whenever you don't need them to be numbers as that can stop a nil from causing an error.

the problem is probably that the system is thinking [possibly wrongly in your case] that it's not got enough space to display the number as is, so it changes to scientific notation…
bubblesayoyo #3
Posted 02 February 2014 - 11:32 PM
Awesome! I knew it would be something easy, just couldn't figure it out. Weird that it would print 'normal' on the computer screen and come out in decimals on the monitor. Best part was, I was already 'toStringing' my variables so no big changes needed. Thanks for the help!
ingie #4
Posted 03 February 2014 - 12:36 AM
hope it's the correct answer :)/>

also, you can simply use, in your instance… parallel.waitForAny( checkEngine, waitForEsc )
you don't need to wrap the functions inside anonymous functions, you can just pass the function variable…