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

4 units to switch on and off with bundled cable

Started by Xixili, 10 April 2013 - 07:02 AM
Xixili #1
Posted 10 April 2013 - 09:02 AM
Dear Minecrafters/Computercrafters,

Im trying to control my nuclear reactor (build up from 4 chambers) separately to turn them on or off.

Im not that good in programming computercraft so thats why im asking help here.
I have managed to turn them on 1 by 1 but if i turn on the 2nd for example the 1st one turns off again.

Maybe you guys can help me how to fix this.

What i wish to achieve:
If i turn on unit 1 and another moment unit 2 then i want that unit 1 stays on for example
also counts for unit 3 and 4.

This is what i have so far:

http://pastebin.com/g2RbsEHZ
Berge #2
Posted 10 April 2013 - 09:31 AM
Use something like this
 redstone.setBundledOutput("back", colors.yellow+colors.orange)
Heres the wiki page on this if you need it. http://computercraft...etBundledOutput
Xixili #3
Posted 10 April 2013 - 10:03 AM
Use something like this
 redstone.setBundledOutput("back", colors.yellow+colors.orange)
Heres the wiki page on this if you need it. http://computercraft...etBundledOutput

Yes i got that working.
But when i turn on i cant turn them off separately anymore
SadKingBilly #4
Posted 10 April 2013 - 10:13 AM
This work?

chambersOn = 0
chamberColors = { colors.green, colors.red, colors.blue, colors.black }
function toggleOutput(toggled)
    if colors.test(chambersOn, toggled) then
	    chambersOn = colors.subtract(chambersOn, toggled)
    else
	    chambersOn = colors.combine(chambersOn, toggled)
    end
end
while true do
    term.clear()
    term.setCursorPos(1, 1)
    print("Which chambers would you like to toggle?")
    chamber = tonumber(read())
    toggleOutput(chamberColors[chamber])
    rs.setBundledOutput("left", chambersOn)
end
EDIT: *cough* Fixed it.
danny_delmax1 #5
Posted 10 April 2013 - 10:50 AM
Since the colors are integers, you could do this to add colors:


if input == "1 on" then
  colorgreen = "on" -- will be used later
  Curcolor = rs.getBundledOutput("back") -- Finds what colors are currently on
  rs.setBundledOutput("back", Curcolor + colors.green) -- sets the currently on colors and the new color on
  print "Unit 1 is ON	"
end

and this to remove them:


if input == "1 off" then
  Curcolor = rs.getBundledOutput("back") -- Finds the color like before
  if colorgreen == "on" -- This will make sure it wont be messed up
	rs.setBundledOutput("back", Curcolor - colors.green) -- turn off the color
	colorgreen = "off" -- Set it back to off- it is off now
	  print "Unit 1 is OFF	"
  else
	print("Unit 1 if already OFF   ") -- tell them its off already
  end
end
Xixili #6
Posted 10 April 2013 - 12:10 PM
Since the colors are integers, you could do this to add colors:


if input == "1 on" then
  colorgreen = "on" -- will be used later
  Curcolor = rs.getBundledOutput("back") -- Finds what colors are currently on
  rs.setBundledOutput("back", Curcolor + colors.green) -- sets the currently on colors and the new color on
  print "Unit 1 is ON	"
end

and this to remove them:


if input == "1 off" then
  Curcolor = rs.getBundledOutput("back") -- Finds the color like before
  if colorgreen == "on" -- This will make sure it wont be messed up
	rs.setBundledOutput("back", Curcolor - colors.green) -- turn off the color
	colorgreen = "off" -- Set it back to off- it is off now
	  print "Unit 1 is OFF	"
  else
	print("Unit 1 if already OFF   ") -- tell them its off already
  end
end
works great!
Only to turn them off i get the error message on rs.setBundledOutput("back", Curcolor - colors.green) turn off the color
danny_delmax1 #7
Posted 10 April 2013 - 12:45 PM
works great!
Only to turn them off i get the error message on rs.setBundledOutput("back",Curcolor- colors.green) turn off the color

What error message do you get?
Engineer #8
Posted 10 April 2013 - 01:48 PM

local white = 1
local orange = 2
local magenta = 4
local lightBlue = 8

local color = 0

if something then 
   color = color + white
elseif somethingelse then
   color = color - white
