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

Central Command question

Started by ibitmyeye3, 29 April 2012 - 10:08 AM
ibitmyeye3 #1
Posted 29 April 2012 - 12:08 PM
So yesterday I had this great idea to make all the lights in my base run along a bundled cable (RP2) and for each section of my base use a different color cable so I could turn every section on and off individually. I instantly decided I needed some sort of central command for all my base operations. Unfortunately, I have little programming experience so I have no idea where to start. Could I get some help on how to make a program that makes it possible to tell the computer a section of the base and then give it a command such as "lights off"? Is this even possible? I apologize if it is a big question, but I am really fairly helpless as of now. That said, all help will be much appreciated.
libraryaddict #2
Posted 29 April 2012 - 02:49 PM
If you want to control another computer then look up "rednet"
You can use wireless with that.
Alternately you can use bundled cables for each section of the base with each color controlling the lights directly.
Or bundled cables to each computer to turn them on or off.

http://www.computercraft.info/forums2/index.php?/topic/144-useful-tutorials-resources/
You can find some tutorials there.
ibitmyeye3 #3
Posted 29 April 2012 - 06:30 PM
I'd like to be able to control them directly. So say my top floor was blue cable in the bundle, I'd like the computer to know that blue cable meant top floor so when I open the top floor section of the program I could say "lights off" and then it would terminate the blue cable's signal.So on and so forth with the rest of the floors with different colored cables. If possible I'd like a little help with the code as well, or at least a bit of help on how to get started.
libraryaddict #4
Posted 29 April 2012 - 09:15 PM
SideOfBundled = "back"
Rooms = {"top", "back", "mines", "dungeon"}
Operate = {"blue", "red", "yellow", "green"}

	
function testBundle(sSide, sColour)
  local nColour = colors[sColour] or colours[sColour]  
  if type(nColour) ~= "number" then  
	return
  end
  if rs.testBundledInput(SideOfBundled, nColour) then
	return true
  else
	return false
  end
end
function bundle(sSide, sColour, sValue)
  local nColour = colors[sColour] or colours[sColour]
  if type(nColour) ~= "number" then
	error( "No such color" )
	return false
  end
  if sValue then
	rs.setBundledOutput( SideOfBundled, colors.combine( rs.getBundledOutput( sSide ), nColour ) )
  elseif not sValue then
	rs.setBundledOutput( SideOfBundled , colors.subtract( rs.getBundledOutput( sSide ), nColour ) )
  end
end
function writeFile()
  WriteFile = io.open("Bundles", "w")
  for n=1,#Operate do
	if testBundle(SideOfBundled, Operate[n]) then
	  WriteFile:write(Operate[n].."n")
	end
  end
  WriteFile:close()
end
function Clear()
  term.setCursorPos(1,1)
  term.clear()
end
function Welcome()
  Clear()
  print("Welcome to USER's Light Control system")
  write("These lights are turned on:")
  for n=1, #Operate do
	if testBundle(SideOfBundled, Operate[n]) then
	  write(" "..Rooms[n]..",")
	end
  end
  x, y = term.getCursorPos()
  term.setCursorPos(x-1, y)
  term.write(" ")
  print()
  write("Rooms:")
  for p,n in pairs(Rooms) do
	write(" "..n..",")
  end
  x, y = term.getCursorPos()
  term.setCursorPos(x-1, y)
  term.write(" ")
  print()
  write("Toggle: ")
  local Temp = read()
  for p=1, #Rooms do
	if Temp == Rooms[p] then
	  bundle(SideOfBundled, Operate[p], not testBundle(SideOfBundled, Operate[p]))
	end
  end
  writeFile()
end

--Load the previous bundled
if fs.exists("Bundles") then
  TalkToMe = io.open("Bundles", "r")
  for line in TalkToMe:lines() do
	bundle(SideOfBundled, line, true)
  end
  TalkToMe:close()
end
while true do
  sleep(0.1)
  Welcome()
end

Happy now?!
xD
Add a room and color at the same places.
It has formatting problems!
Something you can improve

It can also probably be improved without the sleep()
But I was suffering problems with the display not updating correctly.
I think its due to delaying updates to RP cables so we don't get low fps :)/>/>
ibitmyeye3 #5
Posted 29 April 2012 - 09:46 PM
8D You sir are my new favorite person! Thank you for your help! It works perfectly :)/>/>
libraryaddict #6
Posted 29 April 2012 - 10:16 PM
Oh come on!
You are not even gonna improve the formatting!

The layout screen is terrible!
ibitmyeye3 #7
Posted 30 April 2012 - 08:49 PM
Right now I'm more worried about learning all this first. I'm cross referencing with some lua guides to learn how all this works first. Then I promise I'll improve the formatting :)/>/> You have my word!