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

Loop problem D:

Started by Lord, 15 July 2012 - 01:39 PM
Lord #1
Posted 15 July 2012 - 03:39 PM
So guys, i have a problem with a program i wrote to fix the program of the other program, it's pretty simple
<code>

while true do
os.pullEvent("redstone")
if rs.testBundledInput("left", colors.orange) == false
then rs.setBundledOutput("right", colors.orange)
elseif rs.testBundledInput("left", colors.orange) == true
then rs.setBundledOutput("right", 0)
end

if rs.testBundledInput("left", colors.green) == false
then rs.setBundledOutput("right", colors.green)
elseif rs.testBundledInput("left", colors.green) == true
then rs.setBundledOutput("right", 0)
end

if rs.testBundledInput("left", colors.lightBlue) == false
then rs.setBundledOutput("right", colors.lightBlue)
elseif rs.testBundledInput("left", colors.lightBlue) == true
then rs.setBundledOutput("right", 0)
end

if rs.testBundledInput("left", colors.magenta) == false
then rs.setBundledOutput("right", colors.magenta)
elseif rs.testBundledInput("left", colors.magenta) == true
then rs.setBundledOutput("right", 0)
end
end

Basically, it checks if redstone current is applied and if not it send a redstone signal to a lamp. The problem is, the green one doesn't work D: I tried also with the white, nothing happened, any suggestions?
Thanks :P/>/>

P.s. This is in a function, but i don't think that's the problem D:
Orwell #2
Posted 15 July 2012 - 03:46 PM
You can run another program with:

shell.run('programname')

After that use the 'break' statement to exit the for loop.
Lord #3
Posted 15 July 2012 - 04:31 PM
Yeah i know i can run another program but i don't want to do that, basically i wanna know why this works


if rs.testBundledInput("left", colors.lightBlue) == false
then rs.setBundledOutput("right", colors.lightBlue)
elseif rs.testBundledInput("left", colors.lightBlue) == true
then rs.setBundledOutput("right", 0)
end

and this doesn't


if rs.testBundledInput("left", colors.green) == false
then rs.setBundledOutput("right", colors.green)
elseif rs.testBundledInput("left", colors.green) == true
then rs.setBundledOutput("right", 0)
end
MysticT #4
Posted 15 July 2012 - 04:32 PM
Setting the bundled output to one color removes all the other colors. You need to combine them using colors.combine.
Here's some functions to add or remove colors to the output, but keeps the others:

local function addOutput(sSide, nColors)
  local c = colors.combine(rs.getBundledOutput(sSide), nColors)
  rs.setBundledOutput(sSide, c)
end

local function removeOutput(sSide, nColors)
  local c = colors.subtract(rs.getBundledOutput(sSide), nColors)
  rs.setBundledOutput(sSide, c)
end
Now you can just do it like this:

if rs.testBundledInput("back", colors.green) then
  removeOutput("left", colors.green)
else
  addOutput("left", colors.green)
end
Lord #5
Posted 15 July 2012 - 04:44 PM
GREAT!!!! It works :P/>/> :)/>/> ;)/>/> Thank you 1000000 times ;)/>/> ;)/>/> You're great! Tons of Diamonds for you!

Edit: For making the redstone signal clocking, how should i do? Maybe run another program?
Kolpa #6
Posted 15 July 2012 - 05:08 PM
running an other program is always a bad solution :P/>/> because it creates dependencies and is confusing for the people that use ur program, also its difficult to hand over variables
Lord #7
Posted 15 July 2012 - 05:16 PM
What should i do? Is there a lua command to make a redstone rapide pulse instead of triggering it?
MysticT #8
Posted 15 July 2012 - 05:23 PM
You can set the output on, wait some time, and then turn it off. Like this:

local function pulse(sSide)
  rs.setOutput(sSide, true)
  sleep(1)
  rs.setOutput(sSide, false)
end
It would be easy to adapt it to bundled cable if you need.
Lord #9
Posted 15 July 2012 - 05:28 PM
Okay, this is my code

function3 = function()
while true do
os.pullEvent("redstone")
local function addOutput(sSide, nColors)
  local c = colors.combine(rs.getBundledOutput(sSide), nColors)
  rs.setBundledOutput(sSide, c)
end
local function removeOutput(sSide, nColors)
  local c = colors.subtract(rs.getBundledOutput(sSide), nColors)
  rs.setBundledOutput(sSide, c)
