'After-completed-comment': Check out existing KeyPad.js module and its doc... and look at this conversation's code as a programming exercise... and a different style of doing things in JS...
Adding a human interfac / user interface is always a challenge. Below code provides you with function to drive and sense a matrix key pad with any nuber of rows and columns directly with Espruino. You need no other things than a passive - bought or DIY - key pad. For DIY, you need only push buttons and wire them in a matrix and let each button connect its row with its column wire when pressed. Then you connect the row and column wires to Espruino GPIO pins.
Schema of market available or DIY key pad with 12 keys (4 rows, 3 cols):
No additional components are needed: GPIO pins provide the built-in pull-up/down resistors to supprt the press detection and scanning.
Usage in your applicaiton is very simple - for example, for a 12-key pad driven with 4 row pins A0..3 and sensed with 3 column pins A4..6 (and default values for start enabled, watching falling edge, and 200[ms] debouncing):
// var keypad = require("KeypadMDS").connect(... // when available as module
var keypad = keypadModule.connect([A0,A1,A2,A3],[A4,A5,A6],function(btn,tme){
console.log("At " + tme + " btn # " + btn + " was pressed"); });
Principle of operation is:
drive pins set to output and low on rows
sense pins set to input-pullup and set on watch for scan
any press (watch) triggers the scan (passing triggering column and time); scan does:
clears all watches
sets all drive pins to input
sets drive pins individually to output and checks triggering/sensing column pin
calculates button number form row(drive) and column(sense) index
puts all drive pins back to output and low and set sense pins on watch again
invokes callback passing calculated button number and time of press
The code inlcudes some more functions to avoid overlapping and overruning events. It also includes two console log/debug statments in .scan()- lines 73 and 80 - to be removed for use in your project. Code is attached for easy download and use. Placed in 'project sandbox modules' folder (see Espruiono IDE setings), it can be pulled as KeypadMDS module with require("KeypadMDS").
*Fedback is welcome. 'Exercise' was triggered by - paraphrased - How can I do some Arduino(-like) loop to monitor my hardware events...* (see conversation).
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
'After-completed-comment': Check out existing KeyPad.js module and its doc... and look at this conversation's code as a programming exercise... and a different style of doing things in JS...
Adding a human interfac / user interface is always a challenge. Below code provides you with function to drive and sense a matrix key pad with any nuber of rows and columns directly with Espruino. You need no other things than a passive - bought or DIY - key pad. For DIY, you need only push buttons and wire them in a matrix and let each button connect its row with its column wire when pressed. Then you connect the row and column wires to Espruino GPIO pins.
Schema of market available or DIY key pad with 12 keys (4 rows, 3 cols):
No additional components are needed: GPIO pins provide the built-in pull-up/down resistors to supprt the press detection and scanning.
Usage in your applicaiton is very simple - for example, for a 12-key pad driven with 4 row pins A0..3 and sensed with 3 column pins A4..6 (and default values for start enabled, watching falling edge, and 200[ms] debouncing):
Principle of operation is:
The code inlcudes some more functions to avoid overlapping and overruning events. It also includes two console log/debug statments in
.scan()
- lines 73 and 80 - to be removed for use in your project. Code is attached for easy download and use. Placed in 'project sandbox modules' folder (see Espruiono IDE setings), it can be pulled as KeypadMDS module withrequire("KeypadMDS")
.*Fedback is welcome. 'Exercise' was triggered by - paraphrased - How can I do some Arduino(-like) loop to monitor my hardware events...* (see conversation).
3 Attachments