Lessons and the learning game

Buttons, encoders, keypads and touch

Read people properly on an Uno: bounce and debounce, clicks and long presses, encoders, keypads, touch and joysticks, then a retro game menu.

Take the course at /learn/path/inputs-in-depth. It is an intermediate course: it assumes the Uno course, and it does not repeat the keyboard course's diodes and ghosting.

The lessons

Buttons. One press counted two ways: an interrupt on every raw edge, and a debounce that waits 20 ms of quiet. A scope on the pin shows the bounce. Then a state machine for click, double click and long press.

Knobs and keys. A rotary encoder on two interrupt pins with a quadrature state table, its bounce, and its push switch. A 4x4 keypad with pressed, held and released events, a chord, and a door code typed into a buffer.

Touch and sticks. An MPR121's touch bitmask, filtered readings against baselines, and thresholds. A joystick centered at start-up, with a dead zone, -100 to 100 axes, repeating direction events and a fire button.

Project. Star Blaster's menu on a 16x2 I2C LCD and the encoder: scroll and press, a sound toggle, a high score table in EEPROM, and initials dialed in letter by letter.

The benches

The button lessons have their own bench: a pushbutton from pin 2 to ground and a scope on the pin. The others open gallery circuits: the rotary encoder (CLK 2, DT 3, SW 4, a LED on 9), the keypad lock (rows on 9 to 6, columns on 5 to 2), the touch piano (an MPR121 at 0x5A and a piezo on 8) and the joystick bar graph (VRX A0, VRY A1, SW 12, lamps on 2 to 9). The project adds an I2C LCD at 0x27 to the encoder board.

Does the simulated button bounce?

Yes. The pushbutton's bounce property is typical by default, with worst and none. On the bench, one press and release of a typical button made 8 to 12 edges, a worst-case one 26 to 34, and a clean one exactly 2. The rotary encoder has a bounce property too, on by default.

What each sketch does

  • inpbounce: raw edges counted by a CHANGE interrupt, presses by a DEBOUNCE_MS wait for quiet.
  • inpclicks: a five-state machine with LONG_MS and DOUBLE_MS.
  • inpencoder: a 16-entry quadrature table on two interrupts, STEPS_PER_DETENT, and the switch to reset.
  • inpkeypad: Keypad events through addEventListener(), isPressed() for a chord, and a CODE checked with strcmp().
  • inptouch: touched(), filteredData(), baselineData() and setThresholds(), a note per electrode.
  • inpjoy: center calibration, DEAD_ZONE, direction repeat and the button.
  • inpmenu: four screens as a state machine, the encoder and switch, EEPROM scores and a GAME_SCORE stand-in game.

Honest limits

  • Bounce is modeled as a burst of edges whose length depends on the setting, not from the physics of a particular switch.
  • The MPR121 reports a fixed drop for a touch: there is no finger distance, moisture or baseline drift to tune against.
  • The joystick and encoder have no mechanical feel; a click on the canvas is a detent.
  • The game in the menu project is a stand-in that always scores 3150.

More pins: expanders, muxes and drivers puts keypads on I2C, and Custom keyboards from the matrix up covers diodes and ghosting.