end
if rs.testBundledInput("left", colors.green) == false then
  addOutput("right", colors.green)
else
  removeOutput("right", colors.green)
end
if rs.testBundledInput("left", colors.orange) == false then
  addOutput("right", colors.orange)
else
  removeOutput("right", colors.orange)
end
if rs.testBundledInput("left", colors.magenta) == false then
  addOutput("right", colors.magenta)
else
  removeOutput("right", colors.magenta)
end
if rs.testBundledInput("left", colors.lightBlue) == false then
  addOutput("right", colors.lightBlue)
else
  removeOutput("right", colors.lightBlue)
end
end
end
parallel.waitForAll (function3, function2, function1)

I need to make the redstone triggering looping but if i add this

local function pulse(sSide)
  rs.setOutput(sSide, true)
  sleep(1)
  rs.setOutput(sSide, false)
end

it will light up and shut off one time, i need to loop it, but i arleady looped the whole code, what should i do?
1lann #10
Posted 15 July 2012 - 05:43 PM
I need to make the redstone triggering looping but if i add this

local function pulse(sSide)
  rs.setOutput(sSide, true)
  sleep(1)
  rs.setOutput(sSide, false)
end

it will light up and shut off one time, i need to loop it, but i arleady looped the whole code, what should i do?
XD you made the same mistake I did in my code
It's

local function pulse(sSide)
  rs.setOutput(sSide, true)
  sleep(1)
  rs.setOutput(sSide, false)
  sleep(1)
end

Also you shouldn't loop through functions. Eventually they will cause an ArrayOutOfBounds exception. Use while or for loops instead.
Lord #11
Posted 15 July 2012 - 05:50 PM
I still don't understand how to make light flashing (the redstone signal is attached to some lamps) D:
Orwell #12
Posted 15 July 2012 - 11:46 PM
Yeah i know i can run another program but i don't want to do that, basically i wanna know why this works


if rs.testBundledInput("left", colors.lightBlue) == false
then rs.setBundledOutput("right", colors.lightBlue)
elseif rs.testBundledInput("left", colors.lightBlue) == true
then rs.setBundledOutput("right", 0)
end

and this doesn't


if rs.testBundledInput("left", colors.green) == false
then rs.setBundledOutput("right", colors.green)
elseif rs.testBundledInput("left", colors.green) == true
then rs.setBundledOutput("right", 0)
end

Why did you change the question after I posted my my answer? O.o
Or am I becoming nuts? :/
Lord #13
Posted 16 July 2012 - 08:55 AM
D: I didn't changed my question. I didn't ask the way to run another program, i asked why the green one doesn't work D:

Btw, i figured out how to add MysticT's flashy function in my code, but i need to loop it, the problem is that my code is arleady looping. SO the question is, how do i loop a piece of code in a looped program ( completely independent by the program itself?
<codez


function3 = function()
while true do
os.pullEvent("redstone")
local function addOutput(sSide, nColors)
  local c = colors.combine(rs.getBundledOutput(sSide), nColors)
  rs.setBundledOutput(sSide, c)
  sleep(1)
  rs.setBundledOutput(sSide, 0)
  sleep(1)
end


local function removeOutput(sSide, nColors)
  local c = colors.subtract(rs.getBundledOutput(sSide), nColors)
  rs.setBundledOutput(sSide, c)
end

if rs.testBundledInput("left", colors.green) == false then
  addOutput("right", colors.green)
else
  removeOutput("right", colors.green)
end

if rs.testBundledInput("left", colors.orange) == false then
  addOutput("right", colors.orange)
else
  removeOutput("right", colors.orange)
end

if rs.testBundledInput("left", colors.magenta) == false then
  addOutput("right", colors.magenta)
else
  removeOutput("right", colors.magenta)
end

if rs.testBundledInput("left", colors.lightBlue) == false then
  addOutput("right", colors.lightBlue)
else
  removeOutput("right", colors.lightBlue)
end
end
end

parallel.waitForAll (function3, function2, function1)



Basically i wanna loop this:

local function addOutput(sSide, nColors)
  local c = colors.combine(rs.getBundledOutput(sSide), nColors)
  rs.setBundledOutput(sSide, c)
  sleep(1)
  rs.setBundledOutput(sSide, 0)
  sleep(1)
end

Seprately from the whole code so the lights flash :P/>/>