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

ccSensors with multiple mfsu's getting total energy in all

Started by tunacha, 04 November 2012 - 03:18 PM
tunacha #1
Posted 04 November 2012 - 04:18 PM
Hello everyone,
i am trying to write a program that will detect how many mfsu's are near a sensor and add all the stored energy and max energy and output the ammount, i have most iof it written the only problem is
im getting confused on how to make it do it for every target instead of coding each target manually
Basically i have 100 mfsu's set up around a sensor and im trying to add all the euStorage and max together and output the value every like 1 second without having to hard code each mfsu like this


Targets = sensors.getAvailableTargetsforProbe("right", "EUSensor", "EUStorage")
if #Targets ~= 0 then
  Reading1 = sensors.getSensorReadingAsDict("right", "EUSensor", Targets[1], "EUStorage")
  Reading2  = sensors.getSensorReadingAsDict("right", "EUSensor", Targets[2], "EUStorage")

Any help would be appreciated
Thankyou
CoolisTheName007 #2
Posted 04 November 2012 - 04:36 PM
Store the readings in a table:

Targets = sensors.getAvailableTargetsforProbe("right", "EUSensor", "EUStorage")
Readings={}
for _,target in ipairs(Targets) do
  table.insert(Readings, sensors.getSensorReadingAsDict("right", "EUSensor", target, "EUStorage")
end
remiX #3
Posted 04 November 2012 - 06:38 PM
This is how I did it


total_max_storage = 0
total_curr_storage = 0
local maxStorage = {}
local currStorage = {}

Targets = sensors.getAvailableTargetsforProbe("right",dataSensor,dataProbe)
    for i = 1, #Targets do
        dataReadings = sensors.getSensorReadingAsDict("right",dataSensor,Targets[i],dataProbe)
        table.insert(maxStorage, tonumber(dataReadings.maxStorage))
        table.insert(currStorage, tonumber(dataReadings.energy))
        table.insert(tier, tonumber(dataReadings.tier))
    end
    cp(1, 1, 1)
    for i = 1, #maxStorage do
        total_max_storage = total_max_storage + maxStorage[i]
    end
    for i = 1, #currStorage do
        total_curr_storage = total_curr_storage + currStorage[i]
    end

Works great :D/>/>