Hone

Lessons · Industrial control · acting on the change, not the state

Exactly once

Rising-edge logic is true for one scan, the scan a bit goes from off to on. It turns a held button into a single event.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

Increment a count, toggle a mode, start a cycle: things that must happen once per press, not once per scan for as long as the finger is on the button.

How to think about it

Compare now with last scan: [Btn] [/BtnWas] is true only on the first scan Btn is on. Then, below, remember the state: [Btn] (BtnWas). The order matters.

Worked example

[Btn] [/BtnWas] (Press)
True only on the scan Btn first goes true.
[Btn] (BtnWas)
Memory, written after it was read, so next scan the first rung sees BtnWas on and Press drops.

Your turn

Detect the falling edge instead.

[Btn] [BtnWas] (Release)
[Btn] (BtnWas)

The trap

Put the memory rung above the detector and the edge never appears: BtnWas is already on by the time the detector reads it.

Practise acting on the change, not the state on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.