Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ComputerCraft script for controlling my RedPower frame castle gate
-- Castle Gate Control
-- Opens and closes a redpower frame gate at the push of a button
local side = "left" -- Bundled cable location
local alarmSide = "back" -- Howler alarm location
function enableWire(color) -- Turn on a colored wire in the bundle without affecting the other colors
local currentSet = rs.getBundledOutput(side)
if not colors.test(currentSet, color) then
currentSet = colors.combine(currentSet, color)
rs.setBundledOutput(side, currentSet)
end
end
function disableWire(color) -- Turn off a colored wire in the bundle without affecting the other colors
local currentSet = rs.getBundledOutput(side)
if colors.test(currentSet, color) then
currentSet = colors.subtract(currentSet, color)
rs.setBundledOutput(side, currentSet)
end
end
function pulseWire(color) -- Turn a wire on and off
enableWire(color)
os.sleep(0.5)
disableWire(color)
os.sleep(0.5)
end
write("== Castle Gate Control ==\n")
-- Reset the redstone
rs.setBundledOutput(side, 0)
rs.setOutput(alarmSide, false)
-- Start with the gate in a closed position
pulseWire(colors.red)
pulseWire(colors.red)
pulseWire(colors.red)
local raised = 0
while true do
local evt, arg = os.pullEvent()
if evt == "redstone" then
local currentSet = rs.getBundledInput(side)
if colors.test(currentSet, colors.yellow) then
if raised == 0 then
write("Opening the gate!\n")
rs.setOutput(alarmSide, true)
os.sleep(1)
pulseWire(colors.white)
pulseWire(colors.white)
pulseWire(colors.white)
os.sleep(1)
rs.setOutput(alarmSide, false)
raised = 1
else
write("Closing the gate!\n")
rs.setOutput(alarmSide, true)
os.sleep(1)
pulseWire(colors.red)
pulseWire(colors.red)
pulseWire(colors.red)
os.sleep(1)
rs.setOutput(alarmSide, false)
raised = 0
end
end
elseif evt == "key" then
if arg == 45 then -- X key pressed on keyboard, exit the program
write("Exit!\n")
break
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.