Inductive Loop Detector
A wire loop sealed in the road that senses players, mobs, carts, boats and airships above it.
The Inductive Loop Detector is the loop of wire that real roads hide under the asphalt, in front of a traffic light, to know that a car is waiting. Lay it in the road, under the rails or in a landing pad: it senses what stands or passes in the two blocks above it, gives a redstone signal, and tells a computer how many things are there.
local loop = peripheral.wrap("right")
if loop.detected() then
print(loop.count() .. " waiting at the light")
endloop.detected() | Whether anything is above the loop now. |
loop.count() | How many things are above the loop now, counted as listed in the setup above. |
Setting up
Placing it. It is a full block: put it in the road surface, under a rail, or in the floor of a hangar. It watches the space of two blocks right above it.
What it senses. Each of these counts for one:
- players and every mob or animal (villagers, cows, zombies...), armor stands included;
- minecarts of every kind, and boats;
- any other entity that carries a passenger, such as a moving Create contraption with someone in its seat;
- with Create Aeronautics, each airship or vehicle whose bounds cross the space above the loop, whatever its size. A loop built into a vehicle does not count the vehicle that carries it.
Dropped items, arrows and experience orbs are ignored, and so is an empty moving Create contraption, a train included. A player riding a minecart counts twice: the player and the cart.
Its own redstone. Four times a second the loop looks above it. While something is there, it gives a signal of 15 to the blocks beside it and below it, never to the block above it (the road). A lamp or redstone dust next to it lights up with no computer at all.
Reaching it from a computer. Put it against a face of the computer (peripheral.wrap("left")), or against a Data Cable of the computer's network, from the Minicomputer on: a cable laid under the road reaches every loop of a crossroads, each one named after its coordinates (inductive_loop@120,63,-35). peripheral.find("inductive_loop") takes the first one found. See Peripherals.
When the loop touches the computer from the side or from above, its signal arrives on that face: os.pull_event("redstone") then wakes your program exactly when something arrives or the last thing leaves, and the program costs nothing in between.
Detection
Both methods look above the loop at the moment you call them: they do not wait for the loop's own check, four times a second.
Whether anything is above the loop now.
- boolean
truewhen at least one thing is above the loop
It is the question a traffic light asks: is a car waiting?
local loop = peripheral.wrap("right")
local light = peripheral.wrap("left")
light.set("red")
repeat
sleep(0.5)
until loop.detected()
light.set("red_amber")
sleep(2)
light.set("green")See also inductive_loop.count()
How many things are above the loop now, counted as listed in the setup above.
- number
- how many things are above the loop, 0 when there is nothing
local loop = peripheral.find("inductive_loop")
print(loop.count(), "waiting")count() tells more than detected(): a queue of three carts, a player alone (1) or a player in a cart (2), two airships over the same pad.
See also inductive_loop.detected()
Examples
Counting the carts that pass
A loop under the rails of a station. Every time the count goes up, new carts have arrived above the loop: adding these steps counts the carts that went by. A Display Link pointed at the computer shows the total.
local loop = peripheral.find("inductive_loop")
local passed = 0
local before = 0
while true do
local now = loop.count()
if now > before then
passed = passed + now - before
print("carts so far: " .. passed)
display.set({"Carts today", tostring(passed)})
end
before = now
sleep(0.1)
endTen checks a second are enough: a cart at full speed stays about a quarter of a second over the loop.
An airship landing pad
Loops in the floor of a landing pad, on the computer's cable. When an airship sets down, the hangar door opens (a Create Mechanical Piston, a door or a gantry on the top output); when it takes off, the door closes again.
local pad = peripheral.find("inductive_loop")
local docked = false
while true do
local here = pad.detected()
if here ~= docked then
docked = here
rs.set("top", here)
if here then
print("airship docked")
else
print("pad clear")
end
end
sleep(0.5)
endA player walking over the pad also opens the door. To tell an airship from a walker, put a second loop at the far end of the pad, where only a long ship reaches, and wait until both see something.
Waiting for a car with an event
The loop is against the left face of the computer. The program sleeps until the redstone of the loop changes:
while true do
os.pull_event("redstone")
if rs.get("left") > 0 then
print("a car arrived")
else
print("the road is clear")
end
end