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

read EU from MFSU

Started by camadas, 09 January 2014 - 02:30 PM
camadas #1
Posted 09 January 2014 - 03:30 PM
Hi guys here I come again with some question :D/>.

I have this code to read the EU from MFSU and such AKA batbox_


function read_eu(...)
  local eu_stored = 0
  local eu_total = 0
  local args = {...}
  for i=args[1],args[2] do
    eu = peripheral.wrap(batbox_..i)
    eu_stored = eu_stored + bat_box.getEnergyStored()
    eu_total = eu_total + bat_box.getMaxEnergyStored()
  end
  return eu_stored, eu_total
end

and this is the main program:


local moni = peripheral.wrap("left")
moni.setTextScale(1.5)

function mon_limpar()
  moni.clear()
  moni.setCursorPos(1,1)
end

function newLine()
  local_,cY = moni.getCursorPos()
  moni.setCursorPos(1,cY+1)
end

function mj()
  os.loadAPI("energia/read_mj") 
  local mj_avai, mj_total = read_mj.read_energy_cell()
  os.unloadAPI("energia/read_mj")
  perc = math.floor(100*mj_avai/mj_total)
  moni.write("Mj on the House:")
  newLine()
  moni.write("Stored: "..mj_avai.."/"..mj_total.." at "..perc.."%")
  return perc
end

-- to call the API to read the EU from MFSU and such

function eu(...)
  local args = {...}
  os.loadAPI("energia/read_eu")
  local eu_avai, eu_total = read_eu.read_eu(args[1], args[2])
  os.unloadAPI("energia/read_eu")
  perc = math.floor(100*eu_avai/eu_total)
  return eu_avai, eu_total, perc
end

-- Programa principal

while true do
  mon_limpar()
  local mj_p = mj()
  os.loadAPI("energia/red_status")
  local color = red_status.cor_ler("orange")
  if mj_p <= 95 then
    if color == false then
      shell.run("redset","right","orange","true")
    end
  elseif mj_p == 100 then
    if color == true then
      shell.run("redset","right","orange","false")
    end
  end
  os.unloadAPI("energia/red_status")
  -- check EU on engine room
  local eu_e_avai, eu_e_total, perc_e = eu(0,1)
  newLine()
  moni.write("EU from Engine room")
  newLine()
  moni.write("Stored: "..eu_e_avai.."/"..eu_e_total.." at "..perc_e.."%")
  newLine()
  newLine()
  -- check EU on PC room
  local eu_pc_avai, eu_pc_total, perc_pc = eu(2,3)
  moni.write("Stored: "..eu_pc_avai.."/"..eu_pc_total.." at "..perc_pc.."%")
  sleep(5)
end

when i run it, is says that "read_eu:6: attempt to concatenate nill and number."

even if I put the e 0,1 with "0","1" and such i get the same error :/

what should I do here guys
CometWolf #2
Posted 09 January 2014 - 03:37 PM
peripheral.wrap(batbox_..i) won't work. Im guessing you forgot "?
Edited on 09 January 2014 - 02:38 PM
camadas #3
Posted 09 January 2014 - 03:51 PM
peripheral.wrap(batbox_..i) won't work. Im guessing you forgot "?

Strange it should work, i have the same setup for the energy cells


local e_cell = "redstone_energy_cell_"

function read_energy_cell()
  cell_stored = 0
  cell_total = 0
  for i=4,15 do
    cell = peripheral.wrap(e_cell..i)
    cell_stored = cell_stored + cell.getEnergyStored()
    cell_total = cell_total + cell.getMaxEnergyStored()
  end
  return cell_stored, cell_total
end

The diference btw them is just that the i is from 4-15, it will not be a dynamic number
CometWolf #4
Posted 09 January 2014 - 03:54 PM
The difference is that you declare the variable e_cell prior to using it. This does not appear to be the case for batbox_.
camadas #5
Posted 09 January 2014 - 04:02 PM
The difference is that you declare the variable e_cell prior to using it. This does not appear to be the case for batbox_.

I did add this


local bat = "batbox_"
function read_eu(...)
  local eu_stored = 0
  local eu_total = 0
  local args = {...}
  for i=args[1],args[2] do
    eu = peripheral.wrap(bat..i)
    eu_stored = eu_stored + eu.getEnergyStored()
    eu_total = eu_total + eu.getMaxEnergyStored()
  end
  return eu_stored, eu_total
end

before the function, it sort it out, but now i get "read_eu:9: attempt to call nil"
Edited on 09 January 2014 - 03:02 PM
camadas #6
Posted 09 January 2014 - 04:16 PM
Ok fix it :D/>

Just needed to change the openperipheral stuff and done :D/>
robhol #7
Posted 09 January 2014 - 04:56 PM
In the future… code in English. Particularly if you want other people to read it. :P/>