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

[OpenCCsensors] reading Size from Inventory sensor

Started by unix60959, 26 October 2013 - 09:23 AM
unix60959 #1
Posted 26 October 2013 - 11:23 AM
[OpenCCsensors] reading Size from Inventory sensor

Application:

Im going to quickly describe what I am trying to do as there may be a better way than my code. Im using pulverizes to crush cobble to sand and using redstone engines to feed the cobble out of chests. The problem with this system is that eventually the redstone engines heat up and feed the cobble to fast causing the pulverizes to fill up even while operating at full power. My solution to this problem is to monitor the number of items inside the pulverizer and then set up a condition which will turn off and on the redstone engines accordingly. Bassically just keep the pulverizer stocked but not overflow allowing for the maximum efficiency. Right now i have to physically turn on and off a switch when the engines get to warm.

Problem:

The problem im having is getting the value in Size to store in a variable to be used in a condition. Here is the code i have so far.


os.loadAPI("ocs/apis/sensor")
local mySensor = sensor.wrap("bottom")
local key = mySensor.getTargets()
local info = mysensor.getTargetDetails(key)
local size = info.Size
print(size)

I can use the sensorview program to view the targets, which there are about 4 or 5 of them since i have lots of chests, and other things in the vicinity. I place the sensor right next to the pulverizer and it is located at 0,0,-1. when i run the code above I get an error at line 5: attempt to call index ? (a nil value). I feel like im not populating info correctly or the Size parameter is missing. but im not sure to getTargetDetails for a specific target?

the rest of the code would be simple and something like this:


if size < 20 then
    redstone.setOutput("right", true)
end
if size > 55 then
    redstone.setOutput("right", false)
end


Thanks
Lyqyd #2
Posted 26 October 2013 - 04:24 PM
See if this works better:


os.loadAPI("ocs/apis/sensor")
local mySensor = sensor.wrap("bottom")
local info = mysensor.getTargetDetails("0,0,-1")
local size = info.Slots[1].Size
print(size)
unix60959 #3
Posted 26 October 2013 - 07:33 PM
Yes! that works! Thanks a lot for the help.

ps
i plan to show some of my projects and things soon
unix60959 #4
Posted 26 October 2013 - 09:57 PM
Here was my final code:



os.loadAPI("ocs/apis/sensor")

function getSize()
local mySensor = sensor.wrap("bottom")
local info = mySensor.getTargetDetails("0,0,-1")
local size = info.Slots[1].Size
return size
print(size)
end

while true do
local size
size = getSize()
 if size < 20 then
    redstone.setOutput("right", true)
 end
 if size > 55 then
    redstone.setOutput("right", false)
 end
 sleep(1)
 print(size)
end
Bomb Bloke #5
Posted 26 October 2013 - 10:47 PM
Note that when a function returns, it stops execution. That "print" statement in your getSize() function will never be performed.

You might consider defining mySensor as local before defining the function. That way there's no need to wrap it over and over again every time the function is called.
unix60959 #6
Posted 27 October 2013 - 01:23 AM
ah yes, i took that out… must have missed that when i copy and paste