State Changes

Because there are only two conditions possible when reading or writing digital inputs and outputs—high and low—we can use this predictability to detect a state change, where a pin changes from high to low or low to high, or even to count these changes. To detect a state change on a digital input, we don’t actually need to know the precise state that the pin is in. Rather, we only need to know when a pin has changed from one state to another.

To do this, we need a way to compare the pin’s current state with the state of the pin the last time we read it. If we check an input and it’s high but the last time we checked it was low, then the button has been pressed. On the other hand, if we check an input and it’s low but last time we ...

Get Beginning Arduino Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.