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

Controlling multiple peripherals at once.

Started by codec, 08 June 2015 - 07:58 AM
codec #1
Posted 08 June 2015 - 09:58 AM
Im trying to make a simple system for turning on and off 12 big reactor turbines and am having trouble with loops, peripherals and functions. I am wanting it to find any turbine on the network and control it. Here is what i have so far but it doesn't work. No errors or anything, just nothing happens.

local turbine = {}
turbine = {peripheral.find("BigReactors-Turbine_")}

for i=1, #turbine do
   turbine[i] = peripheral.wrap(turbine[i])
end

for i = 1, #turbine do
turbine[i].setActive(false)
end

And here is my first attempt at it. Be warded tho, this is literally my first program. How ever it works fine.


turbine1 = peripheral.wrap("BigReactors-Turbine_1")
turbine2 = peripheral.wrap("BigReactors-Turbine_2")
turbine3 = peripheral.wrap("BigReactors-Turbine_3")
turbine4 = peripheral.wrap("BigReactors-Turbine_4")
turbine5 = peripheral.wrap("BigReactors-Turbine_5")
turbine6 = peripheral.wrap("BigReactors-Turbine_6")

function draw_text_term(x, y, text, text_color, bg_color)
	    term.setTextColor(text_color)
	    term.setBackgroundColor(bg_color)
	    term.setCursorPos(x,y)
	    write(text)
end

term.clear()

function run()

	    draw_text_term(1, 1, "Multi Turbine Control v1							 ", colors.black, colors.blue)
	    draw_text_term(1, 3, "Use the following commands:", colors.lime, colors.black)
	    draw_text_term(1, 4, "on - Turns all turbines on.", colors.white, colors.black)
	    draw_text_term(1, 5, "off - Turns all turbines off.", colors.white, colors.black)
	    draw_text_term(1, 6, "coilon - Engages all coils.", colors.white, colors.black)
	    draw_text_term(1, 7, "coiloff - Disengages all coils.", colors.white, colors.black)
	    term.setCursorPos(1,10)
	    term.setBackgroundColor(colors.black)
	    term.setTextColor(colors.lime)
	    act = read()

	    if act == "off" then
	    turbine1.setActive(false) turbine2.setActive(false) turbine3.setActive(false) turbine4.setActive(false) turbine5.setActive(false) turbine6.setActive(false)
	    draw_text_term(1, 10, "All turbines off.", colors.lime, colors.black)
	    sleep (1.0)
	    end

	    if act == "on" then turbine1.setActive(true) turbine2.setActive(true) turbine3.setActive(true) turbine4.setActive(true) turbine5.setActive(true) turbine6.setActive(true)
	    draw_text_term(1, 10, "All turbines on.", colors.lime, colors.black)
	    sleep (1.0)
	    end


	    if act == "coilon" then turbine1.setInductorEngaged(true) turbine2.setInductorEngaged(true) turbine3.setInductorEngaged(true) turbine4.setInductorEngaged(true) turbine5.setInductorEngaged(true) turbine6.setInductorEngaged(true)
	    draw_text_term(1, 10, "All coils on.", colors.lime, colors.black)
	    sleep (1.0)
	    end

	    if act == "coiloff" then turbine1.setInductorEngaged(false) turbine2.setInductorEngaged(false) turbine3.setInductorEngaged(false) turbine4.setInductorEngaged(false) turbine5.setInductorEngaged(false) turbine6.setInductorEngaged(false)
	    draw_text_term(1, 10, "All coils off.", colors.lime, colors.black)
	    sleep (1.0)
	    end

term.clear()
run()
end
run()
HDeffo #2
Posted 08 June 2015 - 02:23 PM
The problem here is the name

turbine = {peripheral.find("BigReactors-Turbine_")}
BigReactors-Turbine_ isn't a peripheral name the proper name would be BigReactors-Turbine

The underscore is causing it to return nothing. Besides that just a quick glance at the code it should work correct.
flaghacker #3
Posted 08 June 2015 - 03:24 PM
The problem is also here:


for i=1, #turbine do
   turbine[i] = peripheral.wrap(turbine[i])
end

The table returned by peripheral.find already contains "wrapped" peripherals, so you can't and don't need to wrap them again.
HDeffo #4
Posted 08 June 2015 - 03:48 PM
Ah yeah apologies like I had mentioned I only managed time to briefly glance the rest of the code. That would also be a problem :P/>