Create: Computing AgesBrass Docs
Built-in programs

traffic

a traffic light cycle

Runs on All All computers · 20 lines
Type this at the prompt of a computer to copy it onto the medium:
Terminal
> examples traffic
> edit traffic

Runs a Traffic Light through its real cycle: green, amber, red, then red and amber together before green again. With an Inductive Loop Detector, the light stays green until a vehicle waits on the loop. This is the program of the Microcontroller's Ponder scene.

What to build

  • A Traffic Light touching the left of the computer (seen facing the screen).
  • If you like, an Inductive Loop Detector touching the right side. It senses players, mobs, minecarts, boats and Create Aeronautics vehicles up to two blocks above it.

A Microcontroller, driven from below, fits well in a pole next to the road. With nothing on the left, the program stops with no traffic light on the left.

How it works

Brass
local light = peripheral.wrap("left")
local loop = peripheral.wrap("right")

Both blocks are wrapped. The loop is optional: when nothing is on the right, loop is nil.

Brass
light.set("green")
if loop then
  repeat sleep(0.5) until loop.detected()
end
sleep(3)

traffic_light.set() lights an aspect: "off", "red", "red_amber", "amber", "green" or "flashing". As long as a computer drives the light, the computer decides, whatever the redstone says (traffic_light.release() gives it back to redstone). if loop then skips the wait when there is no loop. repeat ... until checks the loop twice a second until inductive_loop.detected() answers true, then the light stays green 3 more seconds.

Brass
light.set("amber")
sleep(2)
light.set("red")
sleep(8)
light.set("red_amber")
sleep(1)

The rest of the cycle: one aspect after the other, each with its own time. Then while true starts again with green.

Ideas

  • Keep the cycle in a table, like the sequencer: easy to read and to tune.

    Brass
    local cycle = {
      {"green", 3}, {"amber", 2}, {"red", 8}, {"red_amber", 1},
    }
    local total = 0
    for _, phase in ipairs(cycle) do
      print(phase[1], phase[2] .. " s")
      total = total + phase[2]
    end
    print("cycle: " .. total .. " s")
    Screen
    green   3 s
    amber   2 s
    red 8 s
    red_amber   1 s
    cycle: 14 s

    The loop then becomes for _, phase in ipairs(cycle) do light.set(phase[1]) sleep(phase[2]) end.

  • Flash amber at night: light.set("flashing") while os.day_time() is past 13,000.
  • Count the vehicles with inductive_loop.count() and give a longer green to the busier road.
  • A pedestrian button: a button on the back that ends the green early.
  • Two lights for a crossroads, always opposite: the traffic intersection recipe.

The program

traffic
-- Traffic light controller: a traffic light on the left, and
-- optionally an inductive loop on the right to wait for cars.
local light = peripheral.wrap("left")
local loop = peripheral.wrap("right")
if light == nil then
  error("no traffic light on the left")
end
while true do
  light.set("green")
  if loop then
    repeat sleep(0.5) until loop.detected()
  end
  sleep(3)
  light.set("amber")
  sleep(2)
  light.set("red")
  sleep(8)
  light.set("red_amber")
  sleep(1)
end