Jump to content




Redpulse with Bundled Cables


5 replies to this topic

#1 Yusunoha

  • Members
  • 8 posts

Posted 13 September 2012 - 09:21 PM

Hello all. today I decided to jump in the deep and try using computercraft to actually write codes for the first time.
I already have something I want to do, and I tried reading some tutorials, but I don't really have the right idea yet on how to write a code.

to explain it really simple, ontop of the computer is a block with a button, on the back is a bundled cable, leading to 3 colored cables, white, orange and magenta.
what I want to have the program do is when it receives a redstone pulse ontop, through the button, I want the white cable to switch on, and the orange cable to pulse 3 times.
if the button gets pressed again, the white cable switches off, and the magenta cable gets pulsed 3 times, and then the program waits for a redstone pulse to switch on white again, and pulse orange 3 times.

what I am trying to do is using redpower frames to have a hidden door with behind a nether portal, that gets activated/deactivated through an igniter when the hidden door opens/closes.

I know there's a program to use redpulse with, but I don't know how to use it with bundledcables.
I hope someone can help me with this :)/>

#2 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 13 September 2012 - 09:51 PM

Ok, first welcome to the forum :P/>
Now, to make set bundled cable output you use the setBundledOutput function from the redstone api (rs api for short):
redstone.setBundledOutput(side, colors)
-- or
rs.setBundledOutput(side, colors)
So, if you want to make a pulse, you need to turn the color you want on, then off. For that you need to use sleep to wait some time:
rs.setBundledOutput(side, colors) -- turn on
sleep(time) -- wait some time
rs.setBundledOutput(side, colors) -- turn off
sleep(time) -- this is so the output gets to update before making a new pulse
You simply put that in a function and you can call it whenever you want from the program:
local function pulse(side, colors, time)
  rs.setBundledOutput(side, colors)
  sleep(time / 2)
  rs.setBundledOutput(side, 0) -- 0 means no colors, so it turns the output off
  sleep(time / 2)
end

pulse("back", colors.orange, 1) -- make a 1-second pulse on the back side with orange color
Now, that will work, but if that will turn off all the other colors off. That's when rs.getBundledOutput comes into play, it tells you what colors are currently on, so you can keep them on when you make the pulse:
rs.getBundledOutput(side)
But we also need to add to the active colors the ones we want to turn on, so we use the combine function from the colors api to do that:
local blackAndWhite = colors.combine(colors.white, colors.black)
So, we combine that with the pulse function so it keeps the previous output on and also turns the new ones on:
local function pulse(side, colors, time)
  local current = rs.getBundledOutput(side)
  rs.setBundledOutput(side, colors.combine(current, colors))
  sleep(time / 2)
  rs.setBundledOutput(side, current)
  sleep(time / 2)
end

Ok, thats all about making the pulse. Now to wait for the button to be pressed you need to wait for a "redstone" event using os.pullEvent:
os.pullEvent("redstone")
(There's a tutorial about os.pullEvent if you want to know more about it)
And then you check if the input is on with:
rs.getInput(side)
To make it keep checking you have to use a loop:
while true do
  -- do something
end
(There's other kind of loops, but this is better in this case)

Now, you'll need some flags to know what to do (if the door is already open you need to close it, not open it again). So I made a program that should work, if there's something that you don't understand, just ask.

Finished program:
local sButtonSide = "top"
local sOutputSide = "back"

local function pulse(side, nColors, time)
  local current = rs.getBundledOutput(side)
  rs.setBundledOutput(side, colors.combine(current, nColors))
  sleep(time / 2)
  rs.setBundledOutput(side, current)
  sleep(time / 2)
end

local lastInput = false
local bOpen = false

while true do
  os.pullEvent("redstone")
  local input = rs.getInput(sButtonSide)
  if input ~= lastInput then
	if input then
	  bOpen = not bOpen
	  if bOpen then
		rs.setBundledOutput(sOutputSide, colors.white)
		pulse(sOutputSide, colors.orange, 1)
	  else
		rs.setBundledOutput(sOutputSide, 0)
		pulse(sOutputSide, colors.magenta, 1)
	  end
	end
	lastInput = input
  end
end

Wow, this is a long post :)/>
Hope you understand it. Happy Coding! :D/>

#3 Yusunoha

  • Members
  • 8 posts

Posted 14 September 2012 - 07:13 AM

wow, I would have never been able to write that all myself, a big thanks to you for the help, thanks :)/>

#4 KaoS

    Diabolical Coder

  • Members
  • 1,508 posts
  • LocationThat dark shadow under your bed...

Posted 14 September 2012 - 07:22 AM

Nice code MysticT, you just need to add count functionality to the pulse function so it can be used 3 times easily

local sButtonSide = "top"
local sOutputSide = "back"
local function pulse(side, nColors, time, count)
  local current = rs.getBundledOutput(side)
  for i=1,tonumber(count) or 1 do
   rs.setBundledOutput(side, colors.combine(current, nColors))
   sleep(time / 2)
   rs.setBundledOutput(side, current)
   sleep(time / 2)
  end
end
local lastInput = false
local bOpen = false
while true do
  os.pullEvent("redstone")
  local input = rs.getInput(sButtonSide)
  if input ~= lastInput then
	    if input then
		  bOpen = not bOpen
		  if bOpen then
			    rs.setBundledOutput(sOutputSide, colors.white)
			    pulse(sOutputSide, colors.orange, 1, 3)
		  else
			    rs.setBundledOutput(sOutputSide, 0)
			    pulse(sOutputSide, colors.magenta, 1, 3)
		  end
	    end
	    lastInput = input
  end
end


#5 wgcfatalahot

  • New Members
  • 3 posts

Posted 02 October 2012 - 09:46 PM

MysticT, I have a question:
I'm trying to make the whole Revenge Note Block Instrumental using ComputerCraft and Bundled Cables. At the beginning it has A# and G at the same time, and it repeats that three times before playing different notes, is there a way to get the Insulated Wires for A# and G to turn on at the same time, then off at the same time WITHOUT having any of the other Insulated Wires turn on, then have A# and G turn on at the same time again etc. (In short, how do I reset the output so I can repeat notes?)

#6 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 02 October 2012 - 10:32 PM

View Postwgcfatalahot, on 02 October 2012 - 09:46 PM, said:

MysticT, I have a question:
I'm trying to make the whole Revenge Note Block Instrumental using ComputerCraft and Bundled Cables. At the beginning it has A# and G at the same time, and it repeats that three times before playing different notes, is there a way to get the Insulated Wires for A# and G to turn on at the same time, then off at the same time WITHOUT having any of the other Insulated Wires turn on, then have A# and G turn on at the same time again etc. (In short, how do I reset the output so I can repeat notes?)
You can use rs.setBundledOutput and colors.combine to set multiple colors at the same time, the set it to 0 to turn it off.
Take a look at my Noteblock API (link in my signature), it let's you do that, there's even a program to convert songs from Noteblock Studio. If you don't want to use it, you can still look how it works and make your own version.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users