Reading buttons with an Arduino isn't hard. But one ends up writing the same handful of code: define a static variable to hold the last recorded button state, read the current state, compare them, perform some action if it's just changed, then update the old value to be the one you just read. Every. Single. Time. And if you need two buttons? It just gets worse. Now you need TWO static variables, which will probably end up being global because who can be bothered to properly do everything in scope, and you know what I just can't be bothered anymore.

So here's a nice library to do all of that crap for me (and you).

Each button object needs only a pin number, and will handle setting the pinMode in it's initialization. The methods are:

  • Button.getState()
  • Button.isRising()
  • Button.isFalling()

Button.update() needs to be called every program loop, as this is what checks the button's state and allows the calculation of the rising and falling tags.

I haven't gotten around to implementing software debouncing, but for most buttons that shouldn't be much of a problem anyway. I may implement that later, if I find a need for it.