Traffic Light
A three-aspect road signal that follows redstone on its own, until a computer takes it over.
The Traffic Light is a road signal with three lamps, red on top, amber in the middle and green at the bottom, like the first electric signals of the 1910s. On its own it follows redstone. Once a computer sets it, it shows whatever your program asks for, and redstone no longer moves it until the program hands it back.
local light = peripheral.wrap("left")
light.set("green")light.set(aspect) | Shows an aspect, and takes control of the light: from now on, redstone is ignored. |
light.get() | The aspect the light shows now, whoever chose it: your program, or the redstone around it. |
light.release() | Hands the light back to redstone. It shows at once the aspect its redstone input asks for (off when it receives nothing). |
Setting up
Placing it. The lamps face you when you place the block, so stand where the drivers will come from. A Create Wrench turns it afterwards.
On its own, it follows redstone. The strongest signal it receives chooses the aspect. A redstone clock, a comparator or a Create Redstone Link can run a crossing without any computer:
| redstone | aspect |
|---|---|
| 0 | off |
| 1 to 4 | red |
| 5 to 7 | red_amber |
| 8 to 11 | green |
| 12 to 14 | amber |
| 15 | flashing |
Reaching it from a computer. Put the light against any face of the computer and name it by that face: peripheral.wrap("left"). On a Minicomputer or a newer computer, it can also touch a Data Cable of the computer's network: it is then named after its coordinates, like traffic_light@120,64,-35. And peripheral.find("traffic_light") takes the first light it finds (the faces first) without any name. Every computer can drive it, the Tube Computer included. See Peripherals and Every device.
A computer can also move a light through redstone, using the table above: rs.set("left", 9) shows green on a light against its left face. For a light far away, put a Create Redstone Link in receiver mode next to it, and transmit the strength from the computer with link.set. The light stays under redstone control.
Control
light.set(aspect)
Shows an aspect, and takes control of the light: from now on, redstone is ignored.
aspectstring"off","red","red_amber","amber","green"or"flashing"
The six aspects:
| aspect | the light shows |
|---|---|
"off" | nothing |
"red" | red: stop |
"red_amber" | red and amber together: get ready, green comes next |
"amber" | amber alone: stop if you can, red comes next |
"green" | green: go |
"flashing" | amber blinking, half a second on, half a second off: give way (what real lights show at night or out of order) |
local light = peripheral.wrap("left")
light.set("red")
sleep(10)
light.set("red_amber")
sleep(2)
light.set("green")The light keeps the computer's aspect after the program ends, after a reboot, and even when the computer is broken: call light.release() to hand it back to redstone (breaking and placing the light again does it too). A lit lamp gives a little light around it (level 7).
When it fails:
- an unknown aspect stops the program with
bad aspect 'yellow' (off, red, red_amber, amber, green, flashing); - when the block on that face is not a Traffic Light any more (broken, replaced):
traffic light removed; - when its chunk is not loaded:
peripheral is not loaded.
The aspect the light shows now, whoever chose it: your program, or the redstone around it.
- string
- the aspect shown now:
"off","red","red_amber","amber","green"or"flashing"
Reading never takes control, so a computer can watch a crossing that a redstone clock runs, and react to it:
local light = peripheral.find("traffic_light")
if light.get() == "red" then
rs.set("top", true) -- lowers the barrier of the level crossing
endA flashing light reads "flashing" in both halves of its blink. When the light has been broken, get returns nil.
See also traffic_light.set()
light.release()
Hands the light back to redstone. It shows at once the aspect its redstone input asks for (off when it receives nothing).
Use it when the computer only takes over part of the time, for example for a priority vehicle, and the redstone timer runs the crossing otherwise:
local light = peripheral.wrap("left")
light.set("green") -- the fire engine goes through
sleep(15)
light.release() -- the redstone timer takes over again
print(light.get())Calling it on a light that is not under control does no harm. When the light has been broken, it does nothing.
See also traffic_light.set()
Examples
A full light cycle
The classic sequence, as in Britain or Germany: green, amber, red, then red and amber together just before green. Save it as startup so the crossing starts again by itself when the chunk loads.
local light = peripheral.wrap("left")
if light == nil then
error("no traffic light on the left")
end
while true do
light.set("green")
sleep(20)
light.set("amber")
sleep(3)
light.set("red")
sleep(20)
light.set("red_amber")
sleep(2)
endA real crossroads has two lights that must never be green together: the Smart intersection recipe drives them from one computer. The built-in traffic program waits for a car on an Inductive Loop Detector before it turns red.
A pedestrian crossing
The road stays green until someone presses the button. One light faces the road, a second one faces the crossing (green means walk), and a stone button sits on the front of the computer.
local cars = peripheral.wrap("left")
local walkers = peripheral.wrap("right")
cars.set("green")
walkers.set("red")
while true do
os.pull_event("redstone")
if rs.get("front") > 0 then
sleep(3) -- the cars cannot stop at once
cars.set("amber")
sleep(3)
cars.set("red")
sleep(1)
walkers.set("green")
sleep(10)
walkers.set("flashing") -- hurry up
sleep(4)
walkers.set("red")
sleep(1)
cars.set("red_amber")
sleep(2)
cars.set("green")
end
endos.pull_event("redstone") sleeps until a redstone input of the computer changes, so the program costs nothing while nobody comes. The check rs.get("front") > 0 keeps only a button pressed right now: the event of the button popping back out starts nothing, and neither does an older press made while people were crossing (a stone button stays in for one second).