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

[Lua][Question] Multiple BundledInput

Started by unlimited, 10 August 2012 - 05:06 PM
unlimited #1
Posted 10 August 2012 - 07:06 PM
hi,
i am trying to have a monitor showing the temperature of my nuclear reactor. so far i have a "MFFS reactor heat monitor server" and 3 "MFFS reactor heat monitor client" connetected to bundledcable colors like this:
white - reactor on
green - reactor 6000+
red - reactor 9000+
black - reactor 11000+

and i have this code on the computer:


local monitorSide = "right"
local cableSide = "bottom"
state = "REACTOR OFF "
heat = "COLD	    "
if peripheral.isPresent(monitorSide) and peripheral.getType(monitorSide) == "monitor" then
  -- redirect terminal output to the monitor
  term.redirect(peripheral.wrap(monitorSide))
else
  -- no monitor, exit
  print("No monitor found")
  return
end
while true do
term.setCursorPos(3,2)
print(state)
term.setCursorPos(4,4)
print(heat)
os.pullEvent("redstone")
  if rs.testBundledInput("bottom", colors.white) == true then
   state = "REACTOR ON  "
  elseif rs.testBundledInput("bottom", colors.white) == false then
   state = "REACTOR OFF "
  elseif rs.testBundledInput("bottom", colors.green) == true then
   heat = "HOT		 "
  elseif rs.testBundledInput("bottom", colors.green) == false then
   heat = "COLD	    "
  elseif rs.testBundledInput("bottom", colors.red) then
   heat = "OVERHEATING"
  elseif rs.testBundledInput("bottom", colors.black) then
   state = "SHUTTINGDOWN"
  else
    state = "failed"  
end
end

my problem is that if for example reactor is 9000+ the colors white, green and red will be on and the computer will detect only the white one.
Is there any way i can have some priority like black > red > green > white so that it will print only the latest signal.


Sorry if i couldn't explain what i want, i've been looking at this for a while now lol
Cranium #2
Posted 10 August 2012 - 07:17 PM
Since lua reads top-down, you may need to have the color tests pointing towards a local variable, and then having the if/then block call back to it.
Like this:

local reactorOnOff=rs.testBundledInput("bottom", colors.white)
if reactorOnOff = true then
state = "Reactor on"
elseif reactorOnOff = false then
state = "Reactor off"
end
Just a quick example, but I hope you get the idea.
unlimited #3
Posted 10 August 2012 - 08:58 PM
hi, i couldn't do it your way but i figured out how to make it work, i used something like


if event == "redstone" and white == true and green == false and red == false and black == false then
state = "REACTOR ON "
heat = "COLD "
elseif event == "redstone" and white == true and green == true and red == false and black == false then
state = "REACTOR ON "
heat = "HOT "

and then i added a sequencer at another color just to refresh the monitor, every time the sequencer gives a signal it checks every colors and prints correctly.

is there any way i can replace the sequencer? the noise is a bit annoying lol
OmegaVest #4
Posted 10 August 2012 - 10:13 PM
You can use a timer event, really. Every time it queues, start a new timer, then do a quickcheck, which is little more than a grab signal, check against last. If anything changed (ie, the bundled cable integer is different), then do a full check.

Obviously, this means you should store the bundled state, which is simple. lastCheck = rs.getBundledInput(side). You can also use the colors api to do checks like testBundledInput does. I think it's just colors.test(bundledGet, color), but I'm not sure.
Cranium #5
Posted 10 August 2012 - 10:25 PM
The colors. test works more like this:
colors.test(rs.getBundledInput("side", color))
You can replace color with colors.combine or subtract, and defince which colors you are testing. It always returns true or false.
Cloudy #6
Posted 11 August 2012 - 01:37 PM
You can use a timer event, really. Every time it queues, start a new timer, then do a quickcheck, which is little more than a grab signal, check against last. If anything changed (ie, the bundled cable integer is different), then do a full check.

Obviously, this means you should store the bundled state, which is simple. lastCheck = rs.getBundledInput(side). You can also use the colors api to do checks like testBundledInput does. I think it's just colors.test(bundledGet, color), but I'm not sure.

Why would you need to use a timer? The redstone event is there for a reason. I generally keep the last bundled cable state around (or at least the last state of the colours I'm monitoring). I then only do something if that variable has changed.

Edit:
I fail for not reading properly ;)/>/> I see what question was being asked now, and how the answer applies.
BigSHinyToys #7
Posted 11 August 2012 - 01:50 PM
in a IF statement the program will only check an ELSEIF when IF fails. The computer is cheeking the list from top to bottom. When the computer finds a ELSEIF that is true then it runs that code and stops looking through the rest of the list. The simples solution is to invert the order of your ELSEIF's so black is first going to white. Meaning when run it checks from black to white looking for the one that is present.

To simplify if back is the last to come on then test it first so the test fails and moves to the next.
Example

local a = 1
local b = 2
local c = 3
print("Example One")
if a == 1 then
	print("A is one")
elseif b == 2 then
	print("B is two")
elseif c == 3 then
	print("C is three")
end
print("Example two")
if c == 3 then
	print("C is three")
elseif b == 2 then
	print("B is two")
elseif a == 1 then
	print("A is one")
end
print("Example three")
if a == 1 then
	print("A is one")
end
if b == 2 then
	print("B is two")
end
if c == 3 then
	print("C is three")
end