Jump to content




Controlling multiple peripherals at once.


  • You cannot reply to this topic
3 replies to this topic

#1 codec

  • New Members
  • 1 posts

Posted 08 June 2015 - 08: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()


#2 HDeffo

  • Members
  • 214 posts

Posted 08 June 2015 - 01: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.

#3 flaghacker

  • Members
  • 655 posts

Posted 08 June 2015 - 02: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.

#4 HDeffo

  • Members
  • 214 posts

Posted 08 June 2015 - 02: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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users