
sequencer
a step sequencer
> examples sequencer > edit sequencer
A step sequencer: a list of steps, each one setting the left and right outputs for a given time, played in a loop. It is the Jacquard Card Reader of the first age written as code, and the base of every timed machine: a door that opens and closes, a piston elevator, a light show, a press that waits for the belt.
What to build
Put something driven by redstone on the left and on the right of the computer (seen facing the screen): lamps to watch the sequence, pistons, or Create blocks such as a Clutch (powered: the shaft stops) or a Gearshift (powered: the rotation reverses).
How it works
local steps = {
{left = 15, right = 0, time = 1},
{left = 0, right = 15, time = 1},
{left = 15, right = 15, time = 2},
{left = 0, right = 0, time = 1},
}The whole sequence is data: a list (a table) of steps, each step a small table with three fields. Changing the sequence means changing this list, not the code. One cycle lasts 1 + 1 + 2 + 1 = 5 seconds.
while true do
for i, step in ipairs(steps) do
rs.set("left", step.left)
rs.set("right", step.right)
sleep(step.time)
end
endipairs() walks the list in order, giving the position i and the step. step.left reads a field of the step. rs.set() takes a strength from 0 to 15 here, rather than true or false. After the last step, while true starts the list again.
To see the steps go by, print them as they play. Here with two steps only:
local steps = {
{left = 15, right = 0, time = 1},
{left = 0, right = 15, time = 1},
}
for i, step in ipairs(steps) do
print("step " .. i .. ": left " .. step.left .. ", right " .. step.right)
rs.set("left", step.left)
rs.set("right", step.right)
sleep(step.time)
endstep 1: left 15, right 0 step 2: left 0, right 15
Ideas
A piston door or elevator with Create: a Clutch on the left stops the motion, a Gearshift on the right reverses it. Four steps make it go, wait, come back and wait:
Brass local steps = { {left = 0, right = 0, time = 3}, -- moving out {left = 15, right = 0, time = 2}, -- stopped {left = 0, right = 15, time = 3}, -- moving back {left = 15, right = 15, time = 2}, -- stopped }Time the steps to the length of the travel at your speed.
- Add a third output: give each step a
topfield and addrs.set("top", step.top)to the loop. - Play the sequence once per button press: replace
while true dowith a loop that first waits for the button,repeat os.pull_event("redstone") until rs.get("back") > 0, then plays the list once. - Store several sequences in a table of lists and choose one with an argument (
sequencer fast), read fromarg[1].
The program
-- A sequencer, like the Jacquard card reader but written as code:
-- each step sets the left and right outputs for a while.
local steps = {
{left = 15, right = 0, time = 1},
{left = 0, right = 15, time = 1},
{left = 15, right = 15, time = 2},
{left = 0, right = 0, time = 1},
}
while true do
for i, step in ipairs(steps) do
rs.set("left", step.left)
rs.set("right", step.right)
sleep(step.time)
end
end