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

How to use more than one Sensor in a Program

Started by Andy586, 13 January 2013 - 09:17 PM
Andy586 #1
Posted 13 January 2013 - 10:17 PM
This is my current code getting the sensors but it only seems to allow me to find one sensor. How do I locate more than one sensor in the same program. The blocked out code was tests to see if I was finding the other sensors to no avail. The names of the sensors attactched to the box are euSensor, dadEuSensor, ScrapSensor.

I've tried

myEuSen = data["euSensor"]
dadEuSen = data["dadEuSensor"]
scrapSen = data["ScrapSensor"]

and

myEuSen = data[1]
dadEuSen = data[2]
scrapSen = data[3]


control = sensors.getController()
data = sensors.getSensors(control)

myEuSen = data["euSensor"]
dadEuSen = data["dadEuSensor"]
scrapSen = data["ScrapSensor"]

--Sen = data[1]

--write(tostring(myEuSen).."\n")
--write(tostring(Sen["euSensor"]).."\n")
--write(tostring(Sen["dadEuSensor"]).."\n")

--MyEuProbe
data = sensors.getProbes(control,myEuSen)
myEuProbe = data [3]

--dadEuProbe
data = sensors.getProbes(control,dadEuSen)
dadEuProbe = data[3]

--scrapProbe
data = sensors.getProbes(control,scrapSen)
scrapProbe = data[3]

write(tostring(myEuProbe).."\n"..tostring(dadEuProbe).."\n"..tostring(scrapProbe).."\n")

