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

Bundled Cabled Timer

Started by CazadoraMala, 04 August 2013 - 04:35 PM
CazadoraMala #1
Posted 04 August 2013 - 06:35 PM
Well, i've veen trying several ways to do this without using RP2 timers, but i don't find the way to do it. I'm trying to make a program that acts like a timer with bundled cables, so i can make some lamps to light in a repetitive ay, but not at the same time. I'll put an example with some pics to explain myself better.

What i want to do is to light for example, white lamp, then black lamp, and repeat this for 20 seconds, and then after those 20 seconds, start to light red lamp and after it the blue lamp.

Spoiler
Repeat lighting those lights for 20 seconds, and after those 20 seconds, the other two.

I don't have the codes i have tryed with because i made all of them on a server, if i can get them back (or make them again) i'll post them.
GamerNebulae #2
Posted 04 August 2013 - 07:33 PM
It would actually be kind of easy to do that. You need the sleep() function and the rs.setBundledOutput() function. You can read more about the Redstone API on the wiki of computercraft. If you know some math, you know how quadratic functions work. I understood from your forum post that you already knew something about ComputerCraft. In case you're not that good at math. I'll explain it to you. How it works is like this:

A number to the power of 0 always equals 1. A number to the power of one equals itself. A quadratic number is the number times itself. For example: 2^2 = 4. While a number to the power of 3 equals this. Example given: 2*2*2 = 8). It's like multiplicating it each time. If you would use a for-loop that goes from the number 0 to the number of 3, you would be settled.


--#Feel free to edit this code to your likings
for x = 0,3 do
  rs.setBundledOutput(2^x, side) --#It will choose the number to the power of x
  sleep(20)
end
rs.setBundledOutput(0, side)

I hope this helped. Sorry if something is messed up, I made this on my phone.
CazadoraMala #3
Posted 05 August 2013 - 08:47 AM
I already know how to make bundled cables work, what i want to do, is two timers in one same program. For 20 seconds, alternate signals between white and black, and then another 20 seconds, but with red and blue. I've been thinking, but i don't know if the idea i have will function. Make 2 different programs, and then, the first one acts as timer for the first 2 colors, and after 20 seconds, it changes to the other 2.
GamerNebulae #4
Posted 05 August 2013 - 09:40 AM
To get this clear and put in steps, you want to do this:
alternate every 20 seconds between white and black and at the same time, doing that with red and blue?
CazadoraMala #5
Posted 05 August 2013 - 01:01 PM
no, what i want to do, is, during 20 seconds, altern between white and black (1 second send signal to white, 1 sec black, 1 sec white…), and after those 20 seconds, change to red and blue (1 second send signal to red, 1 to blue, 1 to red, 1 to blue…)
Kingdaro #6
Posted 05 August 2013 - 01:19 PM
Simple.


while true do
  for i=1, 10 do
    -- output to white lamp
    sleep(1)

    -- output to black lamp
    sleep(1)
  end
  for i=1, 10 do
    -- output to red lamp
    sleep(1)

    -- output to blue lamp
    sleep(1)
  end
end

Each for loop sleeps for one second twice, and therefore, doing it 10 times, should last 20 seconds for each alternating cycle.
CazadoraMala #7
Posted 05 August 2013 - 01:53 PM
that code seems to make lamps to switch each one for 10 secs. First the white one, then the black one, then the red… I want to alternate for 20 secs between white and black, and after that, between red and blue.
Kingdaro #8
Posted 05 August 2013 - 01:57 PM
Have you actually tried the code? In each for loop, it switches to one lamp, sleeps for one second, then the other, and sleeps for another second. That's two seconds for each loop, and each loop lasts 20 seconds.
CazadoraMala #9
Posted 05 August 2013 - 02:22 PM
I've copied and pasted the code, but it doesn't work. I have bundled cables conected to each lamp (white with white, black with black…)


Spoiler


EDIT:

I have tryed to change the code to make the different bundled cables work, i left it like this, but still doesn't work

while true do
  for i=1, 10 do
    -- rs.setBundledOutput(back, colors.white)
    sleep(1)

    -- rs.setBundledOutput(back, colors.black)
    sleep(1)
  end
  for i=1, 10 do
    -- rs.setBundledOutput(back, colors.red)
    sleep(1)

    -- rs.setBundledOutput(back, colors.blue)
    sleep(1)
  end
end
Edited on 05 August 2013 - 12:35 PM
Cranium #10
Posted 05 August 2013 - 02:42 PM
You forgot to add the functions actually turning on the cables. The actual code of course would be as follows:

local side = --#PLEASE PUT THE SIDE HERE
while true do
  for i=1, 10 do
	rs.setBundledOutput(side, colors.white)
	sleep(1)

	rs.setBundledOutput(side, colors.black)
	sleep(1)
  end
  for i=1, 10 do
	rs.setBundledOutput(side, colors.red)
	sleep(1)

	rs.setBundledOutput(side, colors.blue)
	sleep(1)
  end