end

rs.setBundledOutput('side', color >= 0 and color or 0)
use this as your maincode, obviously you need to change it for every color.
SadKingBilly #9
Posted 10 April 2013 - 02:38 PM
[ . . . ]
works great!
Only to turn them off i get the error message on rs.setBundledOutput("back",Curcolor- colors.green) turn off the color
I've fixed my code and it works perfectly now. Just use this code, except change the names of the colors to the colors you're using and make sure their positions match the chamber number in the table.

chambersOn = 0
chamberColors = { colors.green, colors.red, colors.blue, colors.black }
function toggleOutput(toggled)
    if colors.test(chambersOn, toggled) then
	    chambersOn = colors.subtract(chambersOn, toggled)
    else
	    chambersOn = colors.combine(chambersOn, toggled)
    end
end
while true do
    term.clear()
    term.setCursorPos(1, 1)
    print("Which chambers would you like to toggle?")
    chamber = tonumber(read())
    toggleOutput(chamberColors[chamber])
    rs.setBundledOutput("left", chambersOn)
end
It won't work the way you tried to get yours to work (you just enter the chamber number to toggle it, instead of "1 off" or "3 on").
Xixili #10
Posted 10 April 2013 - 09:00 PM
works great!
Only to turn them off i get the error message on rs.setBundledOutput("back",Curcolor- colors.green) turn off the color

What error message do you get?

Sorry im at my job at this moment.
I tested it on 1 unit for now.

When I remove the part

if input == "1" then
Curcolor = rs.getBundledOutput("back") -- Finds the color like before
if coloryellow == "on" -- This will make sure it wont be messed up	 
rs.setBundledOutput("back", Curcolor - colors.yellow) -- turn off the color	 
colorgreen = "off" -- Set it back to off- it is off now	   
print "Unit 1 is OFF  "
else	 
print("Unit 1 if already OFF   ") -- tell them its off already  endend
end
end
Then it works fine.
I type 1 to turn on and to turn of i type 1 again.
So its fine actually




This is what I used

term.clear()
term.setCursorPos(13,1)
term.setTextColor(colors.lightBlue)
print("Reactor 3 Control Terminal")
term.setCursorPos(1,2)
print("---------------------------------------------------")
term.setCursorPos(1,17)
print("---------------------------------------------------")
term.setCursorPos(13,18)
print("Reactor 3 Control Terminal")
term.setTextColor(colors.green)
term.setCursorPos(48,18)
print("V2.1")

while true do
term.setTextColor(colors.orange)
term.setCursorPos(1,3)
term.clearLine()
write "Wich unit do you wish to turn on or off? 1/2/3/4 "
local input = string.lower( read() )

if input == "1" then
  coloryellow = "on" -- will be used later
  Curcolor = rs.getBundledOutput("back") -- Finds what colors are currently on
  rs.setBundledOutput("back", Curcolor + colors.yellow) -- sets the currently on colors and the new color on
  print "Unit 1 is ON   "
end

if input == "1" then
Curcolor = rs.getBundledOutput("back") -- Finds the color like before
if coloryellow == "on" -- This will make sure it wont be messed up	  
rs.setBundledOutput("back", Curcolor - colors.yellow) -- turn off the color	  
colorgreen = "off" -- Set it back to off- it is off now		
print "Unit 1 is OFF  "
else	  
print("Unit 1 if already OFF   ") -- tell them its off already  endend
end
end

if input ==  "exit" then
term.setTextColor(colors.red)
redstone.setBundledOutput("back", 0)
print "Terminal turned off"
break
end
end
Xixili #11
Posted 10 April 2013 - 09:08 PM
Is it also possible to merge the terminal with the monitor ?
This is what I use to control the monitor:


