Minecraft – Compact last in, first out redstone circuit

minecraft-java-editionminecraft-redstone

I am trying to make some 2×2 elevator doors for my piston elevator in minecraft and while I have all the mechanics in place I'm struggling to make the timing circuit right.

Initial setup: doors closed. B denotes a block, P denotes a piston

[B1][B2]    [P2][P3]

        [P1]

Piston 2 pulls block 2

[B1]    [B2][P2][P3]

        [P1]

Piston 1 pulls block 2

[B1]        [P2][P3]
        [B2]
        [P1]

Piston 3 pushes piston 2

[B1]    [P2]    [P3]
        [B2]
        [P1]

Finally, piston 2 pulls block 1

    [B1][P2]    [P3]
        [B2]
        [P1]

This process is then reversed for door closing.

This method for pulling 2 blocks back each by one block works just fine when all four actions are triggered manually, but I want to now create a timing circuit that will run all four actions sequentially when turned on, then run them all in reverse when turned off. Essentially what I need is a last-in, first out order like a "stack" in computer science. Any suggestions for a timing circuit that would accomplish this?

Best Answer

Ultimately came up with a decent solution. 4 blocks with dust are arranged horizontally with a space between each. A U shaped circuit of dust and repeaters runs around them. The four blocks will light in a last in, first out order