end
CazadoraMala #11
Posted 05 August 2013 - 02:47 PM
local side must be the one where the signal outputs?
Cranium #12
Posted 05 August 2013 - 02:53 PM
Yes. local just makes that variable local to that program only, side is the name of the variable, and then you just put in "top", "back", or whatever side you are using for it.

EDIT: Ensure that the side you are entering has quotes around it, either single ( ' ) or double ( " ) quotes.

DOUBLE EDIT: Also, starting a line with a double hyphen ( – ) will comment out that entire line, and code on that line will not be run and is 'skipped over' at runtime.
Remove those lines to run the code.
Edited on 05 August 2013 - 12:57 PM
CazadoraMala #13
Posted 05 August 2013 - 03:17 PM
Oks, thank you all, i finally got it working!

local side="back"
while true do
  for i=1, 10 do
		rs.setBundledOutput(side, colors.white)
		sleep(1)

		rs.setBundledOutput(side, colors.red)
		sleep(1)
  end
  for i=1, 10 do
		rs.setBundledOutput(side, colors.black)
		sleep(1)

		rs.setBundledOutput(side, colors.blue)
		sleep(1)
  end
end


Edit:
If i want it to work only when the computer receives redstone signal, i have to add this?

whlile rs.getInput(top)=true do
Kingdaro #14
Posted 05 August 2013 - 03:37 PM
That might give you some trouble…but it can be done.

It's not as simple as just adding that line to the top, because the cycling will still be going, and will only stop until all of the loops have finished. Once it stops, the while loop quits, and can't be turned on again.

The most feasible way I can think of is checking for the redstone signal each time you switch lights, then breaking out of the for loops so it doesn't do any cycling if this is true. The script would look something like this:

local side="back"
while true do
  for i=1, 10 do
    if not rs.getInput('top') then break end
    rs.setBundledOutput(side, colors.white)
    sleep(1)

    if not rs.getInput('top') then break end
    rs.setBundledOutput(side, colors.red)
    sleep(1)
  end
  for i=1, 10 do
    if not rs.getInput('top') then break end
    rs.setBundledOutput(side, colors.black)
    sleep(1)

    if not rs.getInput('top') then break end
    rs.setBundledOutput(side, colors.blue)
    sleep(1)
  end
  sleep(0.5) --# this line is added so the loop doesn't freak out when it's off
end

A copious amount of typing, but it'd work.
CazadoraMala #15
Posted 05 August 2013 - 03:47 PM
that code makes the program run while a redstone signal is received, or it makes the program run until the last black/blue loop finishes with one only signal?
Kingdaro #16
Posted 05 August 2013 - 03:58 PM
It runs while a signal is received.
CazadoraMala #17
Posted 05 August 2013 - 04:28 PM
and how would it be if i wanted to make the program runwith one only signal?
GamerNebulae #18
Posted 05 August 2013 - 06:27 PM
and how would it be if i wanted to make the program runwith one only signal?

Could you clarify that?
CazadoraMala #19
Posted 05 August 2013 - 06:32 PM
for example, send a signal with a button to the computer, and then, with that short signal, the computer makes all the cicle
GamerNebulae #20
Posted 06 August 2013 - 04:55 AM
If I understood this right, you want to click a button and then make it run through the program, should be very easy. Using a while loop it will wait for a redstone signal and if it gets one, it will run through the program:


local side="back"
while true do
  event = os.pullEvent()
  if event == "redstone" then
	for i=1, 10 do
	  if not rs.getInput('top') then break end
	  rs.setBundledOutput(side, colors.white)
	  sleep(1)
	  if not rs.getInput('top') then break end
	  rs.setBundledOutput(side, colors.red)
	  sleep(1)
	end
	for i=1, 10 do
	  if not rs.getInput('top') then break end
	  rs.setBundledOutput(side, colors.black)
	  sleep(1)
	  if not rs.getInput('top') then break end
	  rs.setBundledOutput(side, colors.blue)
	  sleep(1)
	end
	sleep(0.5) --# this line is added so the loop doesn't freak out when it's off
  end
end

EDIT: Fixed a bug
Kingdaro #21
Posted 06 August 2013 - 10:10 AM
@GamerNebulae, your script's missing an end to the "if event == 'redstone' then".
GamerNebulae #22
Posted 06 August 2013 - 12:22 PM
@GamerNebulae, your script's missing an end to the "if event == 'redstone' then".

Sorry, made this on the forums so the chances are high I'm missing a minority like that.
CazadoraMala #23
Posted 07 August 2013 - 01:11 PM
Thanks you all! I've given thanks to all comments