Create: Computing AgesBrass Docs
Built-in programs

counter

counts redstone pulses

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

Counts the redstone pulses that arrive on the left side of the computer and prints the total after each one. Without wasting a single instruction between pulses: the program sleeps until the redstone changes.

What to build

Put something that pulses on the left of the computer (left as seen facing the screen): a button, a pressure plate at a door to count visitors, a tripwire, an observer, the receiver of a Create Redstone Link.

Terminal
> counter
Waiting for pulses on the left
Pulses: 1
Pulses: 2
Pulses: 3

How it works

Brass
os.pull_event("redstone")

os.pull_event() puts the program to sleep until an event arrives. A redstone event comes each time any input of the computer changes, on any side, going up or down. That is why the program then checks the side it cares about. Waiting this way costs nothing, unlike a loop that would read the input again and again.

Brass
local now = rs.get(side) > 0
if now and not was then
  count = count + 1
  print("Pulses: " .. count)
end
was = now

rs.get() reads the power arriving on a side, from 0 to 15, and > 0 turns it into true or false. A pulse is counted only on its rising edge: when the input is on now (now) but was off before (not was). Without was, the end of each pulse (another redstone event) would be counted too, and so would changes on the other sides.

The same edge detection, tried on a list of readings instead of the real world:

Brass
local readings = {0, 15, 15, 0, 0, 15, 0, 15}
local count, was = 0, false
for _, power in ipairs(readings) do
  local now = power > 0
  if now and not was then
    count = count + 1
  end
  was = now
end
print("Pulses: " .. count)
Screen
Pulses: 3

Ideas

  • Show the count on a Display Board: add display.set({"Visitors", tostring(count)}) after the count goes up, and point a Create Display Link at the computer. See display.
  • A reset button on the right side, checked at each event:

    Brass
    if rs.get("right") > 0 then
      count = 0
      print("Reset")
    end
  • Keep the count across reboots by saving it to a file with fs.write() and reading it back at start with fs.read().
  • Do something every 10 pulses: if count % 10 == 0 then rs.set("top", true) end, to light a lamp when a batch is complete.
  • Count the time between pulses with os.clock() to measure the speed of a production line.

The program

counter
-- Counts redstone pulses arriving on the left: a button, an observer,
-- a machine output... Left and right are seen facing the screen.
local side = "left"
local count = 0
local was = false
print("Waiting for pulses on the " .. side)
while true do
  os.pull_event("redstone")  -- sleeps until an input changes
  local now = rs.get(side) > 0
  if now and not was then
    count = count + 1
    print("Pulses: " .. count)
  end
  was = now
end