mon = peripheral.wrap("right")
local x = rs.testBundledInput("back", colors.yellow)
while x ~=banana do
yellow = rs.testBundledInput("back", colors.yellow)
gray = rs.testBundledInput("back", colors.gray)
green = rs.testBundledInput("back", colors.green)
brown = rs.testBundledInput("back", colors.brown)
purple = rs.testBundledInput("back", colors.purple)
if yellow == true then
mon.setCursorPos(1,1)
mon.setTextColor(colors.orange)
mon.write(" ** Reactor 3 Core Status **  ")
mon.setCursorPos(1,2)
mon.setTextColor(colors.lightBlue)
mon.write("----------------------------------")
mon.setCursorPos(1,11)
mon.setTextColor(colors.lightBlue)
mon.write("----------------------------------")
mon.setCursorPos(1,12)
mon.setTextColor(colors.orange)
mon.write(" ** Reactor 3 Core Status **  ")
mon.setCursorPos(1,3)
mon.setTextColor(colors.yellow)
mon.write(" Unit 1: ONLINE				 ")
else
mon.setCursorPos(1,1)
mon.setTextColor(colors.orange)
mon.write(" ** Reactor 3 Core Status **  ")
mon.setCursorPos(1,2)
mon.setTextColor(colors.lightBlue)
mon.write("-----------------------------------")
mon.setCursorPos(1,3)
mon.setTextColor(colors.yellow)
mon.write(" Unit 1: OFFLINE	   ")
end
if gray == true then
mon.setCursorPos(1,4)
mon.write(" Unit 2: ONLINE	  ")
else
mon.setCursorPos(1,4)
mon.write(" Unit 2: OFFLINE	   ")
end
if green == true then
mon.setCursorPos(1,5)
mon.write(" Unit 3: ONLINE	    ")
else
mon.setCursorPos(1,5)
mon.write(" Unit 3: OFFLINE	   ")
end
if brown == true then
mon.setCursorPos(1,6)
mon.write(" Unit 4: ONLINE	    ")
else
mon.setCursorPos(1,6)
mon.write(" Unit 4: OFFLINE	   ")
end
if purple == true then
mon.setCursorPos(1,10)
mon.write(" Reactor Temp: Exceeded    ")
else
mon.setCursorPos(1,10)
mon.write(" Reactor Temp: Normal	  ")
end
os.sleep(2)
end
danny_delmax1 #12
Posted 11 April 2013 - 11:21 AM
Honestly, i recommend you replace my solution with TheCoryKid's. It almost always better to use functions, as you will have to re-write the code over and over with my way. Also, both if input == "1" blocks will get called, one after the other. it will never alternate, unless you put another if then statement and combine the code.

As you doing the monitor program and the control program, i'm not that sure of how to do it on one computer.

You could use the parallel api, but that might have the monitor not update while you are inputting into the terminal.
You could run them one after the other, but that will pause one program while it runs the other.

The best way would be to connect the bundled cable to another computer, running the monitor program on a hidden one.
Xixili #13
Posted 11 April 2013 - 08:40 PM
Honestly, i recommend you replace my solution with TheCoryKid's. It almost always better to use functions, as you will have to re-write the code over and over with my way. Also, both if input == "1" blocks will get called, one after the other. it will never alternate, unless you put another if then statement and combine the code.

As you doing the monitor program and the control program, i'm not that sure of how to do it on one computer.

You could use the parallel api, but that might have the monitor not update while you are inputting into the terminal.
You could run them one after the other, but that will pause one program while it runs the other.

The best way would be to connect the bundled cable to another computer, running the monitor program on a hidden one.

Yeah thats how I got it now, 2 computers.

I will try your code tonight and let you know.
theoriginalbit #14
Posted 11 April 2013 - 08:55 PM
Since the colors are integers, you could do this to add colors:

rs.setBundledOutput("back", Curcolor + colors.green)
and this to remove them:

rs.setBundledOutput("back", Curcolor - colors.green)
There is a time where this fails… for example what if green is not on, and then you still minus green, you will mess with all other colours that are on… Or what if green is on already and then you add green again, again it messes with the colours that are already on……… So since the colours are on a binary scale you are better off to use colors.combine and colors.subtract…
Edited on 11 April 2013 - 06:56 PM
Kingdaro #15
Posted 12 April 2013 - 12:42 AM
@TOB That only happens if you don't check for the color's existence. :)/>
theoriginalbit #16
Posted 12 April 2013 - 12:48 AM
@TOB That only happens if you don't check for the color's existence. :)/>
True, but its just good practise to just use colours combine and subtract…
Also streamlines the code, 'cause there is no need to check if it is on or off too.
danny_delmax1 #17
Posted 12 April 2013 - 12:52 AM
@TOB That only happens if you don't check for the color's existence. :)/>
True, but its just good practise to just use colours combine and subtract…
Also streamlines the code, 'cause there is no need to check if it is on or off too.

