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

Monitors font work

Started by Dsemagames, 17 July 2015 - 07:01 AM
Dsemagames #1
Posted 17 July 2015 - 09:01 AM
I have a program to control a Big Reactor, if i active the function to show information in a advanced monitor, the computer reboot, but if i desactive, the program works. I wrote another program to see if the monitors work and they show text

This is the code :

[indent=1]
print("running…")

cap = peripheral.find("tile_blockcapacitorbank_name")
mon = peripheral.wrap("right")
–mon = peripheral.find("monitor")
rea = peripheral.find("BigReactors-Reactor")

local numCapacitors = 2
local enciendeA = 50
local apagaA = 95

local energia = 0
local energiaGuardada = 0
local energiaMax = 0
local energiaGuardadaPorCiento = 0
local produccionRF = 0
local usoFuel = 0
local tempCore = 0
local reactorOnline = false

function enciende()
rea.setActive(true)
end

function apaga()
rea.setActive(false)
end

function comma_value(cantidad)
local formateo = cantidad
local swap = false
if formateo < 0 then
formateo = formateo*-1
swap = true
end
while true do
formateo, k = string.gsub(formateo, "^(%d+)(%d%d%d)",'%1%2')
if k == 0 then
break
end
end
if swap then
formateo = "-"..formateo
end
return formateo
end

function muestraEnergia()
mon.clear()
mon.setCursorPos(1,1)
mon.write("Usa de Energia: ")
if energia < 0 then
mon.setTextColor(colors.red)
else
mon.setTextColor(colors.lime)
end
mon.write(comma_value(math.floor(energia)).."RF/T")
mon.setTextColor(colors.white)
mon.setCursorPos(1,2)
mon.write("Energia Almacenada: ")
if energiaGuardadaPorCiento < 55 then
mon.setTextColor(colors.red)
elseif energiaGuardadaPorCiento >= 55 and energiaGuardadaPorCierto <=85 then
mon.setTextColor(colors.yellow)
else
mon.setTextColor(colors.lime)
end
mon.write(energiaGuardadaPorCiento.."%")
mon.setTextColor(colors.white)
mon.setCursorPos(1,3)
mon.write("Este Reactor esta: ")
if reactorOnline then
mon.setTextColor(colors.lime)
mon.write("ENCENDIDO")
else
mon.setTextColor(colors.red)
mon.write("APAGADO")
end
mon.setTextColor(colors.white)
mon.setCursorPos(1,4)
mon.write("Generacion de RedstoneFluix: ")
mon.setTextColor(colors.lime)
mon.weite(comma_value(math.floor(produccionRF)).."RF/T")
mon.setTextColor(colors.white)
mon.serCursorPos(1,5)
mon.write("Temperatura interna: "..math.floor(tempCore).."c")
mon.setCursorPos(1,6)
mon.write("Uso de Combustible: "..usoFuel.."MB/T")
end

function compruebaEnergia()
local energiaTemporal = 0
energiaGuardada = cap.getEnergyStored()
energiaMax = cap.getMaxEnergyStored()
energiaGuardadaPorCiento = math.floor((energiaGuardada/energiaMax)*100)
produccionRF = rea.getEnergyProducedLastTick()
usoFuel = rea.getFuelConsumedLastTick()
usoFuel = math.floor(usoFuel*100)
usoFuel = usoFuel/100
tempCore = rea.getFuelTemperature()
reactorOnline = rea.getActive()
energiaTemporal = cap.getEnergyStored()
sleep(0.1)
energia = (cap.getEnergyStored()-energiaTemporal)/2
energia = energia*numCapacitors
end

function autoReactor()
rea.setAllControlRodLevels(0)
if energiaGuardadaPorCiento < enciendeA then
if not reactorOnline then
enciende()
end
end
if energiaGuardadaPorCiento > apagaA then
apaga()
end
end

function muestraInfo()
compruebaEnergia()
muestraEnergia()
autoReactor()
end

while true do
muestraInfo()
end
[/indent]
flaghacker #2
Posted 17 July 2015 - 12:23 PM
I don't understand your code at all because it isn't english, but I think the problem is that you don't yield.

Yielding is the act of waiting for something (user input, time delay, …). Your code is forced to yield because commputercraft has to run others computers while you're waiting. Computers don't run in parallell, they simply pretend to do so.

When your program doesn't yield, computercraft shuts it down by erroring "too long without yielding" to allow other computers to keep running.

When your code only executes peripheral functions, computercraft can't error out because it has no control over those functions. The code appears to be running just fine, but it prevents other conputers in the world to do anything.

Conputercraft has control over the monitor functions, so as soon as you start using them your program/computer shuts down. Using the monitor functions isn't the cause of the problem, it simply reveals it.

The solution would be to yield in your main loop, you can eg put a sleep (5) in there.
Edited on 17 July 2015 - 10:24 AM
Dsemagames #3
Posted 17 July 2015 - 05:26 PM
Thanks :D/>, but now i have other problem in the same code

Error: 63: attempt to compare __le on nil and number
HPWebcamAble #4
Posted 17 July 2015 - 06:45 PM
Here is line 63:

elseif energiaGuardadaPorCiento >= 55 and energiaGuardadaPorCierto <=85 then

attempt to compare __le on nil and number means you tried to say 'is nil less than or equal to a number?' (le is less then or equal to)

The second variable, 'energiaGuardadoPorCierto', isn't used anywhere else in the program, so it will always be nil.
Dsemagames #5
Posted 27 July 2015 - 07:03 AM
I solve all the errors but the program dont work and now i have a sleep (5) in the main loop
flaghacker #6
Posted 27 July 2015 - 09:19 AM
I solve all the errors but the program dont work and now i have a sleep (5) in the main loop

Specify "don't work". What does it do instead? Error? Blank screen?
Dsemagames #7
Posted 27 July 2015 - 10:08 AM
The computer shut down
flaghacker #8
Posted 27 July 2015 - 10:26 AM
Could you upload your current code to pastebin?
Dsemagames #9
Posted 27 July 2015 - 10:33 AM
http://pastebin.com/kgcsNKx9
flaghacker #10
Posted 27 July 2015 - 10:42 AM
And when does it shut down, does it print anything first?
Dsemagames #11
Posted 27 July 2015 - 11:29 AM
Now works! I change the program to a new computer, and its working. I thing is a bug. Thank for your help ;D
flaghacker #12
Posted 27 July 2015 - 12:21 PM
Now works! I change the program to a new computer, and its working. I thing is a bug. Thank for your help ;D

You can try to reboot the 'broken' computer, maybe that fixes it.
Dsemagames #13
Posted 27 July 2015 - 12:37 PM
The new computer have the same problem ;(