theoriginalbit #2
Posted 13 January 2013 - 11:44 PM
Do the following after line 2 and comment out everything below (so we don't get errors and other prints) and tell me what prints out…

for k, v in pairs( data ) do
  print( k.." "..v )
end

EDIT: I also suggest that you pick better names for your variables… you keep overriding the data variable, it may be part of your problem…
Edited on 13 January 2013 - 10:45 PM
Andy586 #3
Posted 13 January 2013 - 11:50 PM
Nothing printed out. :S

Ran exactly

control = sensors.getController()
data = sensors.getSensorInfo(control)

for k, v in pairs( data ) do
print( k.." "..v )
end


Edit DERP This is what printed:
1 Sensor


But there are 3 sensors connected to it. The Control block even says there's three.
theoriginalbit #4
Posted 14 January 2013 - 12:11 AM
the output "1 Sensor" tells me that at index 1 there is a value Sensor (which I'm going to assume is a table) so that being said now try this

print( type( data[1] )
and if it prints table do this and tell me the output, else tell me what it prints instead of table


for k, v in pairs( data[1] ) do
  print( k.." "..v )
end

Sorry about this long process, ccSensors isn't my best area of expertise…
Andy586 #5
Posted 14 January 2013 - 12:14 AM
Simply says string


I think I found the problem.

I ran a simple test like what I have here on single player.

One controller.
Three Sensors named Test1, Test2, Test3.

Code as follows.


control = sensors.getController()
data = sensors.getSensors()
print(data[1])
print(data[2])
print(data[3])

Output:
Test1
Test2
Test3
>

I did the same thing again on Multiplayer.

Code as follows.


control = sensors.getController()
data = sensors.getSensors()
print(data[1])
print(data[2])
print(data[3])

Output:
Sensor


disk>

Two blank lines at the bottom. I guess it's just bugged for multiplayer. :S
theoriginalbit #6
Posted 14 January 2013 - 12:44 AM
very well could be… maybe start using a different mod pack with openCCSensors… sorry couldn't have told you that from the start…
Andy586 #7
Posted 14 January 2013 - 12:47 AM
Oh it's alright, I was just playing around with it anyway. I can just use the IC2 screen for the energy values, I was just hoping I could code it to all be on one screen. It works fine with one sensor. Ah well, maybe in future updates. :)/>
theoriginalbit #8
Posted 14 January 2013 - 01:03 AM
ccSensors hasn't had an update in ages…
Andy586 #9
Posted 14 January 2013 - 01:03 AM
Oh well…then I guess no sensors for me then xD
theoriginalbit #10
Posted 14 January 2013 - 01:06 AM
There is openCCSensors… Look in the peripherals section… It gets a lot if updates it seems…
Andy586 #11
Posted 14 January 2013 - 01:10 AM
I wouldn't even know where to begin getting that working on my server. I simply downloaded the Tekkit server and started going at it. Other than the basic properties, whitelist, and ops configs I have no idea. xD Anyway thanks for the help. Maybe I'll look into openCCSensors. Either way, have a good morning, afternoon, evening, or night.
theoriginalbit #12
Posted 14 January 2013 - 03:20 PM
Hey I was just thinking while reading this … have you tried putting more control module thingies on other sides of the computer? since the one module wont work with many sensors…
all the getSensors function needs is a side param…
Andy586 #13
Posted 14 January 2013 - 04:30 PM
That'd probably work, but then the max for the computer then is 5, if you don't want a disk drive, but if you do then the max would be 4. I'll run some tests to see if that'll work and get back on here.
theoriginalbit #14
Posted 14 January 2013 - 04:34 PM
That'd probably work, but then the max for the computer then is 5, if you don't want a disk drive, but if you do then the max would be 4. I'll run some tests to see if that'll work and get back on here.
Well after testing thats 5 different modules though, as it seems each sensor has about a radius of 4-5 blocks of detection. so it can see a lot of stuff
Andy586 #15
Posted 14 January 2013 - 05:47 PM
Seems to have worked, and I was thinking about it you might be able to get more sensors attached with a wireless modem. Haven't tested that, not sure how the side would work for a wireless controller if it is even possible.

(Edit: Just checked there is no way to put a wireless modem on a controller, so the max sensors for one computer would be 5 or 4 if you want a monitor or a disk drive.)

Here's my completed code and the output.


--Adds together the values of our table down below.
function sum(values)
local sum = 0

for k,v in pairs(values) do
  sum = sum + v
end
return sum
end

--Defines the patern for gathering the totals from the formated data.
local pattern = "^(%d+)%*(%a+%.?%a+)@(%d+)"

--Defines the sides of each of the controlers.
local mySide = "bottom"
local dadSide = "left"
local scrapSide = "back"

--Defines Sen as string Sensor. *All sensors in multiplayer are named Sensor, and cannot be changed*
local Sen = "Sensor"

--Defines the probe names for each type of sensor used. [3] Is searching for the third line in the array.

--mySide, and dadSide are the same type of probe. (IC2 Probe, searching for the EU data of our MFEs)
local EUProbe = sensors.getProbes(mySide, Sen)[3]
local itemProbe = sensors.getProbes(scrapSide, Sen)[3]

--Defines the targets for our readings.

--My MFE, target one is my batbox, I dont want that.
local myTarget = sensors.getAvailableTargetsforProbe(mySide, Sen, EUProbe)[2]

--My Dads only MFE in the area.
local dadTarget = sensors.getAvailableTargetsforProbe(dadSide, Sen, EUProbe)[1]

--Only chest in range.
local scrapTarget = sensors.getAvailableTargetsforProbe(scrapSide, Sen, itemProbe)[1]


--Loops for continuous data gathering and printing.
while true do

--Defines a table for gathering the values of each stack of scrap.
local ScrapCount = {}

--Gathers readings from each sensor.
local myData = sensors.getSensorReadingAsDict(mySide, Sen, myTarget, EUProbe)
local dadData = sensors.getSensorReadingAsDict(dadSide, Sen, dadTarget, EUProbe)
local scrapData = sensors.getSensorReadingAsDict(scrapSide, Sen, scrapTarget, itemProbe)

--Gathers values for the table.
for i = 1, #scrapData do
  local amount, item, slot = string.match(scrapData[i], pattern)
  if item == "item.itemScrap" then
   table.insert(ScrapCount, amount)
  end
end

--Readies the terminal for printing.
term.clear()
term.setCursorPos(1,1)

write("Available Energy:")
term.setCursorPos(1,3)

write("My MFE:  "..tostring(myData.energy).." : "..tostring(myData.maxStorage))
term.setCursorPos(1,4)

write("Dad MFE: "..tostring(dadData.energy).." : "..tostring(dadData.maxStorage))
term.setCursorPos(35,1)

local total = sum(ScrapCount)
write("Scrap in Chest: "..tostring(total))
sleep(1)
end

Output:
Spoiler
theoriginalbit #16
Posted 14 January 2013 - 05:53 PM
you could have an Ad-Hoc setup where other computers get the data and send it all to that one computer for displaying…


and cool :)/>