My wife is a musician, and this morning she complained that her metronome didn't have the precision she wanted. She could set it to 72 or 76 beats/minute, but nothing in between.
Happily, she said that she didn't need a metronome to be battery-powered, which makes it easier. This looked like a nice opportunity to use a 12-button keypad I had in my parts box with an Arduino Uno. The Arduino would be simple to set up as a metronome with a flashing LED and clicking piezo, and the keypad would take an arbitrary input for tempo.
The result is below. One thing I discovered in working on this is that the Arduino has a very easy-to-use external interrupt scheme, tied to pins 2 and 3. I connected pin 2 to the '*' button on the keypad. So, to enter a tempo, my wife would press '*' to get the Arduino's attention, followed by the number. I used pin 3 to drive an LED, and pin 0 to drive a piezo buzzer.
One useful lesson learned: pins 0 and 1 have special serial duties associated with code loading as well as run-time serial communications for monitoring. That means that if you use serial communications for debugging messages, you must avoid using pins 0 and 1 for anything else or the utility communications will clobber your user data and vice-versa. This prototype continues to use pin 0 for the piezo buzzer, but you get a bit of spurious buzzing at boot-time and when code is loading. Unfortunately, the Arduino Uno has just enough digital input lines to handle the digits 0-9, plus the special character '*' and that leaves only 1 pin for the LED left-over, plus pins 0 and 1. That means there aren't enough pins for mapping to the '#' button, which might have served as a special termination character--an "Enter" key. I got around this by making all tempo inputs a 3-digit number. That means that 60 beats/minute is entered "*060".
Even if you don't need or want a metronome, it seems to me that this is a useful if you want to use a keypad with the Arduino for any kind of digital input. I see that there are matrix-driven keypads that use fewer pins in exchange for more complicated decoding. That's probably a better choice, and if I knew this much I wouldn't have bought this particular keypad. But since I had it on-hand, it's what I used.
Code is attached, and here's how it looks:
- Ken