unter_hosen, on 22 September 2017 - 12:54 AM, said:
when the program runs for the first time I would like it to pulse some cables in a particular order, which I think works with the code below.
Probably not. First off, the sleep() function expects numbers, and by wrapping your 1's in quotes you're passing them in as strings (text representations). If you want to sleep for a second for eg, you'd generally do sleep(1).
Then there's the point that you're setting your output to the same thing multiple times in a row. For example, say you do this:
rs.SetBundledOutput("back", 1)
sleep(1)
rs.SetBundledOutput("back", 1)
sleep(1)
rs.SetBundledOutput("back", 1)
sleep(1)
This'll turn on the white wire and leave it on for a full three seconds. Is that how long you wanted your "pulse" to last? Or did you intend to perform three separate pulses over this time? Assuming the latter case, you might instead do something like:
rs.SetBundledOutput("back", 1) --# Enable white only.
sleep(0.5)
rs.SetBundledOutput("back", 0) --# Disable all output.
sleep(0.5)
rs.SetBundledOutput("back", 1)
sleep(0.5)
rs.SetBundledOutput("back", 0)
sleep(0.5)
rs.SetBundledOutput("back", 1)
sleep(0.5)
rs.SetBundledOutput("back", 0)
sleep(0.5)
This could be simplified by bundling the repeated bits into a
function at the top of your code:
local function pulse( colour )
rs.SetBundledOutput("back", colour)
sleep(0.5)
rs.SetBundledOutput("back", 0)
sleep(0.5)
end
... which would allow you to later do:
pulse(1)
pulse(4)
pulse(4)
pulse(1)
pulse(1)
pulse(1)
pulse(4)
Now let's say you want the code to be able to pause until a certain colour activates. We might build another function for that, making use of a
"while" loop which repeats so long as the colour is off:
local function waitForColour( colour )
while not rs.testBundledInput( "back", colour ) do --# So long as the desired colour isn't active on the back...
os.pullEvent("redstone") --# ... keep yielding (doing nothing) until a redstone state change occurs.
end
end
And a "for" loop is generally used to repeat something a set number of times:
local function pulseList()
pulse(1)
pulse(4)
pulse(4)
pulse(1)
pulse(1)
pulse(1)
pulse(4)
end
pulseList() --# Run through the list once.
--# Then do it another ten times:
for i = 1, 10 do
waitForColour( whateverYourItemDetectorColourValueIs )
pulseList()
end
Then one final loop lets us repeat the whole thing forever:
local function waitForColour( colour )
while not rs.testBundledInput( "back", colour ) do --# So long as the desired colour isn't active on the back...
os.pullEvent("redstone") --# ... keep yielding (doing nothing) until a redstone state change occurs.
end
end
local function pulse( colour )
rs.SetBundledOutput("back", colour)
sleep(0.5)
rs.SetBundledOutput("back", 0)
sleep(0.5)
end
local function pulseList()
pulse(1)
pulse(4)
pulse(4)
pulse(1)
pulse(1)
pulse(1)
pulse(4)
end
while true do --# A loop that repeats indefinitely.
pulseList() --# Run through the list once.
--# Then do it another ten times:
for i = 1, 10 do
waitForColour( whateverYourItemDetectorColourValueIs )
pulseList()
end
--# Wait for the colour that signals a restart:
waitForColour( whateverYourOtherSpecialColourIs )
end