yea, I had this in my subtracting code:

if colorgreen == "on" -- This will make sure it wont be messed up
But forgot it in my adding code- thanks for pointing that out TheOriginalBIt
Kingdaro #18
Posted 12 April 2013 - 12:53 AM
Debatable. If the color is off, the state checking makes sure no attempt is made to toggle the output, while with combine and subtract, operations are always being made.

In the end, I suppose it's just a matter of preference.
theoriginalbit #19
Posted 12 April 2013 - 01:03 AM
Debatable. If the color is off, the state checking makes sure no attempt is made to toggle the output, while with combine and subtract, operations are always being made.
Well yes but really a bitwise operation is a very cheap calculation… Plus the bitwise operator essentially is doing the comparison… If i had time I would explain how the bitwise operators work, but I'm doing some uni stuff at the moment… and I'm pretty sure that you (Kingdaro) know how they work…
Xixili #20
Posted 12 April 2013 - 11:47 AM
Honestly, i recommend you replace my solution with TheCoryKid's. It almost always better to use functions, as you will have to re-write the code over and over with my way. Also, both if input == "1" blocks will get called, one after the other. it will never alternate, unless you put another if then statement and combine the code.

As you doing the monitor program and the control program, i'm not that sure of how to do it on one computer.

You could use the parallel api, but that might have the monitor not update while you are inputting into the terminal.
You could run them one after the other, but that will pause one program while it runs the other.

The best way would be to connect the bundled cable to another computer, running the monitor program on a hidden one.

I tried and I get an error on line 29:
bios:388: [string "solar"]:29: 'then' expected
Its about this line:
rs.setBundledOutput("back", Curcolor - colors.yellow) -- turn off the color


term.setBackgroundColor(colors.white)
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(colors.lightBlue)
term.setTextColor(colors.white)
print("Solar Array Control Terminal						 ")
while true do
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setCursorPos(1,3)
term.clearLine()
write "Wich array do you wish to turn on or off?"
term.setCursorPos(1,4)
write "Array 1, 2, 3, 4, 5, 6, 7, 8:"
term.setCursorPos(1,5)
write ""
local input = string.lower( read() )
if input == "1 on" then
  coloryellow = "on" -- will be used later
  Curcolor = rs.getBundledOutput("back") -- Finds what colors are currently on
  rs.setBundledOutput("back", Curcolor + colors.yellow) -- sets the currently on colors and the new color on
  print "Unit 1 changed status   "
end
if input == "1 off" then
  Curcolor = rs.getBundledOutput("back") -- Finds the color like before
  if coloryellow == "on" -- This will make sure it wont be messed up
		rs.setBundledOutput("back", Curcolor - colors.yellow) -- turn off the color
		colorgreen = "off" -- Set it back to off- it is off now
		  print "Unit 1 is OFF  "
  else
		print("Unit 1 if already OFF   ") -- tell them its off already
  end
end
if input ==  "exit" then
term.clear()
term.setTextColor(colors.red)
redstone.setBundledOutput("back", 0)
term.setCursorPos(1,1)
print "Terminal shutting down"
sleep(2)
term.setCursorPos(1,1)
print "Its now safe to continue"
break
end
end
danny_delmax1 #21
Posted 12 April 2013 - 01:43 PM
I tried and I get an error on line 29:
bios:388: [string "solar"]:29: 'then' expected
Its about this line:
rs.setBundledOutput("back", Curcolor - colors.yellow) -- turn off the color
-snip-

my solution: (I forgot a "then" one above that line, but this one is better anyway)


if input == "1" then
  Curcolor = rs.getBundledOutput("back") -- Finds what colors are currently on
  if colors.test(Curcolor, colors.yellow) then -- If yellow is currently on then
	Newcolor = colors.subtract(Curcolor, colors.yellow)-- Remove yellow from the on colors
	Message = "Yellow toggled: OFF" -- will be print()ed later
  else -- if its NOT on,
	Newcolor = colors.combine(Curcolor, colors.yellow)-- Add yellow to the on colors
	Message = "Yellow toggled: ON" -- will be print()ed later
  end
  rs.setBundledOutput("back", Newcolors) -- sets the currently on colors and the new color on -- Set the new outputs
  print(